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