Skip to main content
 首页 » 编程设计

c# ascii 到键码

2024年02月06日24shihaiming

[DllImport("user32.dll")] 
private static extern short VkKeyScan(char ch); 
 
set = set + "\r\n"; 
int txL = set.Length; 
for (int o = 1; o < (txL); o++) 
{ 
    short vkey = VkKeyScan(set[o]); //MessageBox.Show(vkey.ToString()); 
    Keys retval = (Keys)(vkey & 0xff); 
    int modifiers = vkey >> 8; 
    if ((modifiers & 1) != 0) retval |= Keys.Shift;  
    if ((modifiers & 2) != 0) retval |= Keys.Control; 
    if ((modifiers & 4) != 0) retval |= Keys.Alt; 
} 

此函数将标准字母表和键盘布局转换为 KeyCode,如何将所有其他 Unicode 转换为中文以及 ASCII 中的符号?它也不适用于 shift 键:“!,@,#,$ ...” 那么,如何将 ALT 和其他 Key 组合起来,然后转换为 KeyCode 呢?例如 ALT+33 = "!"和 KeyCode = 0xF0

请您参考如下方法:

有许多 Unicode 字符没有对应的键。例如,我有一个 105 键的键盘,同时有大约一百万个不同的 Unicode 字符。键码字符映射也取决于键盘布局。因此,您的问题没有通用的答案。