public void ReceivingNativeInput_WhenHooked_CallsFactoryWithReceivedInput() { // Arrange Action <NativeKeyboardInput> nativeInputCallback = null; _nativeKeyboardHookServiceMock.Setup(f => f.Hook(It.IsAny <Action <NativeKeyboardInput> >())).Callback <Action <NativeKeyboardInput> >(a => nativeInputCallback = a); _keyboardInputFactoryMock.Setup(f => f.Create(It.IsAny <NativeKeyboardInput>())); var keyboardInput = new KeyboardInput( KeyboardInputKey.A, KeyboardInputDirection.KeyDown, new KeyboardInputLocks(false, false, false), new KeyboardInputModifiers(!false, false, false)); _keyboardInputFactoryMock.Setup(f => f.Create(It.IsAny <NativeKeyboardInput>())).Returns(keyboardInput); var keyboardEventConfiguration = new KeyboardEventConfiguration( new KeyboardInputKeyConfiguration(KeyboardInputKey.AllKeys.ToArray()), ModifierConfiguration.CreateNotApplicable(), LockConfiguration.CreateNotApplicable()); _receiverMock.Setup(f => f.ReceiveAsync(It.IsAny <KeyboardInput>())).Returns(Task.FromResult(true)); _receiverMock.Setup(f => f.Configuration).Returns(keyboardEventConfiguration); _sut.HookKeyboard(); var nativeKeyboardInput = new NativeKeyboardInput(Keys.A, NativeKeyboardInputDirection.KeyDown); // Act nativeInputCallback(nativeKeyboardInput); // Assert _keyboardInputFactoryMock.Verify(f => f.Create(nativeKeyboardInput), Times.Once); }
public static void InitialiseInputHandlers(System.Windows.Forms.Control mouseFocalControl) { KeyboardInputHandler = new NativeKeyboardInput(); MouseInputHandler = new MGForms_MouseInput(mouseFocalControl); // System.Windows.Forms.Application.Idle += delegate { UpdateInputStates(); }; inputHandlersReady = true; }
public void ReceivingNativeInput_ReceiverNotConfiguredForEvent_DoesNotCallReceiver() { // Arrange const bool ScrollLockMustBeActive = true; var keyboardInput = new KeyboardInput( KeyboardInputKey.A, KeyboardInputDirection.KeyDown, new KeyboardInputLocks(false, false, false), new KeyboardInputModifiers(!ScrollLockMustBeActive, false, false)); var keyboardEventConfiguration = new KeyboardEventConfiguration( new KeyboardInputKeyConfiguration(KeyboardInputKey.AllKeys.ToArray()), ModifierConfiguration.CreateNotApplicable(), new LockConfiguration(ScrollLockMustBeActive, false, false)); _receiverMock.Setup(f => f.ReceiveAsync(It.IsAny <KeyboardInput>())).Returns(Task.FromResult(true)); _receiverMock.Setup(f => f.Configuration).Returns(keyboardEventConfiguration); _keyboardInputFactoryMock.Setup(f => f.Create(It.IsAny <NativeKeyboardInput>())).Returns(keyboardInput); Action <NativeKeyboardInput> nativeInputCallback = null; _nativeKeyboardHookServiceMock.Setup(f => f.Hook(It.IsAny <Action <NativeKeyboardInput> >())).Callback <Action <NativeKeyboardInput> >(a => nativeInputCallback = a); _sut.HookKeyboard(); var nativeKeyboardInput = new NativeKeyboardInput(Keys.Alt, NativeKeyboardInputDirection.KeyUp); // Act nativeInputCallback(nativeKeyboardInput); // Assert _receiverMock.Verify(f => f.ReceiveAsync(It.IsAny <KeyboardInput>()), Times.Never); }
public void ReceivingNativeInput_ReceiverConfiguredForAllEvents_CallsReceiver() { // Arrange var keyboardEventConfiguration = KeyboardEventConfiguration.CreateForAllEvents(); var keyboardInput = new KeyboardInput( KeyboardInputKey.A, KeyboardInputDirection.KeyDown, new KeyboardInputLocks(false, false, false), new KeyboardInputModifiers(true, false, false)); _receiverMock.Setup(f => f.ReceiveAsync(It.IsAny <KeyboardInput>())).Returns(Task.FromResult(true)); _receiverMock.Setup(f => f.Configuration).Returns(keyboardEventConfiguration); _keyboardInputFactoryMock.Setup(f => f.Create(It.IsAny <NativeKeyboardInput>())).Returns(keyboardInput); Action <NativeKeyboardInput> nativeInputCallback = null; _nativeKeyboardHookServiceMock.Setup(f => f.Hook(It.IsAny <Action <NativeKeyboardInput> >())).Callback <Action <NativeKeyboardInput> >( func => { nativeInputCallback = func; }); _sut.HookKeyboard(); var nativeKeyboardInput = new NativeKeyboardInput(Keys.A, NativeKeyboardInputDirection.KeyDown); // Act nativeInputCallback(nativeKeyboardInput); // Assert _receiverMock.Verify(f => f.ReceiveAsync(It.IsAny <KeyboardInput>()), Times.Once); }
public KeyboardInput Create(NativeKeyboardInput nativeInput) { return(new KeyboardInput( _inputKeyMappingServant.MapFromNativeKey(nativeInput.Key), MapDirection(nativeInput.Direction), _lockOptionsFactory.Create(), _modiferOptionsFactory.Create())); }
private void OnNativeKeyboardInput(NativeKeyboardInput nativeKeyboardInput) { var keyboardInput = _inputFactory.Create(nativeKeyboardInput); var receivingTasks = _receivers .Where(receiver => receiver.Configuration.CheckIfApplicable(keyboardInput)) .Select(receiver => receiver.ReceiveAsync(keyboardInput)) .ToArray(); Task.Run(() => Task.WhenAll(receivingTasks)); }
public void Mapping_CallsModifierOptionsFactoryOnce() { // Arrange const Keys NativeKey = Keys.A; var nativeKeyboardInput = new NativeKeyboardInput(NativeKey, NativeKeyboardInputDirection.KeyDown); _inputKeyMappingServantMock.Setup(f => f.MapFromNativeKey(Keys.A)).Returns(KeyboardInputKey.A); _lockOptionsFactoryMock.Setup(f => f.Create()).Returns(new KeyboardInputLocks(true, true, true)); _modifierOptionsFactoryMock.Setup(f => f.Create()).Returns(new KeyboardInputModifiers(true, true, true)); // Act _sut.Create(nativeKeyboardInput); // Assert _modifierOptionsFactoryMock.Verify(f => f.Create(), Times.Once); }
public void Mapping_ReturnesMappedInput() { // Arrange const Keys NativeKey = Keys.A; var nativeKeyboardInput = new NativeKeyboardInput(NativeKey, NativeKeyboardInputDirection.KeyDown); _inputKeyMappingServantMock.Setup(f => f.MapFromNativeKey(Keys.A)).Returns(KeyboardInputKey.A); _lockOptionsFactoryMock.Setup(f => f.Create()).Returns(new KeyboardInputLocks(true, false, true)); _modifierOptionsFactoryMock.Setup(f => f.Create()).Returns(new KeyboardInputModifiers(true, true, false)); // Act var actualInput = _sut.Create(nativeKeyboardInput); // Assert Assert.AreEqual(KeyboardInputDirection.KeyDown, actualInput.Direction); Assert.AreEqual(KeyboardInputKey.A, actualInput.InputKey); Assert.AreEqual(true, actualInput.Locks.IsCapsLockActive); Assert.AreEqual(false, actualInput.Locks.IsNumLockActive); Assert.AreEqual(true, actualInput.Locks.IsScrollLockActive); Assert.AreEqual(true, actualInput.Modifiers.IsAltPressed); Assert.AreEqual(true, actualInput.Modifiers.IsCtrlPressed); Assert.AreEqual(false, actualInput.Modifiers.IsShiftPressed); }