public void RegisteredHotKeyShoudReact() { var hotKey = HotKey.Create(Key.Escape, ModifierKeys.None); hotKey.Register(); var toSend = new[] { new KeyEventArgs(Key.Escape, KeyState.KeyDown), new KeyEventArgs(Key.Escape, KeyState.KeyUp) }; var actual = EventWaiter.WaitEvent( handler => hotKey.Pressed += handler, handler => hotKey.Pressed -= handler, () => { _sender.Send(toSend); }); hotKey.Unregister(); Assert.True(actual); hotKey.Dispose(); }
public void SendingKeyShouldFireKeyReceivedEvent() { var expected = new List <KeyEventArgs> { new KeyEventArgs(Key.Escape, KeyState.KeyDown), new KeyEventArgs(Key.Escape, KeyState.KeyUp) }; var actual = EventWaiter.WaitEvent <KeyEventArgs>( handler => _receiver.KeyReceived += handler, handler => _receiver.KeyReceived -= handler, () => _sender.Send(expected), 2); Assert.That(actual, Is.EquivalentTo(expected)); }
public void ModalKeyShouldBeDownDuringReleasingKeyCombination() { var actual = KeyState.KeyUp; var are = new AutoResetEvent(false); _receiver.KeyReceived += (sender, args) => { if (args.Key == Key.S && args.KeyState == KeyState.KeyUp) { actual = _monitor.GetKeyState(Key.LeftCtrl); are.Set(); } }; _sender.Send(new[] { new KeyEventArgs(Key.LeftCtrl, KeyState.KeyDown) }); _sender.Send(new[] { new KeyEventArgs(Key.S, KeyState.KeyDown) }); _sender.Send(new[] { new KeyEventArgs(Key.S, KeyState.KeyUp) }); _sender.Send(new[] { new KeyEventArgs(Key.LeftCtrl, KeyState.KeyUp) }); are.WaitOne(100); Assert.That(actual, Is.EqualTo(KeyState.KeyDown)); }
public void Send(KeyCombination combination) { var keys = new List <KeyEventArgs>(); if (combination.KeyModifier.HasFlag(KeyModifier.LCtrl)) { keys.Add(new KeyEventArgs(Key.LeftCtrl, KeyState.KeyDown)); } if (combination.KeyModifier.HasFlag(KeyModifier.RCtrl)) { keys.Add(new KeyEventArgs(Key.RightCtrl, KeyState.KeyDown)); } if (combination.KeyModifier.HasFlag(KeyModifier.LShift)) { keys.Add(new KeyEventArgs(Key.LeftShift, KeyState.KeyDown)); } if (combination.KeyModifier.HasFlag(KeyModifier.RShift)) { keys.Add(new KeyEventArgs(Key.RightShift, KeyState.KeyDown)); } if (combination.KeyModifier.HasFlag(KeyModifier.LAtl)) { keys.Add(new KeyEventArgs(Key.LeftAlt, KeyState.KeyDown)); } if (combination.KeyModifier.HasFlag(KeyModifier.RAlt)) { keys.Add(new KeyEventArgs(Key.RightAlt, KeyState.KeyDown)); } if (combination.KeyModifier.HasFlag(KeyModifier.LWin)) { keys.Add(new KeyEventArgs(Key.LWin, KeyState.KeyDown)); } if (combination.KeyModifier.HasFlag(KeyModifier.RWin)) { keys.Add(new KeyEventArgs(Key.RWin, KeyState.KeyDown)); } keys.Add(new KeyEventArgs(combination.Key, KeyState.KeyDown)); keys.Add(new KeyEventArgs(combination.Key, KeyState.KeyUp)); if (combination.KeyModifier.HasFlag(KeyModifier.RWin)) { keys.Add(new KeyEventArgs(Key.RWin, KeyState.KeyUp)); } if (combination.KeyModifier.HasFlag(KeyModifier.LWin)) { keys.Add(new KeyEventArgs(Key.LWin, KeyState.KeyUp)); } if (combination.KeyModifier.HasFlag(KeyModifier.RAlt)) { keys.Add(new KeyEventArgs(Key.RightAlt, KeyState.KeyUp)); } if (combination.KeyModifier.HasFlag(KeyModifier.LAtl)) { keys.Add(new KeyEventArgs(Key.LeftAlt, KeyState.KeyUp)); } if (combination.KeyModifier.HasFlag(KeyModifier.RShift)) { keys.Add(new KeyEventArgs(Key.RightShift, KeyState.KeyUp)); } if (combination.KeyModifier.HasFlag(KeyModifier.LShift)) { keys.Add(new KeyEventArgs(Key.LeftShift, KeyState.KeyUp)); } if (combination.KeyModifier.HasFlag(KeyModifier.RCtrl)) { keys.Add(new KeyEventArgs(Key.RightCtrl, KeyState.KeyUp)); } if (combination.KeyModifier.HasFlag(KeyModifier.LCtrl)) { keys.Add(new KeyEventArgs(Key.LeftCtrl, KeyState.KeyUp)); } _sender.Send(keys); }