Skip to content
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

New keyboard various fixes #2817

Merged
merged 9 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ And please only add new entries to the top of this list, right below the `# Unre

# Unreleased

- **Breaking:** Remove all deprecated `modifiers` fields.
- **Breaking:** Overhaul keyboard input handling.
- Replace `KeyboardInput` with `KeyEvent` and `RawKeyEvent`.
- Change `WindowEvent::KeyboardInput` to contain a `KeyEvent`.
Expand All @@ -32,6 +33,9 @@ And please only add new entries to the top of this list, right below the `# Unre
portable) interpretations of a given key-press.
- Add `KeyCodeExtScancode`, which lets you convert between raw keycodes and
`KeyCode`.
- Remove `WindowExtMacOS::option_as_alt` and `WindowExtMacOS::set_option_as_alt`.
- `ModifiersChanged` now uses dedicated `Modifiers` struct.
- On Orbital, fix `ModifiersChanged` not being sent.
- **Breaking:** `CursorIcon` is now used from the `cursor-icon` crate.
- **Breaking:** `CursorIcon::Hand` is now named `CursorIcon::Pointer`.
- **Breaking:** `CursorIcon::Arrow` was removed.
Expand Down
6 changes: 5 additions & 1 deletion docs/res/ATTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ These images are used in the documentation of `winit`.

## keyboard_*.svg

