private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { AltCheckBox.Checked = false; CrtlCheckBox.Checked = false; ShiftCheckBox.Checked = false; KeyActionRadioBtn.Checked = true; TextActionRadioBtn.Checked = false; MouseActionRadioBtn.Checked = false; KeyActionPanel.Visible = true; TextActionPanel.Visible = false; MouseActionPanel.Visible = false; ActionComboBox.ResetText(); MouseActionComboBox.ResetText(); currentRowIndex = e.RowIndex; currentColumnIndex = e.ColumnIndex; try { if (currentRowIndex >= 0 && currentColumnIndex >= 0) { ShowConfigPanel(true); var firstKey = new KeyboardKey(Globals.NumericVirtualKeyCodes.ElementAt(currentRowIndex), KeyCombinationPosition.First); var SecondKey = new KeyboardKey(Globals.NumericVirtualKeyCodes.ElementAt(currentColumnIndex - 1), KeyCombinationPosition.Second); currentCombination = new TwoKeysCombination(); currentCombination.Keys = new[] { firstKey, SecondKey }; currentCombination.logger = logger; } } catch (ArgumentOutOfRangeException) { logger.Warn("Row cell clicked"); } }
public void RegisterTool(IKeyCombination keyCombination, ITool tool) { this.keyCombinationSubjects.Add(KeyCombinationSubject.Builder(keyCombination) .OnActivate(() => this.SelectedTool = tool) .Build()); this.tools.Add(tool); }
public KeyCombinationSubject(IKeyCombination keyCombination, Action onActivate, Action onContinuous, Action onDeactivate) { this.keyCombination = keyCombination; this.onActivate = onActivate; this.onContinuous = onContinuous; this.onDeactivate = onDeactivate; this.active = false; }
/// <summary> /// Determines whether [is empty combination]. /// </summary> /// <param name="combination">The combination.</param> /// <returns> /// <c>true</c> if [is empty combination] [the specified combination]; otherwise, <c>false</c>. /// </returns> /// <exception cref="ArgumentNullException">combination</exception> public static bool IsEmptyCombination(this IKeyCombination combination) { if (combination == null) { throw new ArgumentNullException(nameof(combination)); } return(combination.Keys.All(x => x == null)); }
/// <summary> /// Determines whether the source combination has specified keys. /// </summary> /// <param name="sourceCombination">The source combination.</param> /// <param name="firstKey">string representation of first key.</param> /// <param name="secondKey">string representation of second key.</param> /// <param name="thirdKey">string representation of third key (optional).</param> /// <returns> /// <c>true</c> if the source combination has specified keys; otherwise, <c>false</c>. /// </returns> public static bool HasKeys(this IKeyCombination sourceCombination, string firstKey, string secondKey, string thirdKey = default) { if (thirdKey != default) { return(((ThreeKeysCombination)sourceCombination).FirstKeyVirtualCode == firstKey && ((ThreeKeysCombination)sourceCombination).SecondKeyVirtualCode == secondKey && ((ThreeKeysCombination)sourceCombination).ThirdKeyVirtualCode == thirdKey); } return(((TwoKeysCombination)sourceCombination).FirstKeyVirtualCode == firstKey && ((TwoKeysCombination)sourceCombination).SecondKeyVirtualCode == secondKey); }
public ConfigEditor(ILog log, MainWindow form) { logger = log; mainWindow = form; resources = new ComponentResourceManager(typeof(ConfigEditor)); aliasResources = new ResXResourceSet(ConfigurationManager.AppSettings["KeyAliasesResxFileName"]); newCombinations = new List <IKeyCombination>(); actionItems = ActionComboBoxKeys(); mouseActionItems = MouseActionsComboBoxItems(); currentCombination = new TwoKeysCombination(); combinationsConfiguration = new KeyCombinationsConfiguration(); combinationsConfiguration.CombinationSize = 2; InitializeComponent(); }
public static string ToString(this IKeyCombination combination) { if (combination == null) { throw new ArgumentNullException(nameof(combination)); } if (combination.Keys == null) { throw new ArgumentNullException(nameof(combination.Keys)); } return(string.Join(" ", combination.Keys.Select(x => x.KeyCode.ToString()))); }
private static void WriteCombinationActionNode(JsonWriter writer, JsonSerializer serializer, IKeyCombination combination) { writer.WritePropertyName(nameof(combination.Action)); writer.WriteStartObject(); writer.WritePropertyName(nameof(combination.Action.Type)); serializer.Serialize(writer, combination.Action.Type.ToString()); writer.WritePropertyName("OutputVirtualKeys"); writer.WriteStartArray(); serializer.Converters.Add(new OutputKeysConverter()); serializer.PreserveReferencesHandling = PreserveReferencesHandling.None; switch (combination.Action.Type) { case ActionType.Keyboard: if (combination.Action.IsActionWithLeftArrowAdded() || combination.Action.IsDotOrSemiColonAction() || combination.Action.IsBackslashAction() || combination.Action.IsLeftAltAction() || combination.Action.IsRightAltAction() || combination.Action.IsQuestionMarkAction() || combination.Action.IsUnderscoreAction() || combination.Action.IsApostropheAction() || combination.Action.IsEqualitySignAction() || combination.Action.IsActionWithPolishSigns()) { serializer.Serialize(writer, string.Join("", combination.Action.ActionStringKeys)); } else { serializer.Serialize(writer, combination.Action.VirtualKeys); } break; case ActionType.Mouse: serializer.Serialize(writer, combination.Action.ActionStringKeys[0]); break; default: throw new Exception("Could not serialize action, uknow action type"); } writer.WriteEndArray(); writer.WriteEndObject(); }
public ConfigEditor(ILog log, MainWindow form, KeyCombinationsConfiguration configuration, string configFileName) { logger = log; mainWindow = form; resources = new ComponentResourceManager(typeof(ConfigEditor)); aliasResources = Globals.AliasResources; combinationsConfiguration = configuration; actionItems = ActionComboBoxKeys(); mouseActionItems = MouseActionsComboBoxItems(); currentCombination = new TwoKeysCombination(); newCombinations = configuration.Combinations.ToList(); InitializeComponent(); this.ConfigNameTxtBox.Text = configFileName.Split('.')[0]; newConfigName = this.ConfigNameTxtBox.Text; }
/// <summary> /// Determines whether [is not empty action combination]. /// </summary> /// <param name="combination">The combination.</param> /// <returns> /// <c>true</c> if [is not empty action combination] [the specified combination]; otherwise, <c>false</c>. /// </returns> /// <exception cref="ArgumentNullException"> /// combination /// or /// Action /// </exception> public static bool IsNotEmptyActionCombination(this IKeyCombination combination, ILog logger) { if (combination == null) { logger.Warn("Combination is null"); return(false); } if (combination.Action == null) { logger.Warn("Action is null"); return(false); } return(combination.Action.VirtualKeys.Any()); }
/// <summary> /// Makes a copy of source combination. /// </summary> /// <param name="sourceCombination">The source combination.</param> /// <param name="destinationCombination">The destination combination.</param> /// <returns></returns> /// <exception cref="ArgumentNullException"> /// sourceCombination /// or /// destinationCombination /// </exception> public static IKeyCombination CopyTo(this IKeyCombination sourceCombination, IKeyCombination destinationCombination) { if (sourceCombination == null) { throw new ArgumentNullException(nameof(sourceCombination)); } if (destinationCombination == null) { throw new ArgumentNullException(nameof(destinationCombination)); } destinationCombination.Action = sourceCombination.Action; destinationCombination.Keys = sourceCombination.Keys; return(destinationCombination); }
/// <summary> /// Initializes a new instance of the <see cref="KeyboardHook" /> class. /// </summary> /// <param name="log"></param> /// <param name="combinationsConfiguration">The combinations configuration.</param> /// <param name="effects">The sounds effects.</param> /// <param name="helpWindow">Gui helper window</param> public KeyboardHook(ILog log, KeyCombinationsConfiguration combinationsConfiguration, KeysSoundEffects effects, HelpWindow helpWindow = null) { logger = log; hookInstance = LoadPInvokeKernel32Library("User32"); baseConfiguration = combinationsConfiguration; currentCombination = baseConfiguration.GetCombinationInstance(logger); combinationSize = baseConfiguration.CombinationSize; configCombinations = baseConfiguration.Combinations; lastUsedCombination = baseConfiguration.GetCombinationInstance(logger); combinationsKeysVirtualCodes = configCombinations.ToVirtualKeyCodes().ToList(); numericKeypadKeysVirtualCodes = Globals.NumericKeypadVirtualKeyCodes.ToList(); numericKeypadWithShiftKeyVirtualCodes = Globals.NumericKeypadWithShiftVirtualKeyCodes.ToList(); numericKeysVirtualCodes = Globals.NumericVirtualKeyCodes.ToList(); modKeysVirtualCodes = Globals.ModKeysVirtualKeyCodes.ToList(); KeysStateChecker = new WindowsInputDeviceStateAdaptor(); inputSimulator = new InputSimulator(); soundEffects = effects; helperWindow = helpWindow; }
public static KeyCombinationSubjectBuilder Builder(IKeyCombination keyCombination) { return(new KeyCombinationSubjectBuilder(keyCombination)); }
public KeyCombinationSubjectBuilder(IKeyCombination keyCombination) { this.keyCombination = keyCombination; }
public IntPtr ConfigHook(int code, int wParam, ref KeyboardHookStructure.HookStruct lParam) { var wp = wParam; var vk = lParam.vkCode; var sk = lParam.scanCode; if (code < 0) { //you need to call CallNextHookEx without further processing //and return the value returned by CallNextHookEx return(ApiFunctions.CallNextHookEx(hookInstance, code, wp, ref lParam)); } if (inputSimulator.InputDeviceState.IsKeyDown(VirtualKeyCode.SHIFT)) { logger.Debug($"vk={vk:X}"); logger.Debug($"sk={sk:X}"); } if (vk == (int)VirtualKeyCode.LSHIFT && sk == 554) { ResetPositionCounter(); } if (numericKeysVirtualCodes.Contains((VirtualKeyCode)vk) || sk == 0) { ResetPositionCounter(); return(ApiFunctions.CallNextHookEx(hookInstance, code, wp, ref lParam)); } ConcatenatePosition(); if (combinationsKeysVirtualCodes.Contains((VirtualKeyCode)vk) || numericKeypadKeysVirtualCodes.Contains((VirtualKeyCode)vk) || modKeysVirtualCodes.Contains((VirtualKeyCode)vk)) { if (wp == keyboardKeyDownCode) { KeyboardKey key; if (!modKeysVirtualCodes.Contains((VirtualKeyCode)vk)) { key = new KeyboardKey((VirtualKeyCode)vk, (KeyCombinationPosition)currentCombination.Keys.Count(x => x != null) + 1); } else { if ((VirtualKeyCode)vk == VirtualKeyCode.LSHIFT || (VirtualKeyCode)vk == VirtualKeyCode.RSHIFT) { vk = (int)VirtualKeyCode.SHIFT; } if ((VirtualKeyCode)vk == VirtualKeyCode.LCONTROL || (VirtualKeyCode)vk == VirtualKeyCode.RCONTROL) { vk = (int)VirtualKeyCode.CONTROL; } var temp = configCombinations.Where(x => x.Action.IsModKeyAction()) .FirstOrDefault(x => x.Action.IsThisVirtualCode((VirtualKeyCode)vk)); if (temp != null) { DebugKey(wParam, vk, sk); if (Globals.ModKeysVirtualKeyCodes.Contains((VirtualKeyCode)vk) && Globals.KeyCombinationPositionCounter >= 2) { ResetPositionCounter(); ConcatenatePosition(); } if (Globals.ModKeysVirtualKeyCodes.Contains(temp.Keys[Globals.KeyCombinationPositionCounter].KeyCode)) { currentCombination?.Clear(); return(ApiFunctions.CallNextHookEx(hookInstance, code, wp, ref lParam)); } key = (Globals.IsMouseButtonWithModKey) ? new KeyboardKey(temp.Keys[Globals.KeyCombinationPositionCounter].KeyCode, (KeyCombinationPosition)currentCombination.Keys.Count(x => x != null) + 1) : new KeyboardKey(temp.Keys[Globals.KeyCombinationPositionCounter].KeyCode, (KeyCombinationPosition)currentCombination.Keys.Count(x => x != null)); } else { throw new Exception("Unable to set a key"); } } if (key.CombinationPosition == KeyCombinationPosition.None) { return(ApiFunctions.CallNextHookEx(hookInstance, code, wp, ref lParam)); } currentCombination.SetKeyByPosition(key.CombinationPosition, key.KeyCode); if (!currentCombination.IsFullCombination()) { if (Globals.IsSoundOn) { try { switch (key.CombinationPosition) { case KeyCombinationPosition.First: soundEffects.PlaySound(SoundAction.FirstKey); Globals.FirstKeyInCombination = key.ToString(); if (Globals.IsHelpWindowOn) { var possibleOutputActions = configCombinations .Where(x => x.Keys[0].KeyCode .ConvertNumericKeyCodeToNumericKeypadKeyCode() == key.KeyCode) .Select(x => x.Action.ToString()) .ToList(); helperWindow.FillHelperRow(possibleOutputActions); helperWindow.TopMost = true; helperWindow.Show(); } break; case KeyCombinationPosition.Second: soundEffects.PlaySound(SoundAction.SecondKey); break; case KeyCombinationPosition.Third: soundEffects.PlaySound(SoundAction.ThirdKey); break; } } catch (NullReferenceException ne) { logger.Warn(ne.Message); return(ApiFunctions.CallNextHookEx(hookInstance, -1, wp, ref lParam)); } catch (Exception ex) { logger.Error(ex.Message); logger.Error(ex.StackTrace); } } } else { switch (combinationSize) { case 2: { var combinations = configCombinations.Copy(); currentCombination = combinations.FirstOrDefault(x => x.Equals(currentCombination)); if (currentCombination == null) { logger.Warn("No such combination"); currentCombination = baseConfiguration.GetCombinationInstance(logger); Globals.ResetKeysFlags(); ResetPositionCounter(); PlayLastKeySound(key); helperWindow.ClearHelperRow(); helperWindow.Hide(); return((IntPtr)1); } if (!currentCombination.IsNotEmptyActionCombination(logger)) { if (lastUsedCombination.Action == null) { currentCombination.CopyTo(lastUsedCombination); } if (lastUsedCombination.IsFullCombination()) { Globals.ResetModKeysPressedOnceFlags(); } } try { if (!currentCombination.Action.IsModKeyAction()) { PlayLastKeySound(key); } else { currentCombination.CopyTo(lastUsedCombination); if (currentCombination.Action.IsShiftAction()) { if (Globals.IsSoundOn) { soundEffects.PlaySound(SoundAction.Shift); } if (!Globals.IsShiftPressedOnce && !Globals.IsShiftHoldDown) { Globals.ShiftPressCounter = 1; Globals.IsShiftPressedOnce = true; } else if (!Globals.IsShiftHoldDown || Globals.ShiftPressCounter >= 1) { Globals.ShiftPressCounter++; Globals.IsShiftHoldDown = true; Globals.IsShiftPressedOnce = false; } if (Globals.ShiftPressCounter > 2) { Globals.IsShiftHoldDown = false; Globals.IsShiftPressedOnce = false; } } else if (currentCombination.Action.IsLeftAltAction()) { if (Globals.IsSoundOn) { soundEffects.PlaySound(SoundAction.Alt); } if (!Globals.IsLeftAltPressedOnce && !Globals.IsLeftAltHoldDown) { Globals.LeftAltPressCounter = 1; Globals.IsLeftAltPressedOnce = true; } else if (!Globals.IsLeftAltHoldDown || Globals.LeftAltPressCounter >= 1) { Globals.LeftAltPressCounter++; Globals.IsLeftAltHoldDown = true; Globals.IsLeftAltPressedOnce = false; } if (Globals.LeftAltPressCounter > 2) { Globals.IsLeftAltPressedOnce = false; Globals.IsLeftAltHoldDown = false; } } else if (currentCombination.Action.IsRightAltAction()) { if (Globals.IsSoundOn) { soundEffects.PlaySound(SoundAction.Alt); } if (!Globals.IsRightAltPressedOnce && !Globals.IsRightAltHoldDown) { Globals.RightAltPressCounter = 1; Globals.IsRightAltPressedOnce = true; } else if (!Globals.IsRightAltHoldDown || Globals.RightAltPressCounter >= 1) { Globals.RightAltPressCounter++; Globals.IsRightAltHoldDown = true; Globals.IsRightAltPressedOnce = false; } if (Globals.RightAltPressCounter > 2) { Globals.IsRightAltPressedOnce = false; Globals.IsRightAltHoldDown = false; } } else if (currentCombination.Action.IsCrtlAction()) { if (Globals.IsSoundOn) { soundEffects.PlaySound(SoundAction.Crtl); } if (!Globals.IsCtrlPressedOnce && !Globals.IsCtrlHoldDown) { Globals.CtrlPressCounter = 1; Globals.IsCtrlPressedOnce = true; } else if (!Globals.IsCtrlHoldDown || Globals.CtrlPressCounter >= 1) { Globals.CtrlPressCounter++; Globals.IsCtrlHoldDown = true; Globals.IsCtrlPressedOnce = false; } if (Globals.CtrlPressCounter > 2) { Globals.IsCtrlHoldDown = false; Globals.IsCtrlPressedOnce = false; } } else if (currentCombination.Action.IsLeftWinAction()) { if (Globals.IsSoundOn) { soundEffects.PlaySound(SoundAction.Win); } if (!Globals.IsLeftWinPressedOnce && !Globals.IsLeftWinHoldDown) { Globals.LeftWinPressCounter = 1; Globals.IsLeftWinPressedOnce = true; } else if (!Globals.IsLeftWinHoldDown || Globals.LeftWinPressCounter >= 1) { Globals.LeftWinPressCounter++; Globals.IsLeftWinHoldDown = true; Globals.IsLeftWinPressedOnce = false; } if (Globals.LeftWinPressCounter > 2) { Globals.IsLeftWinHoldDown = false; Globals.IsLeftWinPressedOnce = false; } } else if (currentCombination.Action.IsRightWinAction()) { if (Globals.IsSoundOn) { soundEffects.PlaySound(SoundAction.Win); } if (!Globals.IsRightWinPressedOnce && !Globals.IsRightWinHoldDown) { Globals.RightWinPressCounter = 1; Globals.IsRightWinPressedOnce = true; } else if (!Globals.IsRightWinHoldDown || Globals.RightWinPressCounter >= 1) { Globals.RightWinPressCounter++; Globals.IsRightWinHoldDown = true; Globals.IsRightWinPressedOnce = false; } if (Globals.RightWinPressCounter > 2) { Globals.IsRightWinHoldDown = false; Globals.IsRightWinPressedOnce = false; } } } var combinationAction = currentCombination.Action; if (combinationAction == null) { currentCombination?.Clear(); logger.Warn("No action defined"); } else { var onlyModKeyAction = combinationAction.Run(); if (!onlyModKeyAction) { Globals.ResetModKeysPressedOnceFlags(); } currentCombination.Clear(); helperWindow?.ClearHelperRow(); helperWindow?.Hide(); configCombinations = baseConfiguration.Combinations; } break; } catch (Exception e) { logger.Error(e.Message); return(ApiFunctions.CallNextHookEx(hookInstance, -1, wp, ref lParam)); } } case 3: // TODO break; } } ResetPositionCounter(); return((IntPtr)1); } if (wp == keyboardKeyUpCode) { if (Globals.NumericKeypadWithShiftVirtualKeyCodes.Contains((VirtualKeyCode)vk)) { if (Globals.KeyCombinationPositionCounter >= 2) { currentCombination?.Clear(); } ResetPositionCounter(); return((IntPtr)1); } } ResetPositionCounter(); return(ApiFunctions.CallNextHookEx(hookInstance, -1, wp, ref lParam)); } ResetPositionCounter(); return(ApiFunctions.CallNextHookEx(hookInstance, code, wp, ref lParam)); }
public Tools(Func <IToolApplier> toolApplierProvider, IMousePositionProvider mousePositionProvider, IKeyCombination escape, IKeyCombination activator) { Debug.Assert(toolApplierProvider != null); Debug.Assert(mousePositionProvider != null); this.toolApplierProvider = toolApplierProvider; this.mousePositionProvider = mousePositionProvider; this.keyCombinationSubjects.Add(KeyCombinationSubject.Builder(escape) .OnActivate(() => this.SelectedTool = null) .Build()); this.keyCombinationSubjects.Add(KeyCombinationSubject.Builder(activator) .OnActivate(() => this.ActiveTool = this.SelectedTool) .OnContinuous(() => this.ContinueTool()) .OnDeactivate(() => this.EnactTool()) .Build()); }