Skip to main content

ferroui_base/input/
i_keyboard_device.rs

1use super::IInputDevice;
2use bitflags::bitflags;
3use std::fmt;
4
5bitflags! {
6    /// Defines the keyboard modifier keys.
7    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
8    pub struct KeyModifiers: i32 {
9        const ALT = 1;
10        const CONTROL = 2;
11        const SHIFT = 4;
12        const META = 8;
13    }
14}
15
16/// The error returned when a string does not name keyboard modifiers.
17#[derive(Clone, Debug, PartialEq, Eq)]
18pub struct ParseKeyModifiersError(pub String);
19
20impl fmt::Display for ParseKeyModifiersError {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        write!(f, "Requested value '{}' was not found.", self.0)
23    }
24}
25
26impl std::error::Error for ParseKeyModifiersError {}
27
28impl KeyModifiers {
29    /// No modifier key.
30    pub const NONE: KeyModifiers = KeyModifiers::empty();
31
32    /// Parses a modifier name (`None`, `Alt`, `Control`, `Shift`, `Meta`;
33    /// ignoring case), a comma-separated list of names, or a numeric value.
34    pub fn parse(s: &str) -> Result<KeyModifiers, ParseKeyModifiersError> {
35        let mut result = KeyModifiers::empty();
36
37        for part in s.split(',') {
38            let part = part.trim();
39            let value = if part.eq_ignore_ascii_case("None") {
40                KeyModifiers::empty()
41            } else if part.eq_ignore_ascii_case("Alt") {
42                KeyModifiers::ALT
43            } else if part.eq_ignore_ascii_case("Control") {
44                KeyModifiers::CONTROL
45            } else if part.eq_ignore_ascii_case("Shift") {
46                KeyModifiers::SHIFT
47            } else if part.eq_ignore_ascii_case("Meta") {
48                KeyModifiers::META
49            } else {
50                part.parse::<i32>()
51                    .ok()
52                    .and_then(KeyModifiers::from_bits)
53                    .ok_or_else(|| ParseKeyModifiersError(s.to_string()))?
54            };
55            result |= value;
56        }
57
58        Ok(result)
59    }
60}
61
62bitflags! {
63    /// Defines the state of a key.
64    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
65    pub struct KeyStates: i32 {
66        const DOWN = 1;
67        const TOGGLED = 2;
68    }
69}
70
71impl KeyStates {
72    /// The key is neither down nor toggled.
73    pub const NONE: KeyStates = KeyStates::empty();
74}
75
76bitflags! {
77    /// The modifier keys and pointer buttons reported by the platform with
78    /// a raw input event.
79    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
80    pub struct RawInputModifiers: i32 {
81        const ALT = 1;
82        const CONTROL = 2;
83        const SHIFT = 4;
84        const META = 8;
85
86        const LEFT_MOUSE_BUTTON = 16;
87        const RIGHT_MOUSE_BUTTON = 32;
88        const MIDDLE_MOUSE_BUTTON = 64;
89        const X_BUTTON_1_MOUSE_BUTTON = 128;
90        const X_BUTTON_2_MOUSE_BUTTON = 256;
91        const KEYBOARD_MASK = Self::ALT.bits() | Self::CONTROL.bits() | Self::SHIFT.bits() | Self::META.bits();
92
93        const PEN_INVERTED = 512;
94        const PEN_ERASER = 1024;
95        const PEN_BARREL_BUTTON = 2048;
96    }
97}
98
99impl RawInputModifiers {
100    /// No modifier.
101    pub const NONE: RawInputModifiers = RawInputModifiers::empty();
102}
103
104/// Represents a keyboard device.
105pub trait IKeyboardDevice: IInputDevice {}