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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Human
// Copyright © 2020-2021 Jeron Aldaron Lau.
//
// Licensed under any of:
// - Apache License, Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0)
// - MIT License (https://mit-license.org/)
// - Boost Software License, Version 1.0 (https://www.boost.org/LICENSE_1_0.txt)
// At your choosing (See accompanying files LICENSE_APACHE_2_0.txt,
// LICENSE_MIT.txt and LICENSE_BOOST_1_0.txt).

use std::fmt::{Debug, Formatter, Result};

const MOD_SHIFT: u8 = 0b0000_0001;
const MOD_CTRL: u8 = 0b0000_0010;
const MOD_ALT: u8 = 0b0000_0100;

/// Modifier state.
#[repr(transparent)]
#[derive(Copy, Clone, Default)]
pub struct Mod(u8);

impl Mod {
    /// Check if no modifiers are held down.
    #[inline(always)]
    pub fn none(self) -> bool {
        self.0 == 0
    }

    /// Check if SHIFT is held down.
    #[inline(always)]
    pub fn shift(self) -> bool {
        (self.0 & MOD_SHIFT) != 0
    }

    /// Check if CTRL or CMD is held down.
    #[inline(always)]
    pub fn ctrl(self) -> bool {
        (self.0 & MOD_CTRL) != 0
    }

    /// Check if ALT or OPTION is held down.
    #[inline(always)]
    pub fn alt(self) -> bool {
        (self.0 & MOD_ALT) != 0
    }

    /// No modifiers.
    #[inline(always)]
    pub fn new() -> Self {
        Mod(0)
    }

    /// Add shift key.
    #[inline(always)]
    pub fn add_shift(self) -> Self {
        Self(self.0 | MOD_SHIFT)
    }

    /// Add control key.
    #[inline(always)]
    pub fn add_ctrl(self) -> Self {
        Self(self.0 | MOD_CTRL)
    }

    /// Add alt key.
    #[inline(always)]
    pub fn add_alt(self) -> Self {
        Self(self.0 | MOD_ALT)
    }
}

impl Debug for Mod {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        if self.ctrl() {
            if self.alt() {
                if self.shift() {
                    write!(f, "Ctrl + Alt + Shift")
                } else {
                    write!(f, "Ctrl + Alt")
                }
            } else {
                write!(f, "Ctrl")
            }
        } else if self.alt() {
            if self.shift() {
                write!(f, "Alt + Shift")
            } else {
                write!(f, "Alt")
            }
        } else if self.shift() {
            write!(f, "Shift")
        } else {
            write!(f, "None")
        }
    }
}

