c# fix global hotkey

695 Views Asked by At

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. 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
{ 
    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). 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!

1

There are 1 best solutions below

0
On BEST ANSWER

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. (perhaps there are multiple instances in memory for 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.