[英]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.我需要获取键输入,如果它是数字键盘 1-9 之一,则获取它的 int 值。
for example if NumPad9 is pressed I need to get the value 9.例如,如果按下 NumPad9,我需要获得值 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. Position 类型仅包含 Row 和 Col int 值。
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 和 row 值:
col = (key+1) / 3;
row = (key+1) % 3;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.