Esempio n. 1
0
        private IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam)
        {
            bool continues = true;

            if (nCode >= 0)
            {
                if (wParam.ToUInt32() == (int)KeyEvent.WM_KEYDOWN ||
                    wParam.ToUInt32() == (int)KeyEvent.WM_KEYUP ||
                    wParam.ToUInt32() == (int)KeyEvent.WM_SYSKEYDOWN ||
                    wParam.ToUInt32() == (int)KeyEvent.WM_SYSKEYUP)
                {
                    if (hookedKeyboardCallback != null)
                    {
                        continues = hookedKeyboardCallback((KeyEvent)wParam.ToUInt32(), Marshal.ReadInt32(lParam), CheckModifiers());
                    }
                }
            }

            if (continues)
            {
                return(InterceptKeys.CallNextHookEx(hookId, nCode, wParam, lParam));
            }
            return((IntPtr)1);
        }
Esempio n. 2
0
        private IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            const int pressed     = 0; // HC_ACTION
            IntPtr    intercepted = (IntPtr)1;

            if (nCode == pressed)
            {
                var m    = (WindowsMessage)wParam.ToInt32();
                var info = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
                if (m == WindowsMessage.KEYDOWN || m == WindowsMessage.SYSKEYDOWN)
                {
                    var pressedHotkey = new HotkeyModel(info.vkCode);
                    if (pressedHotkey.Key != Key.None &&
                        pressedHotkey.ModifierKeys.Length != 0)
                    {
                        if (!Capturing)
                        {
                            if (_hotkeys.ContainsKey(pressedHotkey))
                            {
                                _modifierPressed = true;
                                var action = _hotkeys[pressedHotkey];
                                action();
                                return(intercepted);
                            }
                            else
                            {
                                return(InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam));
                            }
                        }
                        else if (HotkeyCaptured != null)
                        {
                            _modifierPressed = true;
                            var args = new HotkeyCapturedEventArgs
                            {
                                Hotkey    = pressedHotkey,
                                Available = !_hotkeys.ContainsKey(pressedHotkey),
                            };
                            HotkeyCaptured.Invoke(this, args);
                            return(intercepted);
                        }
                        else
                        {
                            return(InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam));
                        }
                    }
                    else
                    {
                        return(InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam));
                    }
                }
                else if (m == WindowsMessage.KEYUP)
                {
                    // if win+r is pressed, windows star menu will still popup
                    // so we need to discard keyup event
                    if (_modifierPressed)
                    {
                        _modifierPressed = false;
                        if (info.vkCode == Key.LWIN || info.vkCode == Key.RWIN || info.vkCode == Key.WIN)
                        {
                            return(intercepted);
                        }
                        else
                        {
                            return(InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam));
                        }
                    }
                    else
                    {
                        return(InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam));
                    }
                }
                else
                {
                    return(InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam));
                }
            }
            else
            {
                return(InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam));
            }
        }