diff --git a/packages/wm/src/common/platform/event_window.rs b/packages/wm/src/common/platform/event_window.rs index 6dbe6c32..aeb96c26 100644 --- a/packages/wm/src/common/platform/event_window.rs +++ b/packages/wm/src/common/platform/event_window.rs @@ -34,7 +34,10 @@ use windows::Win32::{ use super::{ KeyboardHook, MouseMoveEvent, Platform, PlatformEvent, WindowEventHook, }; -use crate::{common::Point, user_config::KeybindingConfig}; +use crate::{ + common::{platform::FOREGROUND_INPUT_IDENTIFIER, Point}, + user_config::KeybindingConfig, +}; /// Global instance of sender for platform events. /// @@ -266,13 +269,14 @@ fn handle_input_msg( ) }; - // Ignore if data is invalid or not a mouse event. The `hDevice` check - // ignores programmatic mouse inputs via `SendInput` (this caused issues - // since `NativeWindow::set_foreground` simulates a mouse input). + // Ignore if data is invalid or not a mouse event. Inputs from our own + // process are ignored, which would cause issues since + // `NativeWindow::set_foreground` simulates a mouse input. if res_size == 0 || raw_input_size == u32::MAX || raw_input.header.dwType != RIM_TYPEMOUSE.0 - || raw_input.header.hDevice.is_invalid() + || unsafe { raw_input.data.mouse.ulExtraInformation } + == FOREGROUND_INPUT_IDENTIFIER { return Ok(()); } diff --git a/packages/wm/src/common/platform/native_window.rs b/packages/wm/src/common/platform/native_window.rs index e0a1555e..2e366729 100644 --- a/packages/wm/src/common/platform/native_window.rs +++ b/packages/wm/src/common/platform/native_window.rs @@ -18,7 +18,9 @@ use windows::{ }, }, UI::{ - Input::KeyboardAndMouse::{SendInput, INPUT, INPUT_MOUSE}, + Input::KeyboardAndMouse::{ + SendInput, INPUT, INPUT_0, INPUT_MOUSE, MOUSEINPUT, + }, Shell::{ITaskbarList, TaskbarList}, WindowsAndMessaging::{ EnumWindows, GetClassNameW, GetWindow, GetWindowLongPtrW, @@ -45,6 +47,10 @@ use crate::{ windows::WindowState, }; +/// Magic number used to identify programmatic mouse inputs from our own +/// process. +pub const FOREGROUND_INPUT_IDENTIFIER: u32 = 6379; + #[derive(Debug, Clone)] pub struct NativeWindow { pub handle: isize, @@ -288,6 +294,12 @@ impl NativeWindow { pub fn set_foreground(&self) -> anyhow::Result<()> { let input = [INPUT { r#type: INPUT_MOUSE, + Anonymous: INPUT_0 { + mi: MOUSEINPUT { + dwExtraInfo: FOREGROUND_INPUT_IDENTIFIER as usize, + ..Default::default() + }, + }, ..Default::default() }];