diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index 12b7b471..210e1b0b 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -78,6 +78,7 @@ changelog entry. - Implement `CustomIconProvider` for `RgbaIcon`. - Add `icon` module that exposes winit's icon API. - `VideoMode::new` to create a `VideoMode`. +- `keyboard::ModifiersKey` to track which modifier is exactly pressed. ### Changed diff --git a/src/lib.rs b/src/lib.rs index 87109d43..ff1438e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -305,8 +305,7 @@ mod cursor; pub mod event; pub mod event_loop; pub mod icon; -pub mod keyboard; -pub use winit_core::monitor; +pub use winit_core::{keyboard, monitor}; mod platform_impl; use winit_core::as_any as utils; pub mod window; diff --git a/src/platform_impl/web/keyboard.rs b/src/platform_impl/web/keyboard.rs index e1b0e7e6..83c3c1ab 100644 --- a/src/platform_impl/web/keyboard.rs +++ b/src/platform_impl/web/keyboard.rs @@ -2,8 +2,14 @@ use smol_str::SmolStr; use crate::keyboard::{Key, KeyCode, NamedKey, NativeKey, NativeKeyCode, PhysicalKey}; -impl Key { - pub(crate) fn from_key_attribute_value(kav: &str) -> Self { +pub trait FromAttributeValue { + fn from_attribute_value(kav: &str) -> Self + where + Self: Sized; +} + +impl FromAttributeValue for Key { + fn from_attribute_value(kav: &str) -> Self { Key::Named(match kav { "Unidentified" => return Key::Unidentified(NativeKey::Web(SmolStr::new(kav))), "Dead" => return Key::Dead(None), @@ -319,8 +325,8 @@ impl Key { } } -impl PhysicalKey { - pub fn from_key_code_attribute_value(kcav: &str) -> Self { +impl FromAttributeValue for PhysicalKey { + fn from_attribute_value(kcav: &str) -> Self { PhysicalKey::Code(match kcav { "Backquote" => KeyCode::Backquote, "Backslash" => KeyCode::Backslash, diff --git a/src/platform_impl/web/web_sys/event.rs b/src/platform_impl/web/web_sys/event.rs index dbecbf39..df7c590c 100644 --- a/src/platform_impl/web/web_sys/event.rs +++ b/src/platform_impl/web/web_sys/event.rs @@ -9,6 +9,7 @@ use web_sys::{KeyboardEvent, MouseEvent, Navigator, PointerEvent, WheelEvent}; use super::Engine; use crate::event::{FingerId, MouseButton, MouseScrollDelta, PointerKind}; use crate::keyboard::{Key, KeyLocation, ModifiersState, NamedKey, PhysicalKey}; +use crate::platform_impl::web::keyboard::FromAttributeValue; bitflags::bitflags! { // https://www.w3.org/TR/pointerevents3/#the-buttons-property @@ -170,16 +171,16 @@ pub fn pointer_type(event: &PointerEvent, pointer_id: i32) -> PointerKind { pub fn key_code(event: &KeyboardEvent) -> PhysicalKey { let code = event.code(); - PhysicalKey::from_key_code_attribute_value(&code) + PhysicalKey::from_attribute_value(&code) } pub fn key(event: &KeyboardEvent) -> Key { - Key::from_key_attribute_value(&event.key()) + Key::from_attribute_value(&event.key()) } pub fn key_text(event: &KeyboardEvent) -> Option { let key = event.key(); - let key = Key::from_key_attribute_value(&key); + let key = Key::from_attribute_value(&key); match &key { Key::Character(text) => Some(text.clone()), Key::Named(NamedKey::Tab) => Some(SmolStr::new("\t")), diff --git a/src/keyboard.rs b/winit-core/src/keyboard.rs similarity index 99% rename from src/keyboard.rs rename to winit-core/src/keyboard.rs index f1838d9f..3f41d1c6 100644 --- a/src/keyboard.rs +++ b/winit-core/src/keyboard.rs @@ -1750,7 +1750,7 @@ pub enum ModifiersKeyState { bitflags! { #[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] - pub(crate) struct ModifiersKeys: u8 { + pub struct ModifiersKeys: u8 { const LSHIFT = 0b0000_0001; const RSHIFT = 0b0000_0010; const LCONTROL = 0b0000_0100; diff --git a/winit-core/src/lib.rs b/winit-core/src/lib.rs index 440c3393..676399e4 100644 --- a/winit-core/src/lib.rs +++ b/winit-core/src/lib.rs @@ -1,3 +1,4 @@ #[macro_use] pub mod as_any; +pub mod keyboard; pub mod monitor;