private static int OnKey(int msg, NativeMethods.KBDLLHOOKSTRUCT key) { var result = 0; foreach (var notificationEntry in NotificationEntries) { // It error code is Null, have to ignore the exception // For some unknow raison, sometime GetFocuseWindows throw an exception // Mainly when the station is unlocked, or after an admin password is asked try { if (GetFocusWindow() != notificationEntry.WindowHandle || notificationEntry.KeyCode != key.vkCode) { continue; } var modifierKeys = GetModifierKeyState(); if (!ModifierKeysMatch(notificationEntry.ModifierKeys, modifierKeys)) { continue; } var wParam = new IntPtr(msg); var lParam = new HookKeyMsgData { KeyCode = key.vkCode, ModifierKeys = modifierKeys, WasBlocked = notificationEntry.Block }; if (!PostMessage(notificationEntry.WindowHandle, HookKeyMsg, wParam, lParam)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } if (notificationEntry.Block) { result = 1; } } catch (Win32Exception e) { if (e.NativeErrorCode != 0) { throw; } } } return(result); }
private static int OnKey(Int32 msg, Win32.KBDLLHOOKSTRUCT key) { var result = 0; foreach (var notificationEntry in NotificationEntries) { try { if (GetFocusWindow() == notificationEntry.WindowHandle && notificationEntry.KeyCode == key.vkCode) { var modifierKeys = GetModifierKeyState(); if (!ModifierKeysMatch(notificationEntry.ModifierKeys, modifierKeys)) { continue; } var wParam = new IntPtr(msg); var lParam = new HookKeyMsgData { KeyCode = key.vkCode, ModifierKeys = modifierKeys, WasBlocked = notificationEntry.Block, }; if (!PostMessage(notificationEntry.WindowHandle, HookKeyMsg, wParam, lParam)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } if (notificationEntry.Block) { result = 1; } } } catch { } } return(result); }
private static extern bool PostMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, HookKeyMsgData lParam);
private static int OnKey(Int32 msg, Win32.KBDLLHOOKSTRUCT key) { var result = 0; foreach (var notificationEntry in NotificationEntries) if (GetFocusWindow() == notificationEntry.WindowHandle && notificationEntry.KeyCode == key.vkCode) { var modifierKeys = GetModifierKeyState(); if (notificationEntry.ModifierKeys != 0 && (modifierKeys & notificationEntry.ModifierKeys) == 0) continue; var wParam = new IntPtr(msg); var lParam = new HookKeyMsgData { KeyCode = key.vkCode, ModifierKeys = modifierKeys, WasBlocked = notificationEntry.Block, }; if (!PostMessage(notificationEntry.WindowHandle, HookKeyMsg, wParam, lParam)) throw new Win32Exception(Marshal.GetLastWin32Error()); if (notificationEntry.Block) result = 1; } return result; }
public static extern bool PostMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, HookKeyMsgData lParam);