繁体   English   中英

将小键盘输入转换为他的int值

[英]Convert numpad key input to his int value

我需要获取键输入,如果它是数字键盘 1-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);
    }*/
}

Position 类型仅包含 Row 和 Col 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;
}

如果您需要 col 和 row 值:

col = (key+1) / 3;
row = (key+1) % 3;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM