Skip to content

Commit

Permalink
feat: set foreground window without simulating keypress (#672)
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger authored Aug 12, 2024
1 parent 6f6795c commit de8450e
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions packages/wm/src/common/platform/native_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,21 +280,24 @@ impl NativeWindow {
}

pub fn set_foreground(&self) -> anyhow::Result<()> {
// Simulate a key press event to activate the window.
let input = INPUT {
r#type: INPUT_KEYBOARD,
Anonymous: INPUT_0 {
ki: KEYBDINPUT {
wVk: VIRTUAL_KEY(0),
wScan: 0,
dwFlags: KEYBD_EVENT_FLAGS(0),
time: 0,
dwExtraInfo: 0,
// UIAccess allows for setting the foreground window without needing to
// send a key press event.
#[cfg(not(feature = "ui_access"))]
unsafe {
let input = INPUT {
r#type: INPUT_KEYBOARD,
Anonymous: INPUT_0 {
ki: KEYBDINPUT {
wVk: VIRTUAL_KEY(1),
wScan: 0,
dwFlags: KEYBD_EVENT_FLAGS(0),
time: 0,
dwExtraInfo: 0,
},
},
},
};
};

unsafe {
// Simulate a key press event to activate the window.
SendInput(&[input], std::mem::size_of::<INPUT>() as i32);
}

Expand Down

0 comments on commit de8450e

Please sign in to comment.