简体   繁体   English

C# 热键字母不正确

[英]C# hotkeys incorrect letters

I used SetWindowHook to set a low level keyboark hook for instant global hotkeys.我使用SetWindowHook为即时全局热键设置了一个低级键盘钩子。 But when I try to use hotkeys for letters such as ';'[],/', it returns incorrect/high value letters.但是,当我尝试对诸如“;”[],/'之类的字母使用热键时,它会返回不正确/高值的字母。 Like when I press comma, it gives me a 1/4th sign.就像当我按下逗号时,它给了我一个 1/4 号。

Here is the callback这是回调

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
    char letter;

    if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
    {
        int vkCode = Marshal.ReadInt32(lParam);

        letter = (char)vkCode;

        // converts letters to capitals
        if (char.IsLetter(letter) == true)
        {
            if ((((ushort)GetKeyState(0x14)) & 0xffff) != 0)
            {
                letter = char.ToUpper(letter);

                if (GetAsyncKeyState(((int)VirtualKeys.Shift)) != 0)
                letter = char.ToLower(letter);
            }
            else if (GetAsyncKeyState(((int)VirtualKeys.Shift)) != 0)
            {
                letter = char.ToUpper(letter);
            }
            else
            {
                letter = char.ToLower(letter);
            }
        }

        logs.Add(letter);
    }

    return CallNextHookEx(_hookID, nCode, wParam, lParam);
}

How can I get punctuation hotkeys without manually comparing every single wrong value?如何在不手动比较每个错误值的情况下获得标点符号热键?

The first problem is that you're using a keyboard hook to get hotkeys when there is a perfectly fine RegisterHotkey function.第一个问题是,当有一个完美的RegisterHotkey function 时,您正在使用键盘挂钩来获取热键。

Then there is the misunderstanding that a key and a character are the same thing.然后就有了钥匙和字符是一回事的误解。 Hotkeys are based on virtual keys, check the Keys enum for the virtual key values in C#.热键基于虚拟键,请检查Keys枚举以获取 C# 中的虚拟键值。 There is no 1 to 1 mapping between keys and characters.键和字符之间没有一对一的映射。 Many keyboard layouts don't have [ key.许多键盘布局没有[键。 For example on a German keyboard [ is altgr + 8例如在德语键盘上[altgr + 8

You need to read the scanCode rather than the vkCode from the KBDLLHOOKSTRUCT structure pointed to by lParam .您需要从lParam指向的KBDLLHOOKSTRUCT结构中读取scanCode而不是vkCode

You need to create a managed struct equivalent to KBDLLHOOKSTRUCT , then change your callback to take a ref copy of the struct.您需要创建一个等效于KBDLLHOOKSTRUCT的托管struct ,然后更改您的回调以获取该结构的ref副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM