如何将字符转换到等价System.Windows.Input.Key枚举值? [英] How to convert a character in to equivalent System.Windows.Input.Key Enum value?

查看:603
本文介绍了如何将字符转换到等价System.Windows.Input.Key枚举值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写像这样一个功能,

 公共System.Windows.Input.Key ResolveKey(字符charToResolve)
        {
            // code放在这里,能解决charToResolve
            //到关键枚举值
            //(例如用。作为字符为Key.OemPeriod)

        }
 

我知道我可以写一个巨大的开关情况相匹配的角色,但有没有其他办法? 这样做的东西是关键枚举的字符串可能不与字符匹配,以便Enum.IsDefined将无法正常工作

任何想法?

更新:这是在Windows环境

解决方案

  [的DllImport(user32.dll中)
静态外部短VkKeyScan(焦CH);

静态公共密钥ResolveKey(字符charToResolve)
{
    返回KeyInterop.KeyFromVirtualKey(VkKeyScan(charToResolve));
}  

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

解决方案

[DllImport("user32.dll")]
static extern short VkKeyScan(char ch);

static public Key ResolveKey(char charToResolve)
{
    return KeyInterop.KeyFromVirtualKey(VkKeyScan(charToResolve));
}

这篇关于如何将字符转换到等价System.Windows.Input.Key枚举值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