简体   繁体   English

C#修复全局热键

[英]c# fix global hotkey

I have created a global hotkey and in first it works just fine. 我创建了一个全局热键,首先它可以正常工作。 But when I start to add some designs in the Form and extra code it does not work anymore. 但是,当我开始在Form中添加一些设计并添加额外的代码时,它不再起作用。 Then I back to basics and just comment out the code added to the original code but still no luck. 然后我回到基础知识,只是注释掉添加到原始代码中的代码,但是仍然没有运气。 heres the code: 这是代码:

hotKey class code: class HotKeys { hotKey类代码:类HotKeys {

    public enum fsModifers
    {
        Alt = 0x0001,
        Control = 0x0002,
        Shift = 0x0004,
        Window = 0x0008,
    }

    IntPtr hWnds;

    public HotKeys(IntPtr hWnd)
    {
        this.hWnds = hWnd;
    }

    public void RegisterHotKeys()
    {
        RegisterHotKey(hWnds, 1, (uint)fsModifers.Control, (uint)Keys.T);
        RegisterHotKey(hWnds, 2, (uint)fsModifers.Control, (uint)Keys.R);
    }

    public void UnregisterHotKeys()
    {
        UnregisterHotKey(hWnds, 1);
        UnregisterHotKey(hWnds, 2);
    }

    #region WindowsAPI
    [DllImport("user32.dll")]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

    [DllImport("user32.dll")]
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
    #endregion
}

main form code: (the code below is just the code related to the hotkey) 主要形式的代码:(下面的代码仅仅是与热键相关的代码)

private void Form1_Load(object sender, EventArgs e)
    {
        thisWindow = FindWindow(null, "Form1");
        _hotKeys = new HotKeys(thisWindow);
        _hotKeys.RegisterHotKeys();
    }

  private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        _hotKeys.UnregisterHotKeys();
    }

 protected override void WndProc(ref Message keyPressed)
    {
        if (keyPressed.Msg == 0x0312)
        {
            MessageBox.Show("my msg");
            //keyPress = keyPressed.WParam;
            //if (keyPress == (IntPtr)1)
            //{
            //    if (!autoSkillIsOn)
            //    {
            //        timer1.Start();
            //        autoSkillIsOn = true;
            //    }

            //    else if (autoSkillIsOn)
            //    {
            //        timer1.Stop();
            //        autoSkillIsOn = false;
            //    }
            //}

            //else if (keyPress == (IntPtr)2)
            //{
            //    MessageBox.Show("pressed ctrl R");
            //}
        }

        base.WndProc(ref keyPressed);
    }

as you can see in the WndProc I commented out the things I want to happen and just simply write a simple messageBox but guess what, no messageBox appearing when I press any of the registered hot key(Ctrl+T, Ctrl+R). 正如您在WndProc中看到的那样,我注释掉了我想发生的事情,只写了一个简单的messageBox,但是猜测是什么,当我按下任何已注册的热键(Ctrl + T,Ctrl + R)时,没有messageBox出现。 Why o why this happen? 为什么会这样呢? it works just fine in the first time when the code is just all about the hotkey. 当代码仅与热键有关时,它在第一次时就可以正常工作。 Advance thanks for the help! 预先感谢您的帮助!

I'll post an answer since it seems to have been resolved during troubleshooting in the comments. 我将发布答案,因为它似乎已在注释中的疑难解答过程中得到了解决。

Op is using FindWindow(null, "Form1") to get the reference to the handle, however this was presumably locating the incorrect handle. Op正在使用FindWindow(null, "Form1")获取对句柄的引用,但是大概是在查找错误的句柄。 (perhaps there are multiple instances in memory for From1?) (也许内存中有From1的多个实例?)

By changing to use this.Handle , op is guaranteed to be registering the hot keys to the correct handle for the instance which he is calling from. 通过更改为使用this.Handle ,可以确保op将热键注册到他从中调用的实例的正确句柄。

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

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