/// <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> /// Configure the key combinations /// </summary> /// <param name="keyCombination">IEnumerable of VirtualKeyCode</param> public void Configure(IEnumerable <VirtualKeyCode> keyCombination) { TriggerCombination = keyCombination.Distinct().ToArray(); AvailableKeys = new bool[TriggerCombination.Length]; OtherPressedKeys.Clear(); }