[英]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.