/// <summary> /// Check if the combinations are pressed /// </summary> /// <param name="keyboardHookEventArgs">KeyboardHookEventArgs</param> public bool Handle(KeyboardHookEventArgs keyboardHookEventArgs) { bool handled = false; foreach (var keyboardHookEventHandler in _keyCombinations) { handled |= keyboardHookEventHandler.Handle(keyboardHookEventArgs); } return(handled); }
/// <summary> /// Handle key presses to test if the combination is available /// </summary> /// <param name="keyboardHookEventArgs">KeyboardHookEventArgs</param> public virtual bool Handle(KeyboardHookEventArgs keyboardHookEventArgs) { if (IgnoreInjected && keyboardHookEventArgs.IsInjectedByProcess) { return(false); } bool keyMatched = false; bool isRepeat = false; for (int i = 0; i < TriggerCombination.Length; i++) { if (!CompareVirtualKeyCode(keyboardHookEventArgs.Key, TriggerCombination[i])) { continue; } isRepeat = AvailableKeys[i]; AvailableKeys[i] = keyboardHookEventArgs.IsKeyDown; keyMatched = true; break; } if (!keyMatched) { if (keyboardHookEventArgs.IsKeyDown) { OtherPressedKeys.Add(keyboardHookEventArgs.Key); } else { OtherPressedKeys.Remove(keyboardHookEventArgs.Key); } } // check if this combination is handled var isHandled = keyboardHookEventArgs.IsKeyDown && OtherPressedKeys.Count == 0 && AvailableKeys.All(b => b); // Mark as handled if the key combination is handled and we don't have pass-through if (isHandled && !IsPassThrough) { keyboardHookEventArgs.Handled = true; } // Do not return a true, if this is a repeat and CanRepeat is disabled if (!CanRepeat && isRepeat) { return(false); } return(isHandled); }
/// <summary> /// Check if the combinations are pressed /// </summary> /// <param name="keyboardHookEventArgs">KeyboardHookEventArgs</param> public bool Handle(KeyboardHookEventArgs keyboardHookEventArgs) { var currentHandled = CurrentHandler.Handle(keyboardHookEventArgs); var currentNotPressed = !CurrentHandler.HasKeysPressed; if (currentHandled) { _isHandled[_offset] = true; } else if (!keyboardHookEventArgs.IsKeyDown && _offset > 0 && currentNotPressed && !keyboardHookEventArgs.IsModifier) { Reset(); } // Check if timeout passed, to reset the sequence if (_expireAfter.HasValue) { var isExpired = _expireAfter.Value < DateTimeOffset.Now; if (isExpired) { if (currentNotPressed) { Reset(); } return(false); } } var allHandled = _isHandled.All(b => b); // check if we need to advance if (_isHandled[_offset] && currentNotPressed && !AdvanceHandler()) { // Advance Reset(); } return(currentHandled && allHandled); }
/// <summary> /// Create the KeyboardHookEventArgs from the parameters which where in the event /// </summary> /// <param name="wParam">IntPtr</param> /// <param name="lParam">IntPtr</param> /// <returns>KeyboardHookEventArgs</returns> private static KeyboardHookEventArgs CreateKeyboardEventArgs(IntPtr wParam, IntPtr lParam) { var isKeyDown = wParam == (IntPtr)WmKeydown || wParam == (IntPtr)WmSyskeydown; var keyboardLowLevelHookStruct = (KeyboardLowLevelHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardLowLevelHookStruct)); bool isModifier = keyboardLowLevelHookStruct.VirtualKeyCode.IsModifier(); // Check the key to find if there any modifiers, store these in the global values. switch (keyboardLowLevelHookStruct.VirtualKeyCode) { case VirtualKeyCode.Capital: if (isKeyDown) { _capsLock = !_capsLock; } break; case VirtualKeyCode.NumLock: if (isKeyDown) { _numLock = !_numLock; } break; case VirtualKeyCode.Scroll: if (isKeyDown) { _scrollLock = !_scrollLock; } break; case VirtualKeyCode.LeftShift: _leftShift = isKeyDown; break; case VirtualKeyCode.RightShift: _rightShift = isKeyDown; break; case VirtualKeyCode.LeftControl: _leftCtrl = isKeyDown; break; case VirtualKeyCode.RightControl: _rightCtrl = isKeyDown; break; case VirtualKeyCode.LeftMenu: _leftAlt = isKeyDown; break; case VirtualKeyCode.RightMenu: _rightAlt = isKeyDown; break; case VirtualKeyCode.LeftWin: _leftWin = isKeyDown; break; case VirtualKeyCode.RightWin: _rightWin = isKeyDown; break; } var keyEventArgs = new KeyboardHookEventArgs { TimeStamp = keyboardLowLevelHookStruct.TimeStamp, Key = keyboardLowLevelHookStruct.VirtualKeyCode, Flags = keyboardLowLevelHookStruct.Flags, IsModifier = isModifier, IsKeyDown = isKeyDown, IsLeftShift = _leftShift, IsRightShift = _rightShift, IsLeftAlt = _leftAlt, IsRightAlt = _rightAlt, IsLeftControl = _leftCtrl, IsRightControl = _rightCtrl, IsLeftWindows = _leftWin, IsRightWindows = _rightWin, IsScrollLockActive = _scrollLock, IsNumLockActive = _numLock, IsCapsLockActive = _capsLock }; // Do we need this?? //http://msdn.microsoft.com/en-us/library/windows/desktop/ms646286(v=vs.85).aspx if (!keyEventArgs.IsAlt && (wParam == (IntPtr)WmSyskeydown || wParam == (IntPtr)WmSyskeyup)) { keyEventArgs.IsLeftAlt = true; keyEventArgs.IsSystemKey = true; } return(keyEventArgs); }