keyboard_types/
modifiers.rs

1//! Modifier key data.
2//!
3//! Modifier keys like Shift and Control alter the character value
4//! and are used in keyboard shortcuts.
5//!
6//! Use the constants to match for combinations of the modifier keys.
7
8bitflags::bitflags! {
9    /// Pressed modifier keys.
10    ///
11    /// Specification:
12    /// <https://w3c.github.io/uievents-key/#keys-modifier>
13    #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15    pub struct Modifiers: u32 {
16        const ALT = 0x01;
17        const ALT_GRAPH = 0x2;
18        const CAPS_LOCK = 0x4;
19        const CONTROL = 0x8;
20        const FN = 0x10;
21        const FN_LOCK = 0x20;
22        const META = 0x40;
23        const NUM_LOCK = 0x80;
24        const SCROLL_LOCK = 0x100;
25        const SHIFT = 0x200;
26        const SYMBOL = 0x400;
27        const SYMBOL_LOCK = 0x800;
28        const HYPER = 0x1000;
29        const SUPER = 0x2000;
30    }
31}
32
33impl Modifiers {
34    /// Return `true` if a shift key is pressed.
35    pub fn shift(&self) -> bool {
36        self.contains(Modifiers::SHIFT)
37    }
38
39    /// Return `true` if a control key is pressed.
40    pub fn ctrl(&self) -> bool {
41        self.contains(Modifiers::CONTROL)
42    }
43
44    /// Return `true` if an alt key is pressed.
45    pub fn alt(&self) -> bool {
46        self.contains(Modifiers::ALT)
47    }
48
49    /// Return `true` if a meta key is pressed.
50    pub fn meta(&self) -> bool {
51        self.contains(Modifiers::META)
52    }
53}