Skip to content

Commit

Permalink
clear the modifier keys on exiting the focused window (#519)
Browse files Browse the repository at this point in the history
linux_wayland: clear all mod keys on leaving the active window

This fixes the issue when you are type a key command to leave a window like ALT+tab. The window will receive just ALT and it will remain stuck after you refocus.
  • Loading branch information
narodnik authored Jan 26, 2025
1 parent 8b8dc62 commit 782ab83
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ pub trait EventHandler {

fn key_down_event(&mut self, _keycode: KeyCode, _keymods: KeyMods, _repeat: bool) {}

/// Note: you are not always guaranteed to receive a key_up event. For example on
/// Wayland when leaving the focused window, the key_up event will not be caught.
fn key_up_event(&mut self, _keycode: KeyCode, _keymods: KeyMods) {}

/// Default implementation emulates mouse clicks
Expand Down
9 changes: 8 additions & 1 deletion src/native/linux_wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,14 @@ unsafe extern "C" fn keyboard_handle_enter(
// Ignore this for now.
}
unsafe extern "C" fn keyboard_handle_leave(
_data: *mut ::core::ffi::c_void,
data: *mut ::core::ffi::c_void,
_wl_keyboard: *mut wl_keyboard,
_serial: u32,
_surface: *mut wl_surface,
) {
// Clear modifiers
let display: &mut WaylandPayload = &mut *(data as *mut _);
(display.xkb.xkb_state_update_mask)(display.xkb_state, 0, 0, 0, 0, 0, 0);
EVENTS.push(WaylandEvent::KeyboardLeave);
}
unsafe extern "C" fn keyboard_handle_key(
Expand Down Expand Up @@ -808,6 +811,10 @@ where
match event {
WaylandEvent::KeyboardLeave => {
repeated_keys.clear();
keymods.shift = false;
keymods.ctrl = false;
keymods.logo = false;
keymods.alt = false;
}
WaylandEvent::KeyboardKey(key, state) => {
// https://wayland-book.com/seat/keyboard.html
Expand Down

0 comments on commit 782ab83

Please sign in to comment.