This seems like something that should be easy, but I am having a tough time figuring out what needs to happen here.
这似乎应该很容易,但我很难搞清楚这里需要发生什么。
In the "KeyDown" eventhandler, if the "e.KeyValue" is a number, I want to treat it as a number and store it as an int. So, if I hit "8" on the number pad, I don't want "Numpad8" I want the int value 8 that I can add or subtract or whatever.
在“KeyDown”事件处理程序中,如果“e.KeyValue”是一个数字,我想将其视为一个数字并将其存储为int。所以,如果我在数字键盘上点击“8”,我不想要“Numpad8”我想要我可以加或减的int值8或者其他什么。
So, how do I convert from the KeyValue to an int?
那么,我如何从KeyValue转换为int?
10 个解决方案
#1
I'd go with this solution:
我会选择这个解决方案:
int value = -1;
if (e.KeyValue >= ((int) Keys.NumPad0) && e.KeyValue <= ((int) Keys.NumPad9)) { // numpad
value = e.KeyValue - ((int) Keys.NumPad0);
} else if (e.KeyValue >= ((int) Keys.D0) && e.KeyValue <= ((int) Keys.D9)) { // regular numbers
value = e.KeyValue - ((int) Keys.D0);
}
...if the point was to get the numeric value of the label of they key that was punched in.
...如果要点是获得被打孔的键的标签的数值。
#2
Something like this should work well: (Edited)
像这样的东西应该运作良好:(编辑)
int keyVal = (int)e.KeyValue;
int value = -1;
if ((keyVal >= (int)Keys.D0 && keyVal <= (int)Keys.D9)
{
value = (int)e.KeyValue - (int)Keys.D0;
}
else if (keyVal >= (int)Keys.NumPad0 && keyVal <= (int)Keys.NumPad9)
{
value = (int)e.KeyValue - (int)Keys.NumPad0;
}
#3
Facts: Keyboard has keys. Some keys represent numbers and some do not.
事实:键盘有键。有些键代表数字,有些则没有。
Problem (rephrased): Yield a numeric value represented by a key, if the key represents a number.
问题(改写):如果键表示数字,则产生由键表示的数值。
To solve the problem it is necessary to know which keys (out of set of all keys) represent numbers as well as exact numeric value each (number) key represents.
为了解决这个问题,有必要知道哪些键(所有键中的一组)代表数字以及每个(数字)键代表的精确数值。
To my knowledge there is no an easy way to get such a mapping from the framework.
据我所知,没有一种简单的方法可以从框架中获得这样的映射。
Note: The fact D0-D9 and NumPad0-NamPad9 are sequential in the Keys enum is accidental and relying on these values being ordered sequentially is unfounded.
注意:事实D0-D9和NumPad0-NamPad9在Keys枚举中是顺序的是偶然的,并且依赖于按顺序排序的这些值是没有根据的。
So solution would be:
所以解决方案是:
- Determine if given key represents a number.
- Return numeric value of the key if key represents a number.
确定给定的键是否代表数字。
如果key表示数字,则返回键的数值。
private static readonly IDictionary<Keys, int> NumericKeys =
new Dictionary<Keys, int> {
{ Keys.D0, 0 },
{ Keys.D1, 1 },
{ Keys.D2, 2 },
{ Keys.D3, 3 },
{ Keys.D4, 4 },
{ Keys.D5, 5 },
{ Keys.D6, 6 },
{ Keys.D7, 7 },
{ Keys.D8, 8 },
{ Keys.D9, 9 },
{ Keys.NumPad0, 0 },
{ Keys.NumPad1, 1 },
{ Keys.NumPad2, 2 },
{ Keys.NumPad3, 3 },
{ Keys.NumPad4, 4 },
{ Keys.NumPad5, 5 },
{ Keys.NumPad6, 6 },
{ Keys.NumPad7, 7 },
{ Keys.NumPad8, 8 },
{ Keys.NumPad9, 9 }
};
private int? GetKeyNumericValue(KeyEventArgs e) {
if (NumericKeys.ContainsKey(e.KeyCode)) return NumericKeys[e.KeyCode];
else return null;
}
Arguably, not the simplest solution, but the one that models the solution closely.
可以说,不是最简单的解决方案,而是密切建模解决方案的解决方案。
#4
Depending on how many keys you are keeping track of, the simplest method would be for you to create a select case (or switch statement in C# I guess?) that would check the value of your keydown and depending upon that value assign a value to an integer that is appropriate.
根据您要跟踪的密钥数量,最简单的方法是创建一个select case(或C#中的switch语句,我猜?),它会检查你的keydown的值,并根据该值赋值给一个合适的整数。
#5
If you want NUMERIC values, you'll have to create a switch(e.KeyCode)
statement and evaluate the key and create your own int. There's no simple way to turn a Keys
into a numeric value that represents the number on the key. The closest you could get might be the ASCII equivalent, which would still need to be translated.
如果需要NUMERIC值,则必须创建一个switch(e.KeyCode)语句并评估该键并创建自己的int。没有简单的方法可以将Keys转换为表示键上数字的数值。您可以获得的最接近的可能是ASCII等价物,仍然需要翻译。
#6
Could you just listen for the KeyPress
event instead? It will give the character that was actually pressed.
你能不能只听一下KeyPress活动?它会给出实际按下的角色。
#7
This function will do what you want:
这个功能可以做你想要的:
private int GetKeyValue(int keyValue)
{
if (keyValue >= 48 && keyValue <= 57)
{
return keyValue - 48;
}
else if (keyValue >= 96 && keyValue <= 105)
{
return keyValue - 96;
}
else
{
return -1; // Not a number... do whatever...
}
}
Call it like so:
这样称呼它:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int num = GetKeyValue(e.KeyValue);
}
#8
The KeyValue represents the character code for the key that was pressed. In order to obtain its numerical valaue you should find the character for the key that was pressed, then parse it from a character to an integer.
KeyValue表示按下的键的字符代码。为了获得它的数值,您应该找到按下的键的字符,然后将其从字符解析为整数。
Something like Convert.ToInt32(e.KeyCode.ToString())
像Convert.ToInt32(e.KeyCode.ToString())
#9
Or create a extension method and call it like
或者创建一个扩展方法并调用它
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int num = e.KeyValue.GetKeyValue();
}
where the extension method is
扩展方法的位置
public static class MyExtensions
{
public static int GetKeyValue(this int keyValue)
{
if (keyValue >= 48 && keyValue <= 57)
{
return keyValue - 48;
}
else if (keyValue >= 96 && keyValue <= 105)
{
return keyValue - 96;
}
else
{
return -1; // Not a number... do whatever... }
}
}
}
#10
int i = (int)e.KeyValue;
int i =(int)e.KeyValue;
#1
I'd go with this solution:
我会选择这个解决方案:
int value = -1;
if (e.KeyValue >= ((int) Keys.NumPad0) && e.KeyValue <= ((int) Keys.NumPad9)) { // numpad
value = e.KeyValue - ((int) Keys.NumPad0);
} else if (e.KeyValue >= ((int) Keys.D0) && e.KeyValue <= ((int) Keys.D9)) { // regular numbers
value = e.KeyValue - ((int) Keys.D0);
}
...if the point was to get the numeric value of the label of they key that was punched in.
...如果要点是获得被打孔的键的标签的数值。
#2
Something like this should work well: (Edited)
像这样的东西应该运作良好:(编辑)
int keyVal = (int)e.KeyValue;
int value = -1;
if ((keyVal >= (int)Keys.D0 && keyVal <= (int)Keys.D9)
{
value = (int)e.KeyValue - (int)Keys.D0;
}
else if (keyVal >= (int)Keys.NumPad0 && keyVal <= (int)Keys.NumPad9)
{
value = (int)e.KeyValue - (int)Keys.NumPad0;
}
#3
Facts: Keyboard has keys. Some keys represent numbers and some do not.
事实:键盘有键。有些键代表数字,有些则没有。
Problem (rephrased): Yield a numeric value represented by a key, if the key represents a number.
问题(改写):如果键表示数字,则产生由键表示的数值。
To solve the problem it is necessary to know which keys (out of set of all keys) represent numbers as well as exact numeric value each (number) key represents.
为了解决这个问题,有必要知道哪些键(所有键中的一组)代表数字以及每个(数字)键代表的精确数值。
To my knowledge there is no an easy way to get such a mapping from the framework.
据我所知,没有一种简单的方法可以从框架中获得这样的映射。
Note: The fact D0-D9 and NumPad0-NamPad9 are sequential in the Keys enum is accidental and relying on these values being ordered sequentially is unfounded.
注意:事实D0-D9和NumPad0-NamPad9在Keys枚举中是顺序的是偶然的,并且依赖于按顺序排序的这些值是没有根据的。
So solution would be:
所以解决方案是:
- Determine if given key represents a number.
- Return numeric value of the key if key represents a number.
确定给定的键是否代表数字。
如果key表示数字,则返回键的数值。
private static readonly IDictionary<Keys, int> NumericKeys =
new Dictionary<Keys, int> {
{ Keys.D0, 0 },
{ Keys.D1, 1 },
{ Keys.D2, 2 },
{ Keys.D3, 3 },
{ Keys.D4, 4 },
{ Keys.D5, 5 },
{ Keys.D6, 6 },
{ Keys.D7, 7 },
{ Keys.D8, 8 },
{ Keys.D9, 9 },
{ Keys.NumPad0, 0 },
{ Keys.NumPad1, 1 },
{ Keys.NumPad2, 2 },
{ Keys.NumPad3, 3 },
{ Keys.NumPad4, 4 },
{ Keys.NumPad5, 5 },
{ Keys.NumPad6, 6 },
{ Keys.NumPad7, 7 },
{ Keys.NumPad8, 8 },
{ Keys.NumPad9, 9 }
};
private int? GetKeyNumericValue(KeyEventArgs e) {
if (NumericKeys.ContainsKey(e.KeyCode)) return NumericKeys[e.KeyCode];
else return null;
}
Arguably, not the simplest solution, but the one that models the solution closely.
可以说,不是最简单的解决方案,而是密切建模解决方案的解决方案。
#4
Depending on how many keys you are keeping track of, the simplest method would be for you to create a select case (or switch statement in C# I guess?) that would check the value of your keydown and depending upon that value assign a value to an integer that is appropriate.
根据您要跟踪的密钥数量,最简单的方法是创建一个select case(或C#中的switch语句,我猜?),它会检查你的keydown的值,并根据该值赋值给一个合适的整数。
#5
If you want NUMERIC values, you'll have to create a switch(e.KeyCode)
statement and evaluate the key and create your own int. There's no simple way to turn a Keys
into a numeric value that represents the number on the key. The closest you could get might be the ASCII equivalent, which would still need to be translated.
如果需要NUMERIC值,则必须创建一个switch(e.KeyCode)语句并评估该键并创建自己的int。没有简单的方法可以将Keys转换为表示键上数字的数值。您可以获得的最接近的可能是ASCII等价物,仍然需要翻译。
#6
Could you just listen for the KeyPress
event instead? It will give the character that was actually pressed.
你能不能只听一下KeyPress活动?它会给出实际按下的角色。
#7
This function will do what you want:
这个功能可以做你想要的:
private int GetKeyValue(int keyValue)
{
if (keyValue >= 48 && keyValue <= 57)
{
return keyValue - 48;
}
else if (keyValue >= 96 && keyValue <= 105)
{
return keyValue - 96;
}
else
{
return -1; // Not a number... do whatever...
}
}
Call it like so:
这样称呼它:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int num = GetKeyValue(e.KeyValue);
}
#8
The KeyValue represents the character code for the key that was pressed. In order to obtain its numerical valaue you should find the character for the key that was pressed, then parse it from a character to an integer.
KeyValue表示按下的键的字符代码。为了获得它的数值,您应该找到按下的键的字符,然后将其从字符解析为整数。
Something like Convert.ToInt32(e.KeyCode.ToString())
像Convert.ToInt32(e.KeyCode.ToString())
#9
Or create a extension method and call it like
或者创建一个扩展方法并调用它
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int num = e.KeyValue.GetKeyValue();
}
where the extension method is
扩展方法的位置
public static class MyExtensions
{
public static int GetKeyValue(this int keyValue)
{
if (keyValue >= 48 && keyValue <= 57)
{
return keyValue - 48;
}
else if (keyValue >= 96 && keyValue <= 105)
{
return keyValue - 96;
}
else
{
return -1; // Not a number... do whatever... }
}
}
}
#10
int i = (int)e.KeyValue;
int i =(int)e.KeyValue;