Skip to content

Commit 1e995c3

Browse files
committed
Implement new modifier API
1 parent 906fc2d commit 1e995c3

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

lighthouse-protocol/src/input/input_event.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub enum InputEvent {
1515
mod tests {
1616
use serde_json::json;
1717

18-
use crate::{EventSource, GamepadAxisEvent, GamepadButtonEvent, GamepadControlEvent, GamepadEvent, InputEvent, KeyEvent, MouseButton, MouseEvent, Pos};
18+
use crate::{EventSource, GamepadAxisEvent, GamepadButtonEvent, GamepadControlEvent, GamepadEvent, InputEvent, KeyEvent, KeyModifiers, MouseButton, MouseEvent, Pos};
1919

2020
#[test]
2121
fn key_event() {
@@ -26,20 +26,19 @@ mod tests {
2626
"down": true,
2727
"repeat": false,
2828
"code": "ArrowUp",
29-
"altKey": false,
30-
"ctrlKey": false,
31-
"metaKey": false,
32-
"shiftKey": false,
29+
"modifiers": {
30+
"alt": false,
31+
"ctrl": false,
32+
"meta": false,
33+
"shift": false,
34+
},
3335
})).unwrap(),
3436
InputEvent::Key(KeyEvent {
3537
source: EventSource::Int(0),
3638
down: true,
3739
repeat: false,
3840
code: "ArrowUp".into(),
39-
alt_key: false,
40-
ctrl_key: false,
41-
meta_key: false,
42-
shift_key: false,
41+
modifiers: KeyModifiers::default(),
4342
})
4443
);
4544
}
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::{Deserialize, Serialize};
22

3-
use super::EventSource;
3+
use super::{EventSource, KeyModifiers};
44

55
/// A keyboard event.
66
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
@@ -14,12 +14,6 @@ pub struct KeyEvent {
1414
pub repeat: bool,
1515
/// The key pressed, see the docs on JS's `KeyboardEvent.code` for details.
1616
pub code: String, // TODO: Extract stronger `Key` type
17-
/// Whether the alt key is held.
18-
pub alt_key: bool,
19-
/// Whether the ctrl key is held.
20-
pub ctrl_key: bool,
21-
/// Whether the meta key is held.
22-
pub meta_key: bool,
23-
/// Whether the shiftKey key is held.
24-
pub shift_key: bool,
17+
/// The held key modifiers.
18+
pub modifiers: KeyModifiers,
2519
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
/// A keyboard event.
4+
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
5+
#[serde(rename_all = "camelCase")]
6+
pub struct KeyModifiers {
7+
/// Whether the alt key is held.
8+
pub alt: bool,
9+
/// Whether the ctrl key is held.
10+
pub ctrl: bool,
11+
/// Whether the meta key is held.
12+
pub meta: bool,
13+
/// Whether the shiftKey key is held.
14+
pub shift: bool,
15+
}
16+
17+
impl Default for KeyModifiers {
18+
fn default() -> Self {
19+
Self {
20+
alt: false,
21+
ctrl: false,
22+
meta: false,
23+
shift: false,
24+
}
25+
}
26+
}

lighthouse-protocol/src/input/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod gamepad_control_event;
55
mod gamepad_event;
66
mod input_event;
77
mod key_event;
8+
mod key_modifiers;
89
mod legacy_input_event;
910
mod mouse_button;
1011
mod mouse_event;
@@ -16,6 +17,7 @@ pub use gamepad_control_event::*;
1617
pub use gamepad_event::*;
1718
pub use input_event::*;
1819
pub use key_event::*;
20+
pub use key_modifiers::*;
1921
pub use legacy_input_event::*;
2022
pub use mouse_button::*;
2123
pub use mouse_event::*;

0 commit comments

Comments
 (0)