From 1e995c36b052e632946898737eb12d0954664460 Mon Sep 17 00:00:00 2001 From: fwcd Date: Wed, 5 Mar 2025 05:55:25 +0100 Subject: [PATCH] Implement new modifier API --- lighthouse-protocol/src/input/input_event.rs | 17 ++++++------ lighthouse-protocol/src/input/key_event.rs | 12 +++------ .../src/input/key_modifiers.rs | 26 +++++++++++++++++++ lighthouse-protocol/src/input/mod.rs | 2 ++ 4 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 lighthouse-protocol/src/input/key_modifiers.rs diff --git a/lighthouse-protocol/src/input/input_event.rs b/lighthouse-protocol/src/input/input_event.rs index 759b13a..ffe9062 100644 --- a/lighthouse-protocol/src/input/input_event.rs +++ b/lighthouse-protocol/src/input/input_event.rs @@ -15,7 +15,7 @@ pub enum InputEvent { mod tests { use serde_json::json; - use crate::{EventSource, GamepadAxisEvent, GamepadButtonEvent, GamepadControlEvent, GamepadEvent, InputEvent, KeyEvent, MouseButton, MouseEvent, Pos}; + use crate::{EventSource, GamepadAxisEvent, GamepadButtonEvent, GamepadControlEvent, GamepadEvent, InputEvent, KeyEvent, KeyModifiers, MouseButton, MouseEvent, Pos}; #[test] fn key_event() { @@ -26,20 +26,19 @@ mod tests { "down": true, "repeat": false, "code": "ArrowUp", - "altKey": false, - "ctrlKey": false, - "metaKey": false, - "shiftKey": false, + "modifiers": { + "alt": false, + "ctrl": false, + "meta": false, + "shift": false, + }, })).unwrap(), InputEvent::Key(KeyEvent { source: EventSource::Int(0), down: true, repeat: false, code: "ArrowUp".into(), - alt_key: false, - ctrl_key: false, - meta_key: false, - shift_key: false, + modifiers: KeyModifiers::default(), }) ); } diff --git a/lighthouse-protocol/src/input/key_event.rs b/lighthouse-protocol/src/input/key_event.rs index d3f0117..4f4b445 100644 --- a/lighthouse-protocol/src/input/key_event.rs +++ b/lighthouse-protocol/src/input/key_event.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use super::EventSource; +use super::{EventSource, KeyModifiers}; /// A keyboard event. #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] @@ -14,12 +14,6 @@ pub struct KeyEvent { pub repeat: bool, /// The key pressed, see the docs on JS's `KeyboardEvent.code` for details. pub code: String, // TODO: Extract stronger `Key` type - /// Whether the alt key is held. - pub alt_key: bool, - /// Whether the ctrl key is held. - pub ctrl_key: bool, - /// Whether the meta key is held. - pub meta_key: bool, - /// Whether the shiftKey key is held. - pub shift_key: bool, + /// The held key modifiers. + pub modifiers: KeyModifiers, } diff --git a/lighthouse-protocol/src/input/key_modifiers.rs b/lighthouse-protocol/src/input/key_modifiers.rs new file mode 100644 index 0000000..57d18ed --- /dev/null +++ b/lighthouse-protocol/src/input/key_modifiers.rs @@ -0,0 +1,26 @@ +use serde::{Deserialize, Serialize}; + +/// A keyboard event. +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] +#[serde(rename_all = "camelCase")] +pub struct KeyModifiers { + /// Whether the alt key is held. + pub alt: bool, + /// Whether the ctrl key is held. + pub ctrl: bool, + /// Whether the meta key is held. + pub meta: bool, + /// Whether the shiftKey key is held. + pub shift: bool, +} + +impl Default for KeyModifiers { + fn default() -> Self { + Self { + alt: false, + ctrl: false, + meta: false, + shift: false, + } + } +} diff --git a/lighthouse-protocol/src/input/mod.rs b/lighthouse-protocol/src/input/mod.rs index bf5765f..e6e2a33 100644 --- a/lighthouse-protocol/src/input/mod.rs +++ b/lighthouse-protocol/src/input/mod.rs @@ -5,6 +5,7 @@ mod gamepad_control_event; mod gamepad_event; mod input_event; mod key_event; +mod key_modifiers; mod legacy_input_event; mod mouse_button; mod mouse_event; @@ -16,6 +17,7 @@ pub use gamepad_control_event::*; pub use gamepad_event::*; pub use input_event::*; pub use key_event::*; +pub use key_modifiers::*; pub use legacy_input_event::*; pub use mouse_button::*; pub use mouse_event::*;