iced_core/keyboard/modifiers.rs
1use bitflags::bitflags;
2
3bitflags! {
4 /// The current state of the keyboard modifiers.
5 #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
6 pub struct Modifiers: u32{
7 /// The "shift" key.
8 const SHIFT = 0b100;
9 // const LSHIFT = 0b010 << 0;
10 // const RSHIFT = 0b001 << 0;
11 //
12 /// The "control" key.
13 const CTRL = 0b100 << 3;
14 // const LCTRL = 0b010 << 3;
15 // const RCTRL = 0b001 << 3;
16 //
17 /// The "alt" key.
18 const ALT = 0b100 << 6;
19 // const LALT = 0b010 << 6;
20 // const RALT = 0b001 << 6;
21 //
22 /// The "windows" key on Windows, "command" key on Mac, and
23 /// "super" key on Linux.
24 const LOGO = 0b100 << 9;
25 // const LLOGO = 0b010 << 9;
26 // const RLOGO = 0b001 << 9;
27 /// The Caps Lock key
28 const CAPS_LOCK = 0b100 << 12;
29 }
30}
31
32impl Modifiers {
33 /// The "command" key.
34 ///
35 /// This is normally the main modifier to be used for hotkeys.
36 ///
37 /// On macOS, this is equivalent to `Self::LOGO`.
38 /// Otherwise, this is equivalent to `Self::CTRL`.
39 pub const COMMAND: Self = if cfg!(target_os = "macos") {
40 Self::LOGO
41 } else {
42 Self::CTRL
43 };
44
45 /// Returns true if the [`SHIFT`] key is pressed in the [`Modifiers`].
46 ///
47 /// [`SHIFT`]: Self::SHIFT
48 pub fn shift(self) -> bool {
49 self.contains(Self::SHIFT)
50 }
51
52 /// Returns true if the [`CTRL`] key is pressed in the [`Modifiers`].
53 ///
54 /// [`CTRL`]: Self::CTRL
55 pub fn control(self) -> bool {
56 self.contains(Self::CTRL)
57 }
58
59 /// Returns true if the [`ALT`] key is pressed in the [`Modifiers`].
60 ///
61 /// [`ALT`]: Self::ALT
62 pub fn alt(self) -> bool {
63 self.contains(Self::ALT)
64 }
65
66 /// Returns true if the [`LOGO`] key is pressed in the [`Modifiers`].
67 ///
68 /// [`LOGO`]: Self::LOGO
69 pub fn logo(self) -> bool {
70 self.contains(Self::LOGO)
71 }
72
73 /// Returns true if a "command key" is pressed in the [`Modifiers`].
74 ///
75 /// The "command key" is the main modifier key used to issue commands in the
76 /// current platform. Specifically:
77 ///
78 /// - It is the `logo` or command key (⌘) on macOS
79 /// - It is the `control` key on other platforms
80 pub fn command(self) -> bool {
81 #[cfg(target_os = "macos")]
82 let is_pressed = self.logo();
83
84 #[cfg(not(target_os = "macos"))]
85 let is_pressed = self.control();
86
87 is_pressed
88 }
89
90 /// Returns true if the "jump key" is pressed in the [`Modifiers`].
91 ///
92 /// The "jump key" is the modifier key used to widen text motions. It is the `Alt`
93 /// key in macOS and the `Ctrl` key in other platforms.
94 pub fn jump(self) -> bool {
95 if cfg!(target_os = "macos") {
96 self.alt()
97 } else {
98 self.control()
99 }
100 }
101
102 /// Returns true if the "command key" is pressed on a macOS device.
103 ///
104 /// This is relevant for macOS-specific actions (e.g. `⌘ + ArrowLeft` moves the cursor
105 /// to the beginning of the line).
106 pub fn macos_command(self) -> bool {
107 if cfg!(target_os = "macos") {
108 self.logo()
109 } else {
110 false
111 }
112 }
113}