0

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!

ShiiikK
  • 13
  • 6
  • does it work for a bit and then stop working after running the application? I've had issues in the past where you lose the reference to the handler that's triggering the events from the keypresses? It's a long shot but could it be a similar issue in regards to keeping the reference to the handler active? – Joe_DM Jul 15 '17 at 04:51
  • doesnt work for a bit. When I run it then press the registered key nothing happen. – Haime Reyes Jul 15 '17 at 05:01
  • @Joe_DM hey men I just tried to create a new WinForm and just write the codes for global hotkey and it WORKED! How can I fix it you think? – Haime Reyes Jul 15 '17 at 05:17
  • no one helping me how sad. – Haime Reyes Jul 15 '17 at 05:40
  • It's only been an hour and there's not a lot of information to go off since you say it's working in a fresh application. If the above code is working then it must be something else? – Joe_DM Jul 15 '17 at 05:51
  • You think theres a possibility that one of the toolbox is blocking the trigger? Is it possible in some case? – Haime Reyes Jul 15 '17 at 05:59
  • I just cloned down the old repo where I did something similar a few years back to have a look and it seems that my solution at the time was to hook into the handler of the main for that was pretty much always open. I had issues where I'd minimise the form into the tray on the bottom right of the windows taskbar. When the form was hidden for too long I'd lose the handle to it and the hotkeys would stop. I don't know that your issue is the same. Do they work at all even for a little bit? What handler are you registering the hotkey to and is that handler always available? – Joe_DM Jul 15 '17 at 06:03
  • on your form start, what happens if you replace "thisWindow = FindWindow(null, "Form1"); _hotKeys = new HotKeys(thisWindow);" with " _hotKeys = new HotKeys(this.Handle);" – Joe_DM Jul 15 '17 at 06:08
  • @Joe_DM damn it worked men! How is that? Can you explain a little so I can learn something? Thanks so much – Haime Reyes Jul 15 '17 at 06:11
  • thisWindow = FindWindow(null, "Form1") will go through and finds the handle to an instance of a any form called "Form1" but this doesn't mean it's actually the correct instance that you're working with. e.g. you could have five instances of Form1 open. if you want to get access to the instance that you're currently inside of you can just call `this` instead. I don't know exactly what broke it, but I suspect it was finding the wrong instance and therefore using the wrong handle. It was half a lucky guess but that was my logic anyway. – Joe_DM Jul 15 '17 at 06:14
  • Thanks men you helped me out. Thanks a lot. – Haime Reyes Jul 15 '17 at 06:17
  • My answer probably isn't the most detailed in the world but it's my understanding of how the issue was solved. If you're happy with it, can you please select it as accepted answer :) – Joe_DM Jul 15 '17 at 06:24

1 Answers1

1

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.

Joe_DM
  • 985
  • 1
  • 5
  • 12