0

I'm writing a program to move the cursor with just the arraw keys. The idea is to press the ctrl button and then the arrow keys to move the cursor. I have alreday done the moving bit and figured out how to get a smooth and humen like action. I just have a problem when it comes to the global hotkeys...

  [DllImport("user32.dll")]
    public static extern IntPtr FindWindow(String sClassName, String sAppName);

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

I have set the DllImport in the global araea.

IntPtr thisWindow = FindWindow(null, "Form1");
        RegisterHotKey(thisWindow, 1, (uint)fsModifiers.Control, (uint)Keys.W);

Later in the Form1_Load I find the window and then later sets a hotkey, in this case it's Control + W (0x0002 + W)

protected override void  WndProc(ref Message keyPressed)
    {
        if (keyPressed.Msg == 0x0312)
        {

        }
        base.WndProc(ref keyPressed);
    }

Last I have this method which will detect if the hotkey was pressed, but when I press the hotkey, nothing happens. I have tried to set a brekpoint in (if (keyPressed.Msg == 0x0312)), but the breakpoint dose not go off. Why could this be?

  • 1
    There is a dedicated keyboard mouse feature in Windows. It has been around since Windows XP at least. Why are you trying to reinvent that wheel? – Christopher Apr 16 '20 at 14:16
  • 1
    FindWindow() is unreliable and might find the wrong window, use `this.Handle` instead. The handle value might change due to assignments to the Form properties, ShowInTaskbar is a typical troublemaker for example. You need to put the RegisterHotKey() call in an event handler for the `HandleCreated` event. Also the way to debug this, if the event fires more than once then you know that was the problem. – Hans Passant Apr 16 '20 at 14:18

0 Answers0