private void TestKeyHandler_SequenceWithOptionalKeys_OneTry()
        {
            var sequenceHandler = new KeySequenceHandler(
                new KeyCombinationHandler(VirtualKeyCode.Print),
                new KeyOrCombinationHandler(
                    new KeyCombinationHandler(VirtualKeyCode.Shift, VirtualKeyCode.KeyA),
                    new KeyCombinationHandler(VirtualKeyCode.Shift, VirtualKeyCode.KeyB))
                )
            {
                Timeout = null
            };

            bool result;

            // Print key
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.Print));
            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyUp(VirtualKeyCode.Print));
            Assert.False(result);

            // Shift KeyB
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.Shift));
            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.KeyB));
            Assert.True(result);
            // Shift KeyB
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyUp(VirtualKeyCode.KeyB));
            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyUp(VirtualKeyCode.Shift));
            Assert.False(result);
        }
        private void TestKeyHandler_KeySequenceHandler_Right()
        {
            var sequenceHandler = new KeySequenceHandler(
                new KeyCombinationHandler(VirtualKeyCode.Print),
                new KeyCombinationHandler(VirtualKeyCode.Shift, VirtualKeyCode.KeyA))
            {
                Timeout = null
            };

            var result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.Print));

            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyUp(VirtualKeyCode.Print));
            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.Control));
            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.Control));
            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyUp(VirtualKeyCode.Control));
            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.Shift));
            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.KeyA));
            Assert.True(result);
        }
        private void TestKeyHandler_KeySequenceHandler_Wrong_Right()
        {
            var sequenceHandler = new KeySequenceHandler(
                new KeyCombinationHandler(VirtualKeyCode.Print),
                new KeyCombinationHandler(VirtualKeyCode.Shift, VirtualKeyCode.KeyA))
            {
                // Prevent debug issues, we are not testing the timeout here!
                Timeout = null
            };

            var result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.Print));

            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.Shift));
            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.KeyB));
            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyUp(VirtualKeyCode.KeyB));
            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyUp(VirtualKeyCode.Shift));
            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyUp(VirtualKeyCode.Print));
            Assert.False(result);

            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.Shift));
            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.KeyA));
            Assert.True(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyUp(VirtualKeyCode.KeyA));
            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyUp(VirtualKeyCode.Shift));
            Assert.False(result);
            Assert.False(sequenceHandler.HasKeysPressed);
        }
        private void TestKeyHandler_KeyCombinationHandler_KeyUp()
        {
            var keyCombinationHandler = new KeyCombinationHandler(VirtualKeyCode.Print);

            var result = keyCombinationHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.Print));

            Assert.True(result);
            result = keyCombinationHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.Control));
            Assert.False(result);
            result = keyCombinationHandler.Handle(KeyboardHookEventArgs.KeyUp(VirtualKeyCode.Control));
            Assert.False(result);
        }
        private void TestKeyHandler_KeyCombinationHandler_Repeat()
        {
            var keyCombinationHandler = new KeyCombinationHandler(VirtualKeyCode.Print);

            var keyPrintDown = KeyboardHookEventArgs.KeyDown(VirtualKeyCode.Print);

            var keyPrintUp = KeyboardHookEventArgs.KeyUp(VirtualKeyCode.Print);

            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);
        }
        private async Task TestKeyHandler_SequenceWithOptionalKeys_Timeout()
        {
            var sequenceHandler = new KeySequenceHandler(
                new KeyCombinationHandler(VirtualKeyCode.Print),
                new KeyOrCombinationHandler(
                    new KeyCombinationHandler(VirtualKeyCode.Shift, VirtualKeyCode.KeyA),
                    new KeyCombinationHandler(VirtualKeyCode.Shift, VirtualKeyCode.KeyB))
                )
            {
                Timeout = TimeSpan.FromMilliseconds(200)
            };

            bool result;

            // Print key
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.Print));
            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyUp(VirtualKeyCode.Print));
            Assert.False(result);

            // Shift KeyB
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.Shift));
            Assert.True(sequenceHandler.HasKeysPressed);
            Assert.False(result);
            await Task.Delay(400);

            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyDown(VirtualKeyCode.KeyB));
            Assert.True(sequenceHandler.HasKeysPressed);
            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyUp(VirtualKeyCode.KeyB));
            Assert.True(sequenceHandler.HasKeysPressed);
            Assert.False(result);
            result = sequenceHandler.Handle(KeyboardHookEventArgs.KeyUp(VirtualKeyCode.Shift));
            Assert.False(sequenceHandler.HasKeysPressed);
            Assert.False(result);
        }