Example #1
0
        private static IntPtr LowLevelKeyboardProc(Int32 nCode, IntPtr wParam, Win32.KBDLLHOOKSTRUCT lParam)
        {
            var wParamInt = wParam.ToInt32();
            var result    = 0;

            if (nCode == Win32.HC_ACTION)
            {
                switch (wParamInt)
                {
                case Win32.WM_KEYDOWN:
                case Win32.WM_SYSKEYDOWN:
                case Win32.WM_KEYUP:
                case Win32.WM_SYSKEYUP:
                    result = OnKey(wParamInt, lParam);
                    break;
                }
            }

            if (result != 0)
            {
                return(new IntPtr(result));
            }

            return(Win32.CallNextHookEx(_hook, nCode, wParam, lParam));
        }
Example #2
0
        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);
        }