Skip to content

Handle winit's WindowEvent::ModifiersChanged #13357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ use bevy_app::{App, AppExit, Last, Plugin, PluginsState};
use bevy_ecs::event::ManualEventReader;
use bevy_ecs::prelude::*;
use bevy_ecs::system::SystemState;
#[cfg(target_os = "macos")]
use bevy_input::{
keyboard::{Key, KeyCode, KeyboardInput},
ButtonState,
};
use bevy_input::{
mouse::{MouseButtonInput, MouseMotion, MouseScrollUnit, MouseWheel},
touchpad::{TouchpadMagnify, TouchpadRotate},
Expand All @@ -53,6 +58,8 @@ use bevy_window::{
};
#[cfg(target_os = "android")]
use bevy_window::{PrimaryWindow, RawHandleWrapper};
#[cfg(target_os = "macos")]
use winit::{event::Modifiers, keyboard::ModifiersKeyState};

#[cfg(target_os = "android")]
pub use winit::platform::android::activity as android_activity;
Expand Down Expand Up @@ -198,6 +205,9 @@ struct WinitAppRunnerState {
wait_elapsed: bool,
/// Number of "forced" updates to trigger on application start
startup_forced_updates: u32,
#[cfg(target_os = "macos")]
/// State of the keyboard modifiers.
keyboard_modifiers: Modifiers,
}

impl WinitAppRunnerState {
Expand All @@ -218,6 +228,8 @@ impl Default for WinitAppRunnerState {
wait_elapsed: false,
// 3 seems to be enough, 5 is a safe margin
startup_forced_updates: 5,
#[cfg(target_os = "macos")]
keyboard_modifiers: Modifiers::default(),
}
}
}
Expand Down Expand Up @@ -578,7 +590,67 @@ fn handle_winit_event(
winit_events.send(ReceivedCharacter { window, char });
}
}
winit_events.send(converters::convert_keyboard_input(event, window));

let keyboard_event = converters::convert_keyboard_input(event, window);

#[cfg(not(target_os = "macos"))]
winit_events.send(keyboard_event);

#[cfg(target_os = "macos")]
{
// Send a `KeyboardInput` event if this key is not a modifier.
let mod_keys = [Key::Alt, Key::Control, Key::Shift, Key::Super];
if !mod_keys.contains(&keyboard_event.logical_key) {
winit_events.send(keyboard_event);
}
}
}
#[cfg(target_os = "macos")]
WindowEvent::ModifiersChanged(mods) => {
// Check if the state of any modifier key has changed.
let checks: [(KeyCode, Key, &mut dyn FnMut(Modifiers) -> ModifiersKeyState);
8] = [
(KeyCode::SuperLeft, Key::Super, &mut |mods| {
mods.lsuper_state()
}),
(KeyCode::SuperRight, Key::Super, &mut |mods| {
mods.rsuper_state()
}),
(KeyCode::ShiftLeft, Key::Shift, &mut |mods| {
mods.lshift_state()
}),
(KeyCode::ShiftRight, Key::Shift, &mut |mods| {
mods.rshift_state()
}),
(KeyCode::AltLeft, Key::Alt, &mut |mods| mods.lalt_state()),
(KeyCode::AltRight, Key::Alt, &mut |mods| mods.ralt_state()),
(KeyCode::ControlLeft, Key::Control, &mut |mods| {
mods.lcontrol_state()
}),
(KeyCode::ControlRight, Key::Control, &mut |mods| {
mods.rcontrol_state()
}),
];

for (key_code, key, f) in checks {
let new = f(mods);
let old = f(runner_state.keyboard_modifiers);

if new != old {
winit_events.send(KeyboardInput {
key_code,
logical_key: key,
state: if new == ModifiersKeyState::Pressed {
ButtonState::Pressed
} else {
ButtonState::Released
},
window,
});
}
}

runner_state.keyboard_modifiers = mods;
}
WindowEvent::CursorMoved { position, .. } => {
let physical_position = DVec2::new(position.x, position.y);
Expand Down