private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0) { int vkCode = Marshal.ReadInt32(lParam); // keep track of pressed keys so we don't intercept hotkeys if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN) { if (!keysPressed.Contains(vkCode)) { keysPressed.Add(vkCode); } } // act only when key is raised if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP) { // if more than one key was pressed before a key was raised, user attempted hotkey if (keysPressed.Count == 1 && OnKeyPressed != null) { var kpa = new KeyPressedArgs(KeyInterop.KeyFromVirtualKey(vkCode)); OnKeyPressed(this, kpa); if (kpa.Handled) { return(new IntPtr(1)); } } // reset pressed keys keysPressed.Clear(); } } return(CallNextHookEx(_hookID, nCode, wParam, lParam)); }