예제 #1
0
        /// <inheritdoc/>>
        public async Task StartProcessingInputEvents()
        {
            lock (_lock)
            {
                if (_isProcessingKeyboardEvents)
                {
                    throw new InvalidOperationException($"{nameof(UserInputHandler)} is already processing keyboard events");
                }

                _isProcessingKeyboardEvents = true;
            }

            using (IKeyboardEventSource keyboard = WindowsInput.Capture.Global.KeyboardAsync())
            {
                using (IMouseEventSource mouse = WindowsInput.Capture.Global.MouseAsync())
                {
                    mouse.ButtonScroll += OnMouseScroll;
                    keyboard.KeyEvent  += OnKeyEvent;
                    mouse.ButtonDown   += (sender, args) => OnMouseOnButtonEvent(args, MouseEventType.KeyDown);
                    mouse.ButtonUp     += (sender, args) => OnMouseOnButtonEvent(args, MouseEventType.KeyUp);

                    _taskCompletionSource = new TaskCompletionSource <bool>();

                    await _taskCompletionSource.Task.ConfigureAwait(false);
                }
            }
        }
예제 #2
0
        private void Unsubscribe()
        {
            m_Keyboard?.Dispose();
            m_Keyboard = null;

            m_Mouse?.Dispose();
            m_Mouse = null;
        }
예제 #3
0
        private void Subscribe(IMouseEventSource Mouse)
        {
            this.m_Mouse?.Dispose();
            this.m_Mouse = Mouse;

            if (Mouse != default)
            {
                Mouse.MouseEvent += this.Mouse_MouseEvent;
            }
        }
예제 #4
0
        /// <inheritdoc/>
        public async Task <UserInputKey> WaitForKeyPress(bool blockPressedKey = false)
        {
            var taskCompletionSource = new TaskCompletionSource <UserInputKey>();

            using (IKeyboardEventSource keyboard = WindowsInput.Capture.Global.KeyboardAsync())
            {
                using (IMouseEventSource mouse = WindowsInput.Capture.Global.MouseAsync())
                {
                    mouse.ButtonScroll += (_, keyEventArgs) =>
                    {
                        if (keyEventArgs?.Data?.Button != null)
                        {
                            UserInputKey mappedKey = _keyMapper.MapToUserInputKey(keyEventArgs.Data.Offset);
                            if (blockPressedKey)
                            {
                                keyEventArgs.Next_Hook_Enabled = false;
                            }
                            taskCompletionSource.SetResult(mappedKey);
                        }
                    };
                    mouse.ButtonDown += (_, keyEventArgs) =>
                    {
                        if (keyEventArgs?.Data?.Button != null && keyEventArgs.Data.Button != ButtonCode.VScroll && keyEventArgs.Data.Button != ButtonCode.HScroll)
                        {
                            UserInputKey mappedKey = _keyMapper.MapToUserInputKey(keyEventArgs.Data.Button);
                            if (blockPressedKey)
                            {
                                keyEventArgs.Next_Hook_Enabled = false;
                            }
                            taskCompletionSource.SetResult(mappedKey);
                        }
                    };
                    keyboard.KeyEvent += (_, keyEventArgs) =>
                    {
                        if (keyEventArgs?.Data?.KeyDown != null)
                        {
                            UserInputKey mappedKey = _keyMapper.MapToUserInputKey(keyEventArgs.Data.KeyDown.Key);
                            if (blockPressedKey)
                            {
                                keyEventArgs.Next_Hook_Enabled = false;
                            }
                            taskCompletionSource.SetResult(mappedKey);
                        }
                    };

                    return(await taskCompletionSource.Task.ConfigureAwait(false));
                }
            }
        }