简体   繁体   中英

Convert numpad key input to his int value

I need to get key input, and if it's one of the numpads 1-9, get it's int value.

for example if NumPad9 is pressed I need to get the value 9.

I have been working on it for an hour, can't seem to solve it.

Here is what I have done so far:

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);
    }*/
}

The Position type just contains Row and Col int values.

Also, how can I check if only one key is pressed?

I'm not sure what you need, but should be something similar to this...

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;
}

if you need the col and row values:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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