freya_elements/events/
keyboard.rs

1pub use keyboard_types::{
2    Code,
3    Key,
4    Modifiers,
5};
6
7use crate::{
8    events::ErasedEventData,
9    impl_event,
10};
11
12impl_event! [
13    KeyboardData;
14
15    /// The `keydown` event fires when the user starts pressing any key in the currently focused element.
16    ///
17    /// Event Data: [`KeyboardData`](crate::events::KeyboardData)
18    ///
19    /// ### Example
20    ///
21    /// ```rust, no_run
22    /// # use freya::prelude::*;
23    /// fn app() -> Element {
24    ///     rsx!(
25    ///         rect {
26    ///             onkeydown: |e| println!("Event: {e:?}")
27    ///         }
28    ///     )
29    /// }
30    /// ```
31    onkeydown
32
33    /// The `keyup` event fires when the user releases any key being pressed in the currently focused element.
34    ///
35    /// Event Data: [`KeyboardData`](crate::events::KeyboardData)
36    ///
37    /// ### Example
38    ///
39    /// ```rust, no_run
40    /// # use freya::prelude::*;
41    /// fn app() -> Element {
42    ///     rsx!(
43    ///         rect {
44    ///             onkeyup: |e| println!("Event: {e:?}")
45    ///         }
46    ///     )
47    /// }
48    /// ```
49    onkeyup
50
51    /// The `globalkeydown` event fires when the user starts pressing any key.
52    ///
53    /// Event Data: [`KeyboardData`](crate::events::KeyboardData)
54    ///
55    /// ### Example
56    ///
57    /// ```rust, no_run
58    /// # use freya::prelude::*;
59    /// fn app() -> Element {
60    ///     rsx!(
61    ///         rect {
62    ///             onglobalkeydown: |e| println!("Event: {e:?}")
63    ///         }
64    ///     )
65    /// }
66    /// ```
67    onglobalkeydown
68
69    /// The `globalkeyup` event fires when the user releases any key being pressed.
70    ///
71    /// Event Data: [`KeyboardData`](crate::events::KeyboardData)
72    ///
73    /// ### Example
74    ///
75    /// ```rust, no_run
76    /// # use freya::prelude::*;
77    /// fn app() -> Element {
78    ///     rsx!(
79    ///         rect {
80    ///             onglobalkeyup: |e| println!("Event: {e:?}")
81    ///         }
82    ///     )
83    /// }
84    /// ```
85    onglobalkeyup
86];
87
88/// Data of a Keyboard event.
89#[derive(Debug, Clone, PartialEq)]
90pub struct KeyboardData {
91    pub key: Key,
92    pub code: Code,
93    pub modifiers: Modifiers,
94}
95
96impl KeyboardData {
97    pub fn new(key: Key, code: Code, modifiers: Modifiers) -> Self {
98        Self {
99            key,
100            code,
101            modifiers,
102        }
103    }
104}
105
106impl KeyboardData {
107    /// Try to get the text of the character
108    pub fn to_text(&self) -> Option<&str> {
109        if let Key::Character(c) = &self.key {
110            Some(c)
111        } else {
112            None
113        }
114    }
115}
116
117impl From<&ErasedEventData> for KeyboardData {
118    fn from(val: &ErasedEventData) -> Self {
119        val.downcast::<KeyboardData>().cloned().unwrap()
120    }
121}