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