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        #[deprecated = "marked as legacy in the spec, use META instead"]
29        const HYPER = 0x1000;
30        #[deprecated = "marked as legacy in the spec, use META instead"]
31        const SUPER = 0x2000;
32    }
33}
34
35impl Modifiers {
36    /// Return `true` if a shift key is pressed.
37    pub fn shift(&self) -> bool {
38        self.contains(Modifiers::SHIFT)
39    }
40
41    /// Return `true` if a control key is pressed.
42    pub fn ctrl(&self) -> bool {
43        self.contains(Modifiers::CONTROL)
44    }
45
46    /// Return `true` if an alt key is pressed.
47    pub fn alt(&self) -> bool {
48        self.contains(Modifiers::ALT)
49    }
50
51    /// Return `true` if a meta key is pressed.
52    pub fn meta(&self) -> bool {
53        self.contains(Modifiers::META)
54    }
55}