1use crate::context::Context;
2
3#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
4#[cfg_attr(
5 feature = "serde_support",
6 derive(serde::Serialize, serde::Deserialize)
7)]
8#[allow(missing_docs)]
9pub enum Key {
16 A,
17 B,
18 C,
19 D,
20 E,
21 F,
22 G,
23 H,
24 I,
25 J,
26 K,
27 L,
28 M,
29 N,
30 O,
31 P,
32 Q,
33 R,
34 S,
35 T,
36 U,
37 V,
38 W,
39 X,
40 Y,
41 Z,
42
43 Num0,
44 Num1,
45 Num2,
46 Num3,
47 Num4,
48 Num5,
49 Num6,
50 Num7,
51 Num8,
52 Num9,
53
54 F1,
55 F2,
56 F3,
57 F4,
58 F5,
59 F6,
60 F7,
61 F8,
62 F9,
63 F10,
64 F11,
65 F12,
66 F13,
67 F14,
68 F15,
69 F16,
70 F17,
71 F18,
72 F19,
73 F20,
74 F21,
75 F22,
76 F23,
77 F24,
78
79 NumLock,
80 NumPad1,
81 NumPad2,
82 NumPad3,
83 NumPad4,
84 NumPad5,
85 NumPad6,
86 NumPad7,
87 NumPad8,
88 NumPad9,
89 NumPad0,
90 NumPadPlus,
91 NumPadMinus,
92 NumPadMultiply,
93 NumPadDivide,
94 NumPadEnter,
95
96 LeftCtrl,
97 LeftShift,
98 LeftAlt,
99 RightCtrl,
100 RightShift,
101 RightAlt,
102
103 Up,
104 Down,
105 Left,
106 Right,
107
108 Ampersand,
109 Asterisk,
110 At,
111 Backquote,
112 Backslash,
113 Backspace,
114 CapsLock,
115 Caret,
116 Colon,
117 Comma,
118 Delete,
119 Dollar,
120 DoubleQuote,
121 End,
122 Enter,
123 Equals,
124 Escape,
125 Exclaim,
126 GreaterThan,
127 Hash,
128 Home,
129 Insert,
130 LeftBracket,
131 LeftParen,
132 LessThan,
133 Minus,
134 PageDown,
135 PageUp,
136 Pause,
137 Percent,
138 Period,
139 Plus,
140 PrintScreen,
141 Question,
142 Quote,
143 RightBracket,
144 RightParen,
145 ScrollLock,
146 Semicolon,
147 Slash,
148 Space,
149 Tab,
150 Underscore,
151}
152
153#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
163#[cfg_attr(
164 feature = "serde_support",
165 derive(serde::Serialize, serde::Deserialize)
166)]
167#[allow(missing_docs)]
168pub enum KeyModifier {
169 Ctrl,
170 Alt,
171 Shift,
172}
173
174pub fn is_key_down(ctx: &Context, key: Key) -> bool {
176 ctx.input.keys_down.contains(&key)
177}
178
179pub fn is_key_up(ctx: &Context, key: Key) -> bool {
181 !ctx.input.keys_down.contains(&key)
182}
183
184pub fn is_key_pressed(ctx: &Context, key: Key) -> bool {
186 ctx.input.keys_pressed.contains(&key)
187}
188
189pub fn is_key_released(ctx: &Context, key: Key) -> bool {
191 ctx.input.keys_released.contains(&key)
192}
193
194pub fn is_key_modifier_down(ctx: &Context, key_modifier: KeyModifier) -> bool {
196 let (a, b) = get_modifier_keys(key_modifier);
197
198 is_key_down(ctx, a) || is_key_down(ctx, b)
199}
200
201pub fn is_key_modifier_up(ctx: &Context, key_modifier: KeyModifier) -> bool {
203 let (a, b) = get_modifier_keys(key_modifier);
204
205 is_key_up(ctx, a) && is_key_up(ctx, b)
206}
207
208pub fn get_keys_down(ctx: &Context) -> impl Iterator<Item = &Key> {
210 ctx.input.keys_down.iter()
211}
212
213pub fn get_keys_pressed(ctx: &Context) -> impl Iterator<Item = &Key> {
215 ctx.input.keys_pressed.iter()
216}
217
218pub fn get_keys_released(ctx: &Context) -> impl Iterator<Item = &Key> {
220 ctx.input.keys_released.iter()
221}
222
223pub(crate) fn set_key_down(ctx: &mut Context, key: Key) -> bool {
224 let was_up = ctx.input.keys_down.insert(key);
225
226 if was_up {
227 ctx.input.keys_pressed.insert(key);
228 }
229
230 was_up
231}
232
233pub(crate) fn set_key_up(ctx: &mut Context, key: Key) -> bool {
234 let was_down = ctx.input.keys_down.remove(&key);
235
236 if was_down {
237 ctx.input.keys_released.insert(key);
238 }
239
240 was_down
241}
242
243pub(crate) fn get_modifier_keys(key_modifier: KeyModifier) -> (Key, Key) {
244 match key_modifier {
245 KeyModifier::Ctrl => (Key::LeftCtrl, Key::RightCtrl),
246 KeyModifier::Alt => (Key::LeftAlt, Key::RightAlt),
247 KeyModifier::Shift => (Key::LeftShift, Key::RightShift),
248 }
249}