注册热键在.NET - 三/四个按键组合组合、热键、按键、NET

2023-09-02 02:05:47 作者:无趣先生

我被卡住了。

现在,我使用下面的code听热键:

  [的DllImport(user32.dll中)
    公共静态的extern BOOL RegisterHotKey(IntPtr的的HWND,
      INT ID,INT fsModifiers,INT VLC);
    [的DllImport(user32.dll中)
    公共静态的extern BOOL UnregisterHotKey(IntPtr的的HWND,INT ID);


    保护覆盖无效的WndProc(参考消息M)
    {
        如果(m.Msg == 0x0312)
        {
            //什么我需要
        }
        base.WndProc(REF米);
    }
 

和这个函数注册热键:

  Form1.RegisterHotKey(this.Handle,this.GetType()GetHash code(),0,(INT)CHR);
 

它完美的作品。 我的问题是我如何注册多个热键相同的组合,例如:

在A + B + C + D ALT + SHIFT + B CTRL + ALT + SHIFT + X

编辑:我发现(如Zooba说的)如何解密的热键被送到这里的解决方案:

 保护覆盖无效的WndProc(参考消息M)
    {
        如果(m.Msg == 0x0312)
        {
            按键键=(密钥)(((INT)m.LParam>> 16)及0xFFFF的);
            ModifierKeys修改=(ModifierKeys)((INT)m.LParam和放大器; 0xFFFF的);
            如果((调整值+++键==ALT + S))
            {
                //做任何我需要的。
            }
        }
        base.WndProc(REF米);
    }
 
有什么快捷键才能立即结束无响应程序

解决方案

我找到了答案。而不是使用 registerhotkey ,我用 KeyState ,它解决了我所有的问题。如果有人有兴趣,你可以去这里(备份上archive.org )

I got stuck.

Right now, I am using the following code to listen to hotkeys:

    [DllImport("user32.dll")]
    public static extern bool RegisterHotKey(IntPtr hWnd,
      int id, int fsModifiers, int vlc);
    [DllImport("user32.dll")]
    public static extern bool UnregisterHotKey(IntPtr hWnd, int id);


    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0312)
        {
            // whatever i need
        }
        base.WndProc(ref m);
    }

and this function to register hotkey:

Form1.RegisterHotKey(this.Handle, this.GetType().GetHashCode(), 0, (int)chr);

it works perfectly. my question is how do I register multiple hotkeys as the same combination, for example:

A+B+C+D ALT+SHIFT+B CTRL+ALT+SHIFT+X

edit: I found out (like Zooba said) how to "decrypt" which hotkey was sent and here's the solution:

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0312)
        {
            Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
            ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF);
            if ((modifier + "+" + key == "Alt+S"))
            {
                //do what ever I need.
            }
        }
        base.WndProc(ref m);
    }

解决方案

I found the answer. Instead of using registerhotkey, I used KeyState and it solved all my problems. If anyone is interested, you can go here (backup on archive.org)