如何将字符转换为等效的 System.Windows.Input.Key Enum 值?
我想写一个像这样的函数,
public System.Windows.Input.Key ResolveKey(char charToResolve)
{
// Code goes here, that resolves the charToResolve
// in to the Key enumerated value
// (For example with '.' as the character for Key.OemPeriod)
}
我知道我可以写一个巨大的 Switch-case 来匹配字符,但是还有其他方法吗? 问题是 Key 枚举的字符串可能与字符不匹配,因此 Enum.IsDefined 将不起作用
有什么想法吗?
更新:这是在Windows环境下
I want to write a function like so,
public System.Windows.Input.Key ResolveKey(char charToResolve)
{
// Code goes here, that resolves the charToResolve
// in to the Key enumerated value
// (For example with '.' as the character for Key.OemPeriod)
}
I know I can write a huge Switch-case to match the character, but is there any other way?
The thing with this is the Key enum's string may not match with the character so Enum.IsDefined will not work
Any ideas?
Update: This is in Windows environment
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用 System.Windows.Input.KeyConverter 类的 ConvertFrom 方法。
Try using the ConvertFrom method of the System.Windows.Input.KeyConverter class.
最近,我发现了一个很好的来自 Jon Hanna 的类似问题的答案,它也可以处理控制键状态:
阅读参考答案的完整说明
Recently I found a great answer for similar question from Jon Hanna which can handle control key states as well:
Read full descriptions from referenced answer
只需这样转换:
在我的例子中,我按键盘上的“ENTER”,
O
将进入ENTER {13}
,ChValue 将进入字符代码13
例如,对于
TAB
键,我将以这种方式接收字符代码9
。Just convert it this way:
In my case, I press "ENTER" on my keyboard,
O
is going intoENTER {13}
and ChValue is going into Character Code13
For
TAB
key, I will receive Character Code9
that way, for example.