These files are a modified version of "[ANSI US QWERTY (Windows)](https://commons.wikimedia.org/wiki/File:ANSI_US_QWERTY_(Windows).svg)" by [Tomiĉo](https://commons.wikimedia.org/wiki/User:Tomi%C4%89o). It is originally released under the [CC-BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/deed.en) License. Minor modifications have been made by [John Nunley](https://github.com/notgull), which have been released under the same license as a derivative work.
These files are a modified version of "[ANSI US QWERTY (Windows)](https://commons.wikimedia.org/wiki/File:ANSI_US_QWERTY_(Windows).svg)"
by [Tomiĉo] (https://commons.wikimedia.org/wiki/User:Tomi%C4%89o). It is
originally released under the [CC-BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/deed.en)
License. Minor modifications have been made by [John Nunley](https://github.com/notgull),
which have been released under the same license as a derivative work.
2 changes: 1 addition & 1 deletion examples/cursor_grab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn main() {
println!("error: {err}");
}
}
WindowEvent::ModifiersChanged(m) => modifiers = m,
WindowEvent::ModifiersChanged(new) => modifiers = new.state(),
_ => (),
},
Event::DeviceEvent { event, .. } => match event {
Expand Down
4 changes: 2 additions & 2 deletions examples/key_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ fn main() {
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::ModifiersChanged(new_state) => {
modifiers = new_state;
WindowEvent::ModifiersChanged(new) => {
modifiers = new.state();
}
WindowEvent::KeyboardInput { event, .. } => {
if event.state == ElementState::Pressed && !event.repeat {
Expand Down
4 changes: 2 additions & 2 deletions examples/multithreaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ fn main() {
);
}
}
WindowEvent::ModifiersChanged(mod_state) => {
modifiers = mod_state;
WindowEvent::ModifiersChanged(new) => {
modifiers = new.state();
}
WindowEvent::KeyboardInput {
event:
Expand Down
66 changes: 0 additions & 66 deletions examples/window_option_as_alt.rs

This file was deleted.

134 changes: 102 additions & 32 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use std::path::PathBuf;
use crate::window::Window;
use crate::{
dpi::{PhysicalPosition, PhysicalSize},
keyboard::{self, ModifiersState},
keyboard::{self, ModifiersKeyState, ModifiersKeys, ModifiersState},
platform_impl,
window::{Theme, WindowId},
};
Expand Down Expand Up @@ -391,12 +391,7 @@ pub enum WindowEvent<'a> {
},

/// The keyboard modifiers have changed.
///
/// ## Platform-specific
///
/// - **Web:** This API is currently unimplemented on the web. This isn't by design - it's an
/// issue, and it should get fixed - but it's the current state of the API.
ModifiersChanged(ModifiersState),
ModifiersChanged(Modifiers),

/// An event from an input method.
///
Expand All @@ -415,8 +410,6 @@ pub enum WindowEvent<'a> {
/// limited by the display area and it may have been transformed by the OS to implement effects such as cursor
/// acceleration, it should not be used to implement non-cursor-like interactions such as 3D camera control.
position: PhysicalPosition<f64>,
#[deprecated = "Deprecated in favor of WindowEvent::ModifiersChanged"]
modifiers: ModifiersState,
},

/// The cursor has entered the window.
Expand All @@ -430,17 +423,13 @@ pub enum WindowEvent<'a> {
device_id: DeviceId,
delta: MouseScrollDelta,
phase: TouchPhase,
#[deprecated = "Deprecated in favor of WindowEvent::ModifiersChanged"]
modifiers: ModifiersState,
},

/// An mouse button press has been received.
MouseInput {
device_id: DeviceId,
state: ElementState,
button: MouseButton,
#[deprecated = "Deprecated in favor of WindowEvent::ModifiersChanged"]
modifiers: ModifiersState,
},

/// Touchpad magnification event with two-finger pinch gesture.
Expand Down Expand Up @@ -576,45 +565,36 @@ impl Clone for WindowEvent<'static> {
},
Ime(preedit_state) => Ime(preedit_state.clone()),
ModifiersChanged(modifiers) => ModifiersChanged(*modifiers),
#[allow(deprecated)]
CursorMoved {
device_id,
position,
modifiers,
} => CursorMoved {
device_id: *device_id,
position: *position,
modifiers: *modifiers,
},
CursorEntered { device_id } => CursorEntered {
device_id: *device_id,
},
CursorLeft { device_id } => CursorLeft {
device_id: *device_id,
},
#[allow(deprecated)]
MouseWheel {
device_id,
delta,
phase,
modifiers,
} => MouseWheel {
device_id: *device_id,
delta: *delta,
phase: *phase,
modifiers: *modifiers,
},
#[allow(deprecated)]
MouseInput {
device_id,
state,
button,
modifiers,
} => MouseInput {
device_id: *device_id,
state: *state,
button: *button,
modifiers: *modifiers,
},
TouchpadMagnify {
device_id,
Expand Down Expand Up @@ -686,43 +666,34 @@ impl<'a> WindowEvent<'a> {
event,
is_synthetic,
}),
ModifiersChanged(modifiers) => Some(ModifiersChanged(modifiers)),
ModifiersChanged(modifers) => Some(ModifiersChanged(modifers)),
Ime(event) => Some(Ime(event)),
#[allow(deprecated)]
CursorMoved {
device_id,
position,
modifiers,
} => Some(CursorMoved {
device_id,
position,
modifiers,
}),
CursorEntered { device_id } => Some(CursorEntered { device_id }),
CursorLeft { device_id } => Some(CursorLeft { device_id }),
#[allow(deprecated)]
MouseWheel {
device_id,
delta,
phase,
modifiers,
} => Some(MouseWheel {
device_id,
delta,
phase,
modifiers,
}),
#[allow(deprecated)]
MouseInput {
device_id,
state,
button,
modifiers,
} => Some(MouseInput {
device_id,
state,
button,
modifiers,
}),
TouchpadMagnify {
device_id,
Expand Down Expand Up @@ -966,6 +937,105 @@ pub struct KeyEvent {
pub(crate) platform_specific: platform_impl::KeyEventExtra,
}

/// Describes keyboard modifiers event.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Modifiers {
pub(crate) state: ModifiersState,

// NOTE: Currently pressed modifiers keys.
//
// The field providing a metadata, it shouldn't be used as a source of truth.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// The field providing a metadata, it shouldn't be used as a source of truth.
// The field provides metadata, and shouldn't be used as a source of truth.

nit

pub(crate) pressed_mods: ModifiersKeys,
}

impl Modifiers {
/// The state of the modifiers.
pub fn state(&self) -> ModifiersState {
self.state
}

/// The state of the left shift key.
pub fn lshift_state(&self) -> ModifiersKeyState {
if self.pressed_mods.contains(ModifiersKeys::LSHIFT) {
ModifiersKeyState::Pressed
} else {
ModifiersKeyState::Unknown
}
}
kchibisov marked this conversation as resolved.
Show resolved Hide resolved

/// The state of the right shift key.
pub fn rshift_state(&self) -> ModifiersKeyState {
if self.pressed_mods.contains(ModifiersKeys::RSHIFT) {
ModifiersKeyState::Pressed
} else {
ModifiersKeyState::Unknown
}
}

/// The state of the left alt key.
pub fn lalt_state(&self) -> ModifiersKeyState {
if self.pressed_mods.contains(ModifiersKeys::LALT) {
ModifiersKeyState::Pressed
} else {
ModifiersKeyState::Unknown
}
}

/// The state of the right alt key.
pub fn ralt_state(&self) -> ModifiersKeyState {
if self.pressed_mods.contains(ModifiersKeys::RALT) {
ModifiersKeyState::Pressed
} else {
ModifiersKeyState::Unknown
}
}

/// The state of the left control key.
pub fn lcontrol_state(&self) -> ModifiersKeyState {
if self.pressed_mods.contains(ModifiersKeys::LCONTROL) {
ModifiersKeyState::Pressed
} else {
ModifiersKeyState::Unknown
}
}

/// The state of the right control key.
pub fn rcontrol_state(&self) -> ModifiersKeyState {
if self.pressed_mods.contains(ModifiersKeys::RCONTROL) {
ModifiersKeyState::Pressed
} else {
ModifiersKeyState::Unknown
}
}

/// The state of the left super key.
pub fn lsuper_state(&self) -> ModifiersKeyState {
if self.pressed_mods.contains(ModifiersKeys::LSUPER) {
ModifiersKeyState::Pressed
} else {
ModifiersKeyState::Unknown
}
}

/// The state of the right super key.
pub fn rsuper_state(&self) -> ModifiersKeyState {
if self.pressed_mods.contains(ModifiersKeys::RSUPER) {
ModifiersKeyState::Pressed
} else {
ModifiersKeyState::Unknown
}
}
}

impl From<ModifiersState> for Modifiers {
fn from(value: ModifiersState) -> Self {
Self {
state: value,
pressed_mods: Default::default(),
}
}
}

/// Describes [input method](https://en.wikipedia.org/wiki/Input_method) events.
///
/// This is also called a "composition event".
Expand Down
Loading