Skip to main content

ferroui_base/input/
key_event_args.rs

1use super::{IKeyModifiersEventArgs, Key, KeyDeviceType, KeyModifiers, PhysicalKey};
2use crate::ferro_routed_event_args;
3use crate::interactivity::RoutedEventArgs;
4
5/// Provides information specific to a keyboard event.
6#[derive(Clone, Default)]
7pub struct KeyEventArgs {
8    base: RoutedEventArgs,
9
10    /// The virtual-key for the associated event.
11    ///
12    /// A given physical key can result in different virtual keys depending
13    /// on the current keyboard layout. This is the key that is generally
14    /// referred to when creating keyboard shortcuts. For example, when
15    /// pressing the key located at the `Z` position on standard US English
16    /// QWERTY keyboard, this property returns:
17    ///
18    /// - `Key::Z` for an English (QWERTY) layout
19    /// - `Key::W` for a French (AZERTY) layout
20    /// - `Key::Y` for a German (QWERTZ) layout
21    /// - `Key::Z` for a Russian (JCUKEN) layout
22    pub key: Key,
23
24    /// The key modifiers for the associated event.
25    pub key_modifiers: KeyModifiers,
26
27    /// The physical key for the associated event.
28    ///
29    /// This value is independent of the current keyboard layout and usually
30    /// corresponds to the key printed on a standard US English QWERTY
31    /// keyboard. This is the key to use for position-dependent bindings
32    /// such as WASD movement in games.
33    pub physical_key: PhysicalKey,
34
35    /// The unicode symbol of the key, or `None` if none is applicable.
36    ///
37    /// For example, when pressing the key located at the `Z` position on
38    /// standard US English QWERTY keyboard, this property returns:
39    ///
40    /// - `z` for an English (QWERTY) layout
41    /// - `w` for a French (AZERTY) layout
42    /// - `y` for a German (QWERTZ) layout
43    /// - `я` for a Russian (JCUKEN) layout
44    pub key_symbol: Option<String>,
45
46    /// The type of key device that sent this event.
47    pub key_device_type: KeyDeviceType,
48}
49
50ferro_routed_event_args!(KeyEventArgs: RoutedEventArgs);
51
52impl KeyEventArgs {
53    /// Creates args with default values and no routed event.
54    pub fn new() -> Self {
55        Self::default()
56    }
57}
58
59impl IKeyModifiersEventArgs for KeyEventArgs {
60    fn key_modifiers(&self) -> KeyModifiers {
61        self.key_modifiers
62    }
63}