C# 将numpad键输入转换为他的int值

C# 将numpad键输入转换为他的int值,c#,xna,keyboard,C#,Xna,Keyboard,我需要获取键输入,如果是numpad1-9之一,则获取它的int值 例如,如果按下NumPad9,我需要得到值9 我已经研究了一个小时,似乎无法解决它 以下是我迄今为止所做的工作: class Input { private int[] Key = { 7, 8, 9, 4, 5, 6, 1, 2, 3 }; private Position[] Values; public Input() { Values = new Position[9];

我需要获取键输入,如果是numpad1-9之一,则获取它的int值

例如,如果按下NumPad9,我需要得到值9

我已经研究了一个小时,似乎无法解决它

以下是我迄今为止所做的工作:

class Input
{
    private int[] Key = { 7, 8, 9, 4, 5, 6, 1, 2, 3 };
    private Position[] Values;
    public Input()
    {
        Values = new Position[9];
        int index = 0;
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                Values[index++] = new Position(i, j);

    }
    public Position GetPos(int Key)
    {
        return Values[Key];
    }
    /*public Position ReadInput(KeyboardState Keyboard)
    {
     * Here is the function that I need to call, how can I check efficiently if one of the
     * Numpads 1-9 is pressed?
     * return GetPos(IntegerValue);
    }*/
}
类输入
{
私有int[]密钥={7,8,9,4,5,6,1,2,3};
私人地位[]价值观;
公共投入()
{
数值=新位置[9];
int指数=0;
对于(int i=0;i<3;i++)
对于(int j=0;j<3;j++)
数值[index++]=新位置(i,j);
}
公共位置GetPos(int键)
{
返回值[键];
}
/*公共位置读取输入(键盘状态键盘)
{
*下面是我需要调用的函数,如何有效地检查
*是否按了Numpads 1-9?
*返回GetPos(IntegerValue);
}*/
}
位置类型仅包含行和列int值


另外,我如何检查是否只按了一个键?

我不确定您需要什么,但应该是类似于此的

bool TryReadInput(out int Value)
{
    int Min = (int) Keys.D0;
    int Max = (int) Keys.D9;
    for (int k = Min; k<=Max; k++)
    {
         if (current_kb_state.IsKeyDown( (Keys) k) 
           && previous_kb_state.IsKeyUp( (Keys) k))
         { 
             value = k - Min;
             return true;
         }   
    }
    value = -1;
    return false;
}

我不确定你需要什么,但应该是类似的东西

bool TryReadInput(out int Value)
{
    int Min = (int) Keys.D0;
    int Max = (int) Keys.D9;
    for (int k = Min; k<=Max; k++)
    {
         if (current_kb_state.IsKeyDown( (Keys) k) 
           && previous_kb_state.IsKeyUp( (Keys) k))
         { 
             value = k - Min;
             return true;
         }   
    }
    value = -1;
    return false;
}
看这个看这个