dioxus_web/events/
keyboard.rs1use std::str::FromStr;
2
3use dioxus_html::{
4 input_data::decode_key_location, Code, HasKeyboardData, Key, Location, Modifiers,
5 ModifiersInteraction,
6};
7use web_sys::KeyboardEvent;
8
9use super::{Synthetic, WebEventExt};
10
11impl HasKeyboardData for Synthetic<KeyboardEvent> {
12 fn key(&self) -> Key {
13 Key::from_str(self.event.key().as_str()).unwrap_or(Key::Unidentified)
14 }
15
16 fn code(&self) -> Code {
17 Code::from_str(self.event.code().as_str()).unwrap_or(Code::Unidentified)
18 }
19
20 fn location(&self) -> Location {
21 decode_key_location(self.event.location() as usize)
22 }
23
24 fn is_auto_repeating(&self) -> bool {
25 self.event.repeat()
26 }
27
28 fn is_composing(&self) -> bool {
29 self.event.is_composing()
30 }
31
32 fn as_any(&self) -> &dyn std::any::Any {
33 &self.event
34 }
35}
36
37impl ModifiersInteraction for Synthetic<KeyboardEvent> {
38 fn modifiers(&self) -> Modifiers {
39 let mut modifiers = Modifiers::empty();
40
41 if self.event.alt_key() {
42 modifiers.insert(Modifiers::ALT);
43 }
44 if self.event.ctrl_key() {
45 modifiers.insert(Modifiers::CONTROL);
46 }
47 if self.event.meta_key() {
48 modifiers.insert(Modifiers::META);
49 }
50 if self.event.shift_key() {
51 modifiers.insert(Modifiers::SHIFT);
52 }
53
54 modifiers
55 }
56}
57
58impl WebEventExt for dioxus_html::KeyboardData {
59 type WebEvent = web_sys::KeyboardEvent;
60
61 #[inline(always)]
62 fn try_as_web_event(&self) -> Option<web_sys::KeyboardEvent> {
63 self.downcast::<web_sys::KeyboardEvent>().cloned()
64 }
65}