private async Task TestKeyHandler_SingleCombination() { int pressCount = 0; var keyHandler = new KeyCombinationHandler(VirtualKeyCode.Back, VirtualKeyCode.RightShift) { IgnoreInjected = false, IsPassThrough = false }; using (KeyboardHook.KeyboardEvents.Where(keyHandler).Subscribe(keyboardHookEventArgs => pressCount++)) { await Task.Delay(20); KeyboardInputGenerator.KeyCombinationPress(VirtualKeyCode.Back, VirtualKeyCode.RightShift); await Task.Delay(20); KeyboardInputGenerator.KeyCombinationPress(VirtualKeyCode.Back, VirtualKeyCode.RightShift); await Task.Delay(20); } Assert.True(pressCount == 2); KeyboardInputGenerator.KeyCombinationPress(VirtualKeyCode.Back, VirtualKeyCode.RightShift); await Task.Delay(20); Assert.True(pressCount == 2); }
private async Task TestKeyHandler_Slow_Subscriber() { int pressCount = 0; const int pressHandlingTime = 500; // Wait 2x press plus overhead const int waitForPressHandling = (int)((pressHandlingTime * 2) * 1.1); var sequenceHandler = new KeyCombinationHandler(VirtualKeyCode.Shift, VirtualKeyCode.KeyA) { IgnoreInjected = false }; using (KeyboardHook.KeyboardEvents.Where(sequenceHandler).Subscribe(keyboardHookEventArgs => { Log.Info().WriteLine("Key combination was pressed, slow handling!", null); Thread.Sleep(pressHandlingTime); Log.Info().WriteLine("Key combination was pressed, finished!", null); pressCount++; })) { Log.Info().WriteLine("Pressing key combination", null); KeyboardInputGenerator.KeyCombinationPress(VirtualKeyCode.Shift, VirtualKeyCode.KeyA); Log.Info().WriteLine("Pressed key combination", null); Log.Info().WriteLine("Pressing key combination", null); KeyboardInputGenerator.KeyCombinationPress(VirtualKeyCode.Shift, VirtualKeyCode.KeyA); Log.Info().WriteLine("Pressed key combination", null); await Task.Delay(waitForPressHandling); Assert.Equal(2, pressCount); } }
private void TestKeyHandler_KeyCombinationHandler_Repeat() { var keyCombinationHandler = new KeyCombinationHandler(VirtualKeyCode.Print); var keyPrintDown = new KeyboardHookEventArgs { Key = VirtualKeyCode.Print, IsKeyDown = true }; var keyPrintUp = new KeyboardHookEventArgs { Key = VirtualKeyCode.Print, IsKeyDown = false }; var result = keyCombinationHandler.Handle(keyPrintDown); Assert.True(result); result = keyCombinationHandler.Handle(keyPrintDown); Assert.False(result); // Key up again result = keyCombinationHandler.Handle(keyPrintUp); Assert.False(result); result = keyCombinationHandler.Handle(keyPrintDown); Assert.True(result); }
public void AddCombinationHandler(Buttons buttons, KeyCombinationHandler handler) { List <KeyCombinationHandler> listHandlers = null; foreach (Buttons b in keyCombinationHandlerMap.Keys) { if (b.Equals(buttons)) { listHandlers = keyCombinationHandlerMap[b]; break; } } if (listHandlers == null) { listHandlers = new List <KeyCombinationHandler>(); keyCombinationHandlerMap[buttons] = listHandlers; } if (!listHandlers.Contains(handler)) { listHandlers.Add(handler); } events.AddRef(); }
public void AddCombinationHandler(HashSet <Keys> keys, KeyCombinationHandler handler) { List <KeyCombinationHandler> listHandlers = null; foreach (HashSet <Keys> k in keyCombinationHandlerMap.Keys) { if (AreKeysetsEqual(keys, k)) { listHandlers = keyCombinationHandlerMap[k]; break; } } if (listHandlers == null) { listHandlers = new List <KeyCombinationHandler>(); keyCombinationHandlerMap[keys] = listHandlers; } if (!listHandlers.Contains(handler)) { listHandlers.Add(handler); } AddRef(); }
private static void Main(string[] args) { var key = new KeyCombinationHandler(VirtualKeyCode.KeyA); using (KeyboardHook.KeyboardEvents.Where(key).Subscribe(e => Hit())) { MessageLoop.ProcessMessages(); } }
private void TestKeyHandler_KeyCombinationHandler_KeyUp() { var keyCombinationHandler = new KeyCombinationHandler(VirtualKeyCode.Print); var result = keyCombinationHandler.Handle(KeyDown(VirtualKeyCode.Print)); Assert.True(result); result = keyCombinationHandler.Handle(KeyDown(VirtualKeyCode.Control)); Assert.False(result); result = keyCombinationHandler.Handle(KeyUp(VirtualKeyCode.Control)); Assert.False(result); }
public void RemoveCombinationHandler(Buttons buttons, KeyCombinationHandler handler) { foreach (Buttons b in keyCombinationHandlerMap.Keys) { if (b.Equals(buttons)) { keyCombinationHandlerMap[b].Remove(handler); break; } } events.RemoveRef(); }
public void RemoveCombinationHandler(HashSet <Keys> keys, KeyCombinationHandler handler) { foreach (HashSet <Keys> k in keyCombinationHandlerMap.Keys) { if (AreKeysetsEqual(keys, k)) { keyCombinationHandlerMap[k].Remove(handler); break; } } RemoveRef(); }
/// <summary> /// The constructor for the history MenuItem /// </summary> /// <param name="dopyContextMenuTranslations"></param> /// <param name="windowManager"></param> /// <param name="historyViewModelFactory"></param> public HistoryMenuItem( IDopyTranslations dopyContextMenuTranslations, IWindowManager windowManager, Func <Owned <HistoryViewModel> > historyViewModelFactory ) { // automatically update the DisplayName dopyContextMenuTranslations.CreateDisplayNameBinding(this, nameof(IDopyTranslations.History)); Id = "Y_History"; Icon = new PackIconMaterial { Kind = PackIconMaterialKind.History }; // Key to react to var controlShiftPasteKey = new KeyCombinationHandler(VirtualKeyCode.Control, VirtualKeyCode.Shift, VirtualKeyCode.KeyV); KeyboardHook.KeyboardEvents // The hotkey to listen do .Where(controlShiftPasteKey) #if !NETCOREAPP3_1 // TODO: What about dotnet core 3.0? // Make sure it's on the dispatcher .SubscribeOnDispatcher() #endif // What to do .Subscribe(args => { if (IsEnabled) { Click(this); } args.Handled = true; }); HotKeyHint = "Ctrl + Shift + V"; ClickAction = clickedItem => { IsEnabled = false; try { using var historyViewModel = historyViewModelFactory(); windowManager.ShowDialog(historyViewModel.Value); } finally { IsEnabled = true; } }; }
public void Startup() { var uiSynchronizationContext = SynchronizationContext.Current; var keyHandler = new KeyCombinationHandler(_pipConfiguration.HotKey) { CanRepeat = false, IsPassThrough = false }; KeyboardHook.KeyboardEvents .Where(keyHandler) .SubscribeOn(uiSynchronizationContext) .ObserveOn(uiSynchronizationContext) .Subscribe(keyboardHookEventArgs => { // Get the current active window var pipSource = InteropWindowQuery.GetForegroundWindow(); while (pipSource.GetParent() != IntPtr.Zero) { pipSource = InteropWindowFactory.CreateFor(pipSource.GetParent()); } // If there is already a form, close it and remove it from the dictionary if (_thumbnailForms.TryGetValue(pipSource.Handle, out var thumbnailForm)) { thumbnailForm.Close(); _thumbnailForms.Remove(pipSource.Handle); return; } // Check if we have a location available if (!_locationPool.HasAvailable) { return; } thumbnailForm = new ThumbnailForm(_pipConfiguration, _locationPool, pipSource.Handle, uiSynchronizationContext); _thumbnailForms[pipSource.Handle] = thumbnailForm; thumbnailForm.Show(); }); }