/// Input keycode for a key on a keyboard.
#[repr(u8)]
#[derive(Debug, Copy, Clone)]
#[non_exhaustive]
pub enum Key {
    /// Also known as the ESCAPE key.
    Back = 0x00u8,
    /// Numeric 1 on either top row or numpad.
    One = 0x01,
    /// Numeric 2 on either top row or numpad.
    Two = 0x02,
    /// Numeric 3 on either top row or numpad.
    Three = 0x03,
    /// Numeric 4 on either top row or numpad.
    Four = 0x04,
    /// Numeric 5 on either top row or numpad.
    Five = 0x05,
    /// Numeric 6 on either top row or numpad.
    Six = 0x06,
    /// Numeric 7 on either top row or numpad.
    Seven = 0x07,
    /// Numeric 8 on either top row or numpad.
    Eight = 0x08,
    /// Numeric 9 on either top row or numpad.
    Nine = 0x09,
    /// Numeric 0 on either top row or numpad.
    Zero = 0x0A,
    /// Minus / Underscore Key
    Minus = 0x0B,
    /// Equal Sign / Plus Key
    Equal = 0x0C,
    /// Backtick / Tilde Key
    Backtick = 0x0D,
    /// Tab
    Tab = 0x0E,
    /// Q (may be named by a different glyph depending on language of user).
    Q = 0x0F,
    /// W (may be named by a different glyph depending on language of user).
    W = 0x10,
    /// E (may be named by a different glyph depending on language of user).
    E = 0x11,
    /// R (may be named by a different glyph depending on language of user).
    R = 0x12,
    /// T (may be named by a different glyph depending on language of user).
    T = 0x13,
    /// Y (may be named by a different glyph depending on language of user).
    Y = 0x14,
    /// U (may be named by a different glyph depending on language of user).
    U = 0x15,
    /// I (may be named by a different glyph depending on language of user).
    I = 0x16,
    /// U (may be named by a different glyph depending on language of user).
    O = 0x17,
    /// I (may be named by a different glyph depending on language of user).
    P = 0x18,
    /// [ (may be named by a different glyph depending on language of user).
    BracketOpen = 0x19,
    /// ] (may be named by a different glyph depending on language of user).
    BracketClose = 0x1A,
    /// Backspace.
    Backspace = 0x1B,
    /// Env (Also known as: Win, Super, Cmd, Search) Key
    Env = 0x1C,
    /// A (may be named by a different glyph depending on language of user).
    A = 0x1D,
    /// S (may be named by a different glyph depending on language of user).
    S = 0x1E,
    /// D (may be named by a different glyph depending on language of user).
    D = 0x1F,
    /// F (may be named by a different glyph depending on language of user).
    F = 0x20,
    /// G (may be named by a different glyph depending on language of user).
    G = 0x21,
    /// H (may be named by a different glyph depending on language of user).
    H = 0x22,
    /// J (may be named by a different glyph depending on language of user).
    J = 0x23,
    /// K (may be named by a different glyph depending on language of user).
    K = 0x24,
    /// L (may be named by a different glyph depending on language of user).
    L = 0x25,
    /// ; (may be named by a different glyph depending on language of user).
    Semicolon = 0x26,
    /// ' (may be named by a different glyph depending on language of user).
    Apostrophe = 0x27,
    /// Enter (Also Return).
    Enter = 0x28,
    /// Left Shift Key
    LShift = 0x29,
    /// Z (may be named by a different glyph depending on language of user).
    Z = 0x2A,
    /// X (may be named by a different glyph depending on language of user).
    X = 0x2B,
    /// C (may be named by a different glyph depending on language of user).
    C = 0x2C,
    /// V (may be named by a different glyph depending on language of user).
    V = 0x2D,
    /// B (may be named by a different glyph depending on language of user).
    B = 0x2E,
    /// N (may be named by a different glyph depending on language of user).
    N = 0x2F,
    /// M (may be named by a different glyph depending on language of user).
    M = 0x30,
    /// , (may be named by a different glyph depending on language of user).
    Comma = 0x31,
    /// . (may be named by a different glyph depending on language of user).
    Period = 0x32,
    /// / (may be named by a different glyph depending on language of user).
    Slash = 0x33,
    /// \ (may be named by a different glyph depending on language of user).
    Backslash = 0x34,
    /// Up Arrow
    Up = 0x35,
    /// Right Shift Key
    RShift = 0x36,
    /// Left control
    LCtrl = 0x37,
    /// Left alt
    LAlt = 0x38,
    /// Space (or Left Thumb Button)
    Space = 0x39,
    /// Compose Key (Alt Gr, Right Thumb Button, NumLock, ScrLk Key)
    Compose = 0x3A,
    /// Right Alt
    RAlt = 0x3B,
    /// Right Control
    RCtrl = 0x3C,
    /// Left Arrow Key
    Left = 0x3D,
    /// Down Arrow Key
    Down = 0x3E,
    /// Right Arrow Key
    Right = 0x3F,
    
    // Back = 0x40u8,
    /// F1 Key
    F1 = 0x41,
    /// F2 Key
    F2 = 0x42,
    /// F3 Key
    F3 = 0x43,
    /// F4 Key
    F4 = 0x44,
    /// F5 Key
    F5 = 0x45,
    /// F6 Key
    F6 = 0x46,
    /// F7 Key
    F7 = 0x47,
    /// F8 Key
    F8 = 0x48,
    /// F9 Key
    F9 = 0x49,
    /// F10 Key
    F10 = 0x4A,
    /// F11 Key
    F11 = 0x4B,
    /// F12 Key
    F12 = 0x4C,
    /// Insert Key
    Insert = 0x4D,
    // Tab = 0x4E,
    // Q = 0x4F,
    // W = 0x50,
    // E = 0x51,
    // R = 0x52,
    // T = 0x53,
    // Y = 0x54,
    // U = 0x55,
    // I = 0x56,
    // O = 0x57,
    // P = 0x58,
    // BracketOpen = 0x19,
    // BracketClose = 0x1A,
    /// The delete key
    Delete = 0x5B,
    /// The Caps Lock Key
    CapsLock = 0x5C,
    // A = 0x1D,
    // S = 0x1E,
    // D = 0x1F,
    // F = 0x20,
    // G = 0x21,
    // H = 0x22,
    // J = 0x23,
    // K = 0x24,
    // L = 0x25,
    // Semicolon = 0x26,
    // Apostrophe = 0x27,
    // NumpadEnter = 0x28,
    // LShift = 0x29,
    // Z = 0x2A,
    // X = 0x2B,
    // C = 0x2C,
    // V = 0x2D,
    // B = 0x2E,
    // N = 0x2F,
    // M = 0x30,
    // Comma = 0x31,
    // Period = 0x32,
    // Slash = 0x33,
    // Backslash = 0x34,
    /// Page Up
    PageUp = 0x75,
    // RShift = 0x36,
    // LCtrl = 0x37,
    // LAlt = 0x38,
    /// Pause Key
    Pause = 0x79,
    // Compose = 0x3A,
    // RAlt = 0x3B,
    /// Context Menu
    Menu = 0x7C,
    /// Home
    Home = 0x7D,
    /// Page Down
    PageDown = 0x7E,
    /// End
    End = 0x7F,
}