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)
            {
                // 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)
                    {
                        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);
        }
Example #3
0
        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);
        }