如何将“密钥”转换为 枚举值转换为“int” C# 中的字符?
这看起来应该很容易,但我很难弄清楚这里需要发生什么。
在“KeyDown”事件处理程序中,如果“e.KeyValue”是一个数字,我想将其视为数字并将其存储为 int。 所以,如果我在数字键盘上点击“8”,我不需要“Numpad8”,我想要的是可以加、减或其他的 int 值 8。
那么,如何将 KeyValue 转换为 int 呢?
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.
So, how do I convert from the KeyValue to an int?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我会采用这个解决方案:
...如果重点是获取所输入的键的标签的数值。
I'd go with this solution:
...if the point was to get the numeric value of the label of they key that was punched in.
像这样的东西应该效果很好:(已编辑)
Something like this should work well: (Edited)
事实:
键盘有按键。
有些键代表数字,有些则不代表数字。
问题(改写):
如果键代表数字,则生成由键表示的数值。
为了解决这个问题,有必要知道哪些键(所有键的集合中)代表数字以及每个(数字)键代表的确切数值。
据我所知,没有一种简单的方法可以从框架中获取这样的映射。
注意:D0-D9 和 NumPad0-NamPad9 在 Keys 枚举中是连续的这一事实是偶然的,依赖这些值按顺序排序是没有根据的。
所以解决方案是:
可以说,这不是最简单的解决方案,而是紧密模拟该解决方案的解决方案。
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.
So solution would be:
Arguably, not the simplest solution, but the one that models the solution closely.
根据您跟踪的键数量,最简单的方法是创建一个 select case(我猜是 C# 中的 switch 语句?),它会检查 keydown 的值,并根据该值将一个值分配给一个合适的整数。
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.
如果您想要 NUMERIC 值,则必须创建一个
switch(e.KeyCode)
语句并计算键并创建您自己的 int。 没有简单的方法可以将Keys
转换为代表键上数字的数值。 您能得到的最接近的可能是 ASCII 等效值,但仍需要翻译。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 aKeys
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.您可以只监听
KeyPress
事件吗? 它将给出实际按下的字符。Could you just listen for the
KeyPress
event instead? It will give the character that was actually pressed.该函数将执行您想要的操作:
像这样调用它:
This function will do what you want:
Call it like so:
KeyValue 表示按下的键的字符代码。
为了获得其数值,您应该找到按下的键的字符,然后将其从字符解析为整数。
类似于 Convert.ToInt32(e.KeyCode.ToString())
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.
Something like Convert.ToInt32(e.KeyCode.ToString())
或者创建一个扩展方法并像
扩展方法所在的地方一样调用它
Or create a extension method and call it like
where the extension method is
我用了这两行简单的代码:
I used this 2 lines simple code:
这是获取可用值的另一种方法:
Carlos Ferreira 发布的替代方法。
here is another way on how to get a usable value:
Alternative to what Carlos Ferreira posted.
int i = (int)e.KeyValue;
int i = (int)e.KeyValue;