如何将字符转换为等效的 System.Windows.Input.Key Enum 值? [英] How to convert a character in to equivalent System.Windows.Input.Key Enum value?

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

问题描述

我想写一个这样的函数,

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)

        }

我知道我可以写一个巨大的 Switch-case 来匹配角色,但还有其他方法吗?与此相关的是 Key 枚举的字符串可能与字符不匹配,因此 Enum.IsDefined 将不起作用

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

有什么想法吗?

更新:这是在 Windows 环境中

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 Enum 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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