1#![allow(clippy::trivially_copy_pass_by_ref)]
20
21use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not};
22
23pub use keyboard_types::{Code, KeyState, Location};
24
25pub type KbKey = keyboard_types::Key;
27
28#[non_exhaustive]
35#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
36pub struct KeyEvent {
37 pub state: KeyState,
39 pub key: KbKey,
41 pub code: Code,
43 pub location: Location,
45 pub mods: Modifiers,
47 pub repeat: bool,
49 pub is_composing: bool,
52}
53
54#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
62pub struct Modifiers(keyboard_types::Modifiers);
63
64pub trait IntoKey {
73 fn into_key(self) -> KbKey;
74}
75
76impl KeyEvent {
77 #[doc(hidden)]
78 pub fn for_test(mods: impl Into<Modifiers>, key: impl IntoKey) -> KeyEvent {
80 let mods = mods.into();
81 let key = key.into_key();
82 KeyEvent {
83 key,
84 code: Code::Unidentified,
85 location: Location::Standard,
86 state: KeyState::Down,
87 mods,
88 is_composing: false,
89 repeat: false,
90 }
91 }
92}
93
94impl Modifiers {
95 pub const ALT: Modifiers = Modifiers(keyboard_types::Modifiers::ALT);
96 pub const ALT_GRAPH: Modifiers = Modifiers(keyboard_types::Modifiers::ALT_GRAPH);
97 pub const CAPS_LOCK: Modifiers = Modifiers(keyboard_types::Modifiers::CAPS_LOCK);
98 pub const CONTROL: Modifiers = Modifiers(keyboard_types::Modifiers::CONTROL);
99 pub const FN: Modifiers = Modifiers(keyboard_types::Modifiers::FN);
100 pub const FN_LOCK: Modifiers = Modifiers(keyboard_types::Modifiers::FN_LOCK);
101 pub const META: Modifiers = Modifiers(keyboard_types::Modifiers::META);
102 pub const NUM_LOCK: Modifiers = Modifiers(keyboard_types::Modifiers::NUM_LOCK);
103 pub const SCROLL_LOCK: Modifiers = Modifiers(keyboard_types::Modifiers::SCROLL_LOCK);
104 pub const SHIFT: Modifiers = Modifiers(keyboard_types::Modifiers::SHIFT);
105 pub const SYMBOL: Modifiers = Modifiers(keyboard_types::Modifiers::SYMBOL);
106 pub const SYMBOL_LOCK: Modifiers = Modifiers(keyboard_types::Modifiers::SYMBOL_LOCK);
107 pub const HYPER: Modifiers = Modifiers(keyboard_types::Modifiers::HYPER);
108 pub const SUPER: Modifiers = Modifiers(keyboard_types::Modifiers::SUPER);
109
110 pub fn raw(&self) -> keyboard_types::Modifiers {
114 self.0
115 }
116
117 pub fn shift(&self) -> bool {
119 self.contains(Modifiers::SHIFT)
120 }
121
122 pub fn ctrl(&self) -> bool {
124 self.contains(Modifiers::CONTROL)
125 }
126
127 pub fn alt(&self) -> bool {
129 self.contains(Modifiers::ALT)
130 }
131
132 pub fn meta(&self) -> bool {
134 self.contains(Modifiers::META)
135 }
136
137 pub fn empty() -> Modifiers {
139 Default::default()
140 }
141
142 pub fn is_empty(&self) -> bool {
144 self.0.is_empty()
145 }
146
147 pub fn contains(&self, other: Modifiers) -> bool {
149 self.0.contains(other.0)
150 }
151
152 pub fn set(&mut self, other: Modifiers, value: bool) {
154 self.0.set(other.0, value)
155 }
156}
157
158impl BitAnd for Modifiers {
159 type Output = Self;
160
161 fn bitand(self, rhs: Self) -> Self {
162 Modifiers(self.0 & rhs.0)
163 }
164}
165
166impl BitAndAssign for Modifiers {
167 fn bitand_assign(&mut self, rhs: Self) {
169 *self = Modifiers(self.0 & rhs.0)
170 }
171}
172
173impl BitOr for Modifiers {
174 type Output = Self;
175
176 fn bitor(self, rhs: Self) -> Self {
177 Modifiers(self.0 | rhs.0)
178 }
179}
180
181impl BitOrAssign for Modifiers {
182 fn bitor_assign(&mut self, rhs: Self) {
184 *self = Modifiers(self.0 | rhs.0)
185 }
186}
187
188impl BitXor for Modifiers {
189 type Output = Self;
190
191 fn bitxor(self, rhs: Self) -> Self {
192 Modifiers(self.0 ^ rhs.0)
193 }
194}
195
196impl BitXorAssign for Modifiers {
197 fn bitxor_assign(&mut self, rhs: Self) {
199 *self = Modifiers(self.0 ^ rhs.0)
200 }
201}
202
203impl Not for Modifiers {
204 type Output = Self;
205
206 fn not(self) -> Self {
207 Modifiers(!self.0)
208 }
209}
210
211impl IntoKey for KbKey {
212 fn into_key(self) -> KbKey {
213 self
214 }
215}
216
217impl IntoKey for &str {
218 fn into_key(self) -> KbKey {
219 KbKey::Character(self.into())
220 }
221}