如何将字符转换为等效的System.Windows.Input.Key枚举值?

Vin*_*Vin 16 .net c# enums key input

我想写一个这样的函数,

        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)

        }
Run Code Online (Sandbox Code Playgroud)

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

有任何想法吗?

更新:这是在Windows环境中

xcu*_*cud 25

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

static public Key ResolveKey(char charToResolve)
{
    return KeyInterop.KeyFromVirtualKey(VkKeyScan(charToResolve));
}
Run Code Online (Sandbox Code Playgroud)


Joa*_*son 7

尝试使用System.Windows.Input.KeyConverter类的ConvertFrom方法.