ratzilla/
event.rs

1/// A key event.
2#[derive(Debug, Clone, Eq, PartialEq)]
3pub struct KeyEvent {
4    /// The key code.
5    pub code: KeyCode,
6    /// Whether the control key is pressed.
7    pub ctrl: bool,
8    /// Whether the alt key is pressed.
9    pub alt: bool,
10    /// Whether the shift key is pressed.
11    pub shift: bool,
12}
13
14/// A mouse movement event.
15#[derive(Debug, Clone, Eq, PartialEq)]
16pub struct MouseEvent {
17    /// The mouse button that was pressed.
18    pub button: MouseButton,
19    /// The triggered event.
20    pub event: MouseEventKind,
21    /// The x coordinate of the mouse.
22    pub x: u32,
23    /// The y coordinate of the mouse.
24    pub y: u32,
25    /// Whether the control key is pressed.
26    pub ctrl: bool,
27    /// Whether the alt key is pressed.
28    pub alt: bool,
29    /// Whether the shift key is pressed.
30    pub shift: bool,
31}
32
33/// Convert a [`web_sys::KeyboardEvent`] to a [`KeyEvent`].
34impl From<web_sys::KeyboardEvent> for KeyEvent {
35    fn from(event: web_sys::KeyboardEvent) -> Self {
36        let ctrl = event.ctrl_key();
37        let alt = event.alt_key();
38        let shift = event.shift_key();
39        KeyEvent {
40            code: event.into(),
41            ctrl,
42            alt,
43            shift,
44        }
45    }
46}
47
48/// A key code.
49#[derive(Debug, Clone, Eq, PartialEq)]
50pub enum KeyCode {
51    /// Normal letter key input.
52    Char(char),
53    /// F keys.
54    F(u8),
55    /// Backspace key
56    Backspace,
57    /// Enter or return key
58    Enter,
59    /// Left arrow key
60    Left,
61    /// Right arrow key
62    Right,
63    /// Up arrow key
64    Up,
65    /// Down arrow key
66    Down,
67    /// Tab key
68    Tab,
69    /// Delete key
70    Delete,
71    /// Home key
72    Home,
73    /// End key
74    End,
75    /// Page up key
76    PageUp,
77    /// Page down key
78    PageDown,
79    /// Escape key
80    Esc,
81    /// Unidentified.
82    Unidentified,
83}
84
85/// Convert a [`web_sys::KeyboardEvent`] to a [`KeyCode`].
86impl From<web_sys::KeyboardEvent> for KeyCode {
87    fn from(event: web_sys::KeyboardEvent) -> Self {
88        let key = event.key();
89        if key.len() == 1 {
90            let char = key.chars().next();
91            if let Some(char) = char {
92                return KeyCode::Char(char);
93            } else {
94                return KeyCode::Unidentified;
95            }
96        }
97        match key.as_str() {
98            "F1" => KeyCode::F(1),
99            "F2" => KeyCode::F(2),
100            "F3" => KeyCode::F(3),
101            "F4" => KeyCode::F(4),
102            "F5" => KeyCode::F(5),
103            "F6" => KeyCode::F(6),
104            "F7" => KeyCode::F(7),
105            "F8" => KeyCode::F(8),
106            "F9" => KeyCode::F(9),
107            "F10" => KeyCode::F(10),
108            "F11" => KeyCode::F(11),
109            "F12" => KeyCode::F(12),
110            "Backspace" => KeyCode::Backspace,
111            "Enter" => KeyCode::Enter,
112            "ArrowLeft" => KeyCode::Left,
113            "ArrowRight" => KeyCode::Right,
114            "ArrowUp" => KeyCode::Up,
115            "ArrowDown" => KeyCode::Down,
116            "Tab" => KeyCode::Tab,
117            "Delete" => KeyCode::Delete,
118            "Home" => KeyCode::Home,
119            "End" => KeyCode::End,
120            "PageUp" => KeyCode::PageUp,
121            "PageDown" => KeyCode::PageDown,
122            "Escape" => KeyCode::Esc,
123            _ => KeyCode::Unidentified,
124        }
125    }
126}
127
128/// A mouse button.
129#[derive(Debug, Clone, Eq, PartialEq)]
130pub enum MouseButton {
131    /// Left mouse button
132    Left,
133    /// Right mouse button
134    Right,
135    /// Middle mouse button
136    Middle,
137    /// Back mouse button
138    Back,
139    /// Forward mouse button
140    Forward,
141    /// Unidentified mouse button
142    Unidentified,
143}
144
145/// A mouse event.
146#[derive(Debug, Clone, Eq, PartialEq)]
147pub enum MouseEventKind {
148    /// Mouse moved
149    Moved,
150    /// Mouse button pressed
151    Pressed,
152    /// Mouse button released
153    Released,
154    /// Unidentified mouse event
155    Unidentified,
156}
157
158/// Convert a [`web_sys::MouseEvent`] to a [`MouseEvent`].
159impl From<web_sys::MouseEvent> for MouseEvent {
160    fn from(event: web_sys::MouseEvent) -> Self {
161        let ctrl = event.ctrl_key();
162        let alt = event.alt_key();
163        let shift = event.shift_key();
164        let event_type = event.type_().into();
165        MouseEvent {
166            // Button is only valid if it is a mousedown or mouseup event.
167            button: if event_type == MouseEventKind::Moved {
168                MouseButton::Unidentified
169            } else {
170                event.button().into()
171            },
172            event: event_type,
173            x: event.client_x() as u32,
174            y: event.client_y() as u32,
175            ctrl,
176            alt,
177            shift,
178        }
179    }
180}
181
182/// Convert a [`web_sys::MouseEvent`] to a [`MouseButton`].
183impl From<i16> for MouseButton {
184    fn from(button: i16) -> Self {
185        match button {
186            0 => MouseButton::Left,
187            1 => MouseButton::Middle,
188            2 => MouseButton::Right,
189            3 => MouseButton::Back,
190            4 => MouseButton::Forward,
191            _ => MouseButton::Unidentified,
192        }
193    }
194}
195
196/// Convert a [`web_sys::MouseEvent`] to a [`MouseEventKind`].
197impl From<String> for MouseEventKind {
198    fn from(event: String) -> Self {
199        let event = event.as_str();
200        match event {
201            "mousemove" => MouseEventKind::Moved,
202            "mousedown" => MouseEventKind::Pressed,
203            "mouseup" => MouseEventKind::Released,
204            _ => MouseEventKind::Unidentified,
205        }
206    }
207}