我正在使用以下代码来跟踪在外部应用程序中按下的键。
它可以工作,但不幸的是,当程序运行时,外部窗口不再跟踪热键。
有没有办法注册热键而不影响任何外部程序?
[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
}
}
}
}