예제 #1
0
 public KeyboardHookEventArgs(
     LowLevelKeyboardInputEvent keyboardData,
     KeyboardState keyboardState,
     bool ctrl,
     bool alt,
     bool shift,
     bool meta)
 {
     KeyboardData  = keyboardData;
     KeyboardState = keyboardState;
     Ctrl          = ctrl;
     Alt           = alt;
     Shift         = shift;
     Meta          = meta;
 }
예제 #2
0
        private IntPtr LowLevelKeyboardProc(int nCode,
                                            IntPtr wParam,
                                            IntPtr lParam)
        {
            if (nCode < 0)
            {
                return(CallNextHookEx(_windowsHookHandle,
                                      nCode,
                                      wParam,
                                      lParam));
            }

            var wparamTyped = wParam.ToInt32();

            // TODO: Invoke KeyboardPressed

            if (Enum.IsDefined(typeof(KeyboardState),
                               wparamTyped))
            {
                KeyboardState kbState = (KeyboardState)wparamTyped;
                LowLevelKeyboardInputEvent kbEvent = (LowLevelKeyboardInputEvent)Marshal.PtrToStructure(lParam,
                                                                                                        typeof(LowLevelKeyboardInputEvent));

                if (kbState == KeyboardState.KeyDown || kbState == KeyboardState.SysKeyDown)
                {
                    var hkReg = HotKeys.SafeGet(
                        new HotKey(
                            kbEvent.Key,
                            GetCtrlPressed(), GetAltPressed(), GetShiftPressed(), GetMetaPressed())
                        );

                    if (hkReg != null)
                    {
                        bool scopeMatches = true;

                        if (hkReg.Scope != HotKeyScope.Global)
                        {
                            var foregroundWdwHandle = GetForegroundWindow();

                            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                            if (foregroundWdwHandle == null || foregroundWdwHandle == IntPtr.Zero)
                            {
                                scopeMatches = false;
                            }

                            else if (hkReg.Scope == HotKeyScope.SMBrowser && foregroundWdwHandle != _elWdwHandle)
                            {
                                scopeMatches = false;
                            }

                            else if (hkReg.Scope == HotKeyScope.SM)
                            {
                                GetWindowThreadProcessId(foregroundWdwHandle, out var foregroundProcId);

                                if (foregroundProcId != _smProcessId)
                                {
                                    scopeMatches = false;
                                }
                            }
                        }

                        if (scopeMatches)
                        {
                            TriggeredCallbacks.Enqueue(hkReg.Callback);
                            TriggeredEvent.Set();

                            return((IntPtr)1);
                        }
                    }
                }
            }

            return(CallNextHookEx(_windowsHookHandle,
                                  nCode,
                                  wParam,
                                  lParam));
        }
        private IntPtr LowLevelKeyboardProc(int nCode,
                                            IntPtr wParam,
                                            IntPtr lParam)
        {
            if (nCode < 0)
            {
                return(CallNextHookEx(_windowsHookHandle,
                                      nCode,
                                      wParam,
                                      lParam));
            }

            var wparamTyped = wParam.ToInt32();

            // TODO: Invoke KeyboardPressed

            if (Enum.IsDefined(typeof(KeyboardState),
                               wparamTyped))
            {
                KeyboardState kbState = (KeyboardState)wparamTyped;
                LowLevelKeyboardInputEvent kbEvent = (LowLevelKeyboardInputEvent)Marshal.PtrToStructure(lParam,
                                                                                                        typeof(LowLevelKeyboardInputEvent));

                if (kbState == KeyboardState.KeyDown || kbState == KeyboardState.SysKeyDown)
                {
                    var hk = new HotKey(
                        kbEvent.Key,
                        GetCtrlPressed(), GetAltPressed(), GetShiftPressed(), GetMetaPressed());
                    var hkReg = HotKeys.SafeGet(hk);

                    if (MainCallback != null && hk.Modifiers != KeyModifiers.None)
                    {
                        MainCallback(hk);
                    }

                    if (hkReg != null)
                    {
                        bool scopeMatches = true;

                        if (hkReg.Scope != HotKeyScope.Global)
                        {
                            var foregroundWdwHandle = GetForegroundWindow();

                            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                            if (foregroundWdwHandle == null || foregroundWdwHandle == IntPtr.Zero)
                            {
                                scopeMatches = false;
                            }

                            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                            else if (_elWdwHandle == null || _elWdwHandle == IntPtr.Zero)
                            {
                                LogTo.Warning(
                                    $"KeyboardHook: HotKey {hk} requested with scope {Enum.GetName(typeof(HotKeyScope), hkReg.Scope)}, but _elWdwHandle is {_elWdwHandle}. Trying to refresh.");

                                OnElementWindowAvailable();

                                // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                                if (_elWdwHandle == null || _elWdwHandle == IntPtr.Zero)
                                {
                                    scopeMatches = false;
                                }
                            }

                            else if (hkReg.Scope == HotKeyScope.SMBrowser && foregroundWdwHandle != _elWdwHandle)
                            {
                                scopeMatches = false;
                            }

                            else if (hkReg.Scope == HotKeyScope.SM)
                            {
                                GetWindowThreadProcessId(foregroundWdwHandle, out var foregroundProcId);

                                if (foregroundProcId != _smProcessId)
                                {
                                    scopeMatches = false;
                                }
                            }
                        }

                        if (scopeMatches)
                        {
                            TriggeredCallbacks.Enqueue(hkReg.Callback);
                            TriggeredEvent.Set();

                            return((IntPtr)1);
                        }
                    }
                }
            }

            return(CallNextHookEx(_windowsHookHandle,
                                  nCode,
                                  wParam,
                                  lParam));
        }