如何在 C# 中将“Keys”枚举值转换为“int”字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/769529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I convert a "Keys" enum value to an "int" character in C#?
提问by Andy Stampor
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”是一个数字,我想把它当作一个数字并将它存储为一个整数。所以,如果我在数字键盘上点击“8”,我不想要“Numpad8”,我想要我可以添加或减去的 int 值 8。
So, how do I convert from the KeyValue to an int?
那么,如何从 KeyValue 转换为 int?
采纳答案by Tommi Forsstr?m
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.
...如果重点是获取他们输入的键的标签的数值。
回答by TheTXI
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 的值并根据该值分配一个值一个合适的整数。
回答by Jeremy
int i = (int)e.KeyValue;
int i = (int)e.KeyValue;
回答by Adam Robinson
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。没有简单的方法可以将 aKeys
转换为表示键上数字的数值。你能得到的最接近的可能是 ASCII 等价物,它仍然需要翻译。
回答by Samuel
Could you just listen for the KeyPress
event instead? It will give the character that was actually pressed.
你可以只听KeyPress
事件吗?它将给出实际按下的字符。
回答by Robin Day
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())
回答by John Rasch
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);
}
回答by John Fisher
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;
}
回答by salgo60
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... }
}
}
}
回答by THX-1138
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.
- 确定给定的键是否代表一个数字。
- 如果键表示数字,则返回键的数值。
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.
可以说,这不是最简单的解决方案,而是对解决方案进行紧密建模的解决方案。