/// <summary> /// Generates a string matching a given PressedKeysInfo /// </summary> /// <param name="pressedKeysInfo"></param> /// <returns></returns> public string GetPressedKeysAsSetting(PressedKeysInfo pressedKeysInfo) { // cannot really determine here which keys were lifted (user might lift multiple keys at once, but we might not catch every one of them, but we should know which modifier is being pressed) string pressedKey = Enum.IsDefined(typeof(ModifierKeys), (int)pressedKeysInfo.Keys) ? "None" : pressedKeysInfo.Keys.ToString(); // this settings format is to be compatible with previously used fmutils keyboard hook return(string.Format("Key={0}; Win={1}; Alt={2}; Ctrl={3}; Shift={4}", new object[] { pressedKey, pressedKeysInfo.IsWinPressed, pressedKeysInfo.IsAltPressed, pressedKeysInfo.IsCtrlPressed, pressedKeysInfo.IsShiftPressed })); }
/// <summary> /// assignes the pressedKeysInfo to hotkeyPressedStates /// </summary> /// <param name="pressedKeysInfo"></param> private void AddOrUpdateHotkeyState(PressedKeysInfo pressedKeysInfo) { if (this.hotkeyPressedStates.ContainsKey(pressedKeysInfo)) { this.hotkeyPressedStates[pressedKeysInfo] = false; } else { this.hotkeyPressedStates.Add(pressedKeysInfo, false); } }
public SimpleGlobalHotkeyServiceEventArgs(bool keyDown, PressedKeysInfo pressedKeysInfo) { this.Key = pressedKeysInfo.Keys; this.KeyDown = keyDown; this.KeyUp = !keyDown; this.IsWinPressed = pressedKeysInfo.IsWinPressed; this.IsAltPressed = pressedKeysInfo.IsAltPressed; this.IsCtrlPressed = pressedKeysInfo.IsCtrlPressed; this.IsShiftPressed = pressedKeysInfo.IsShiftPressed; }
/// <summary> /// Assigns an Action to be triggered on releasing a defined PressedKeysInfo from keyboard /// </summary> /// <param name="pressedKeysInfo"></param> /// <param name="hotkeyAction"></param> public void AddOrUpdateOnReleaseHotkey(PressedKeysInfo pressedKeysInfo, Action hotkeyAction) { AddOrUpdateHotkeyState(pressedKeysInfo); if (this.onReleaseHotkeys.ContainsKey(pressedKeysInfo)) { this.onReleaseHotkeys[pressedKeysInfo] = hotkeyAction; } else { this.onReleaseHotkeys.Add(pressedKeysInfo, hotkeyAction); } }
public SimpleGlobalHotkeyService() { //logger = NLog.LogManager.GetCurrentClassLogger(); pressedKeysInfo = PressedKeysInfo.Empty; keyboardHook = new GlobalKeyboardHook(); hotkeyPressedStates = new Dictionary <PressedKeysInfo, bool>(); quickCastHotkeys = new Dictionary <PressedKeysInfo, Action>(); onReleaseHotkeys = new Dictionary <PressedKeysInfo, Action>(); Start(); }
public void ResetAndReadHotkeysFromConfig() { globalHotkeyService.RemoveAllHotkeys(); var currentDispatcher = Dispatcher.CurrentDispatcher; // hotkey action should not make hotkeyservice/hook wait globalHotkeyService.AddOrUpdateOnReleaseHotkey( PressedKeysInfo.FromString(appSettingService.HotKey_StartStopRecording), () => { if (audioPlaybackService.Stopped) { currentDispatcher.Invoke(() => actionService.ToggleStartStopRecording()); } }); //() => //{ // var task = Task.Run(() => { if (audioPlaybackService.Stopped) { currentDispatcher.Invoke(() => actionService.ToggleStartStopRecording()); } }); // task.Start(); // task.Wait(); // task.Dispose(); //}); }
/// <summary> /// processes the passed PressedKeysInfo through possible quickcast (keydown) or on release hotkeys (keyup) /// modifies the passed PressedKeysInfo to reflect the updated keystates after hotkey processing /// </summary> /// <param name="e"></param> /// <param name="pressedKeysInfo"></param> private void ProcessKeyHook(GlobalKeyboardHook.GlobalKeyboardHookEventArgs e) { if (ProcessingHotkeys) { if (e.KeyDown) { ProcessHotkeysDown(); } else if (e.KeyUp) { ProcessHotkeysUp(); ResetHotkeyPressedStates(); // should this happen conditionally? // if you hold down some keys and lift ANY of them, keystate will be reset // a hotkey on release [shift] + [pause] will trigger if both held down and pause is released // (shift could be held down and pause pressed repeatedly and that works - BUT if pause were to be held down and shift is pressed repeatedly the hotkey only triggeres the first time) // this also applies to quickcast hotkeys pressing THE "key" repeatedly works, but for modifiers: NO // flipping modifier key seems pretty odd (just think about the annoying windows messages when spamming shift) so i think im okay with this :D pressedKeysInfo = PressedKeysInfo.Empty; } } }