Redirect global hotkey C#

143 Views Asked by At

I'm using the following code to track keys being pressed in an external application.

It works, but unfortunately the external window doesn't track the hotkey anymore when the program is running.

Is there any way register the hotkey without affecting any external programs?

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

RegisterHotKey(formHandle, MYACTION_HOTKEY_ID, 0, (int)Keys.D1);

protected override void WndProc(ref Message m)
{
    if (h != null)
        h.GlobalHotkeyListener(ref m);
    base.WndProc(ref m);
}



internal void GlobalHotkeyListener(ref Message m)
{
    if (IsRunning && m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID)
    {
        Point p;
        if (GetCursorPos(out p))
        {
            IntPtr hWnd = WindowFromPoint(p);
            Table t = IsTableRegistered(hWnd);
            if (hWnd != IntPtr.Zero && t != null)
            { 
                // Do stuff to window under mouse when hotkey is pressed
            }
        }
    }
}
0

There are 0 best solutions below