/// <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); } } }
private void Unsubscribe() { m_Keyboard?.Dispose(); m_Keyboard = null; m_Mouse?.Dispose(); m_Mouse = null; }
private void Subscribe(IMouseEventSource Mouse) { this.m_Mouse?.Dispose(); this.m_Mouse = Mouse; if (Mouse != default) { Mouse.MouseEvent += this.Mouse_MouseEvent; } }
/// <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)); } } }