1
//! Scancode -> [`PhysicalKey`] tables, one per platform convention.
2
//!
3
//! `ScanCode` already carried the physical key as a raw `u32` on every
4
//! backend, so nothing new has to be plumbed to fill
5
//! `KeyboardState.current_physical_key` — what was missing is the tables that
6
//! turn a platform-specific number into a name an application can match on.
7
//!
8
//! Three conventions cover every backend the engine has:
9
//!
10
//! * **evdev** (`from_evdev`) — Wayland's `wl_keyboard.key` IS an evdev code, X11 keycodes are
11
//!   `evdev + 8`, and Android's `KeyEvent` scan code is evdev too. One table serves all three.
12
//! * **PS/2 set 1** (`from_windows_scancode`) — what `WM_KEYDOWN`'s `lParam` carries, where the
13
//!   `E0` prefix (bit 24 of `lParam`) is what separates NumpadEnter from Enter, ControlRight from
14
//!   ControlLeft, and the arrow cluster from the numpad.
15
//! * **Carbon virtual keycodes** (`from_macos_keycode`) — `NSEvent.keyCode`. These are POSITIONAL
16
//!   despite the name; `kVK_ANSI_A` is the position, not the letter.
17
//!
18
//! Every table returns [`PhysicalKey::Unidentified`] for a code it does not
19
//! name, which is a value the enum carries precisely so that an unknown key is
20
//! reported honestly rather than guessed at or dropped.
21

            
22
use crate::window::PhysicalKey;
23

            
24
impl PhysicalKey {
25
    /// Linux evdev keycode -> position.
26
    ///
27
    /// Used directly by Wayland and Android. X11 callers must pass
28
    /// `keycode - 8`; see [`Self::from_x11_keycode`].
29
    #[must_use]
30
    // A transcription of the evdev code table, in code order. Distinct codes do
31
    // map to one key (95 and 121 are both NumpadComma); reordering them into
32
    // or-patterns would stop this reading like the table it mirrors.
33
    #[allow(clippy::match_same_arms)]
34
    // One arm per hardware code, and the code IS the documentation: collapsing
35
    // arms to get under the line count would hide which scancodes map where,
36
    // which is the only thing this table says.
37
    #[allow(clippy::too_many_lines)]
38
50
    pub const fn from_evdev(code: u32) -> Self {
39
        use PhysicalKey::{
40
            AltLeft, AltRight, ArrowDown, ArrowLeft, ArrowRight, ArrowUp, Backquote, Backslash,
41
            Backspace, BracketLeft, BracketRight, CapsLock, Comma, ContextMenu, ControlLeft,
42
            ControlRight, Convert, Delete, Digit0, Digit1, Digit2, Digit3, Digit4, Digit5, Digit6,
43
            Digit7, Digit8, Digit9, End, Enter, Equal, Escape, Home, Insert, IntlBackslash, IntlRo,
44
            IntlYen, KanaMode, KeyA, KeyB, KeyC, KeyD, KeyE, KeyF, KeyG, KeyH, KeyI, KeyJ, KeyK,
45
            KeyL, KeyM, KeyN, KeyO, KeyP, KeyQ, KeyR, KeyS, KeyT, KeyU, KeyV, KeyW, KeyX, KeyY,
46
            KeyZ, Lang1, Lang2, MetaLeft, MetaRight, Minus, NonConvert, NumLock, Numpad0, Numpad1,
47
            Numpad2, Numpad3, Numpad4, Numpad5, Numpad6, Numpad7, Numpad8, Numpad9, NumpadAdd,
48
            NumpadComma, NumpadDecimal, NumpadDivide, NumpadEnter, NumpadEqual, NumpadMultiply,
49
            NumpadSubtract, PageDown, PageUp, Pause, Period, PrintScreen, Quote, ScrollLock,
50
            Semicolon, ShiftLeft, ShiftRight, Slash, Space, Tab, Unidentified, F1, F10, F11, F12,
51
            F13, F14, F15, F16, F17, F18, F19, F2, F20, F21, F22, F23, F24, F3, F4, F5, F6, F7, F8,
52
            F9,
53
        };
54
50
        match code {
55
3
            1 => Escape,
56
2
            2 => Digit1,
57
            3 => Digit2,
58
            4 => Digit3,
59
            5 => Digit4,
60
            6 => Digit5,
61
            7 => Digit6,
62
            8 => Digit7,
63
            9 => Digit8,
64
            10 => Digit9,
65
2
            11 => Digit0,
66
            12 => Minus,
67
            13 => Equal,
68
2
            14 => Backspace,
69
2
            15 => Tab,
70
            16 => KeyQ,
71
2
            17 => KeyW,
72
            18 => KeyE,
73
            19 => KeyR,
74
            20 => KeyT,
75
            21 => KeyY,
76
            22 => KeyU,
77
            23 => KeyI,
78
            24 => KeyO,
79
            25 => KeyP,
80
            26 => BracketLeft,
81
            27 => BracketRight,
82
2
            28 => Enter,
83
2
            29 => ControlLeft,
84
2
            30 => KeyA,
85
            31 => KeyS,
86
            32 => KeyD,
87
            33 => KeyF,
88
            34 => KeyG,
89
            35 => KeyH,
90
            36 => KeyJ,
91
            37 => KeyK,
92
            38 => KeyL,
93
            39 => Semicolon,
94
            40 => Quote,
95
2
            41 => Backquote,
96
2
            42 => ShiftLeft,
97
            43 => Backslash,
98
2
            44 => KeyZ,
99
            45 => KeyX,
100
            46 => KeyC,
101
            47 => KeyV,
102
            48 => KeyB,
103
            49 => KeyN,
104
            50 => KeyM,
105
2
            51 => Comma,
106
            52 => Period,
107
2
            53 => Slash,
108
2
            54 => ShiftRight,
109
            55 => NumpadMultiply,
110
            56 => AltLeft,
111
2
            57 => Space,
112
2
            58 => CapsLock,
113
2
            59 => F1,
114
            60 => F2,
115
            61 => F3,
116
            62 => F4,
117
            63 => F5,
118
            64 => F6,
119
            65 => F7,
120
            66 => F8,
121
            67 => F9,
122
            68 => F10,
123
            69 => NumLock,
124
            70 => ScrollLock,
125
            71 => Numpad7,
126
            72 => Numpad8,
127
            73 => Numpad9,
128
            74 => NumpadSubtract,
129
            75 => Numpad4,
130
            76 => Numpad5,
131
            77 => Numpad6,
132
2
            78 => NumpadAdd,
133
            79 => Numpad1,
134
            80 => Numpad2,
135
            81 => Numpad3,
136
2
            82 => Numpad0,
137
            83 => NumpadDecimal,
138
            // 86 is KEY_102ND, the extra key ISO boards have next to left
139
            // shift that ANSI boards do not.
140
2
            86 => IntlBackslash,
141
            87 => F11,
142
2
            88 => F12,
143
            89 => IntlRo,
144
            92 => Convert,
145
            93 => KanaMode,
146
            94 => NonConvert,
147
            95 => NumpadComma,
148
            96 => NumpadEnter,
149
2
            97 => ControlRight,
150
            98 => NumpadDivide,
151
            99 => PrintScreen,
152
            100 => AltRight,
153
            102 => Home,
154
            103 => ArrowUp,
155
            104 => PageUp,
156
            105 => ArrowLeft,
157
            106 => ArrowRight,
158
            107 => End,
159
            108 => ArrowDown,
160
            109 => PageDown,
161
            110 => Insert,
162
            111 => Delete,
163
            117 => NumpadEqual,
164
            119 => Pause,
165
            121 => NumpadComma,
166
            122 => Lang1,
167
            123 => Lang2,
168
            124 => IntlYen,
169
            125 => MetaLeft,
170
            126 => MetaRight,
171
            127 => ContextMenu,
172
            183 => F13,
173
            184 => F14,
174
            185 => F15,
175
            186 => F16,
176
            187 => F17,
177
            188 => F18,
178
            189 => F19,
179
            190 => F20,
180
            191 => F21,
181
            192 => F22,
182
            193 => F23,
183
            194 => F24,
184
3
            _ => Unidentified,
185
        }
186
50
    }
187

            
188
    /// X11 keycode -> position.
189
    ///
190
    /// XKB keycodes are `evdev + 8` by protocol, so this is the evdev table
191
    /// with the offset removed. A keycode below 8 cannot be an evdev key and
192
    /// is reported as unidentified rather than wrapping into a wrong one.
193
    #[must_use]
194
31
    pub const fn from_x11_keycode(keycode: u32) -> Self {
195
31
        if keycode < 8 {
196
8
            return Self::Unidentified;
197
23
        }
198
23
        Self::from_evdev(keycode - 8)
199
31
    }
200

            
201
    /// PS/2 set-1 scancode -> position, as `WM_KEYDOWN` reports it.
202
    ///
203
    /// `extended` is `lParam` bit 24 (the `E0` prefix). It is not optional
204
    /// detail: without it Enter and `NumpadEnter`, `ControlLeft` and `ControlRight`,
205
    /// and the whole arrow cluster versus the numpad are the SAME scancode.
206
    #[must_use]
207
    // One arm per hardware code, and the code IS the documentation: collapsing
208
    // arms to get under the line count would hide which scancodes map where,
209
    // which is the only thing this table says.
210
    #[allow(clippy::too_many_lines)]
211
47
    pub const fn from_windows_scancode(scancode: u32, extended: bool) -> Self {
212
        use PhysicalKey::{
213
            AltLeft, AltRight, ArrowDown, ArrowLeft, ArrowRight, ArrowUp, Backquote, Backslash,
214
            Backspace, BracketLeft, BracketRight, CapsLock, Comma, ContextMenu, ControlLeft,
215
            ControlRight, Convert, Delete, Digit0, Digit1, Digit2, Digit3, Digit4, Digit5, Digit6,
216
            Digit7, Digit8, Digit9, End, Enter, Equal, Escape, Home, Insert, IntlBackslash, IntlRo,
217
            IntlYen, KanaMode, KeyA, KeyB, KeyC, KeyD, KeyE, KeyF, KeyG, KeyH, KeyI, KeyJ, KeyK,
218
            KeyL, KeyM, KeyN, KeyO, KeyP, KeyQ, KeyR, KeyS, KeyT, KeyU, KeyV, KeyW, KeyX, KeyY,
219
            KeyZ, MetaLeft, MetaRight, Minus, NonConvert, NumLock, Numpad0, Numpad1, Numpad2,
220
            Numpad3, Numpad4, Numpad5, Numpad6, Numpad7, Numpad8, Numpad9, NumpadAdd,
221
            NumpadDecimal, NumpadDivide, NumpadEnter, NumpadEqual, NumpadMultiply, NumpadSubtract,
222
            PageDown, PageUp, Pause, Period, PrintScreen, Quote, ScrollLock, Semicolon, ShiftLeft,
223
            ShiftRight, Slash, Space, Tab, Unidentified, F1, F10, F11, F12, F13, F14, F15, F16,
224
            F17, F18, F19, F2, F20, F21, F22, F23, F24, F3, F4, F5, F6, F7, F8, F9,
225
        };
226
47
        if extended {
227
13
            return match scancode {
228
1
                0x1C => NumpadEnter,
229
1
                0x1D => ControlRight,
230
1
                0x35 => NumpadDivide,
231
                0x37 => PrintScreen,
232
1
                0x38 => AltRight,
233
1
                0x45 => Pause,
234
1
                0x47 => Home,
235
1
                0x48 => ArrowUp,
236
                0x49 => PageUp,
237
1
                0x4B => ArrowLeft,
238
1
                0x4D => ArrowRight,
239
                0x4F => End,
240
1
                0x50 => ArrowDown,
241
                0x51 => PageDown,
242
1
                0x52 => Insert,
243
1
                0x53 => Delete,
244
                0x5B => MetaLeft,
245
                0x5C => MetaRight,
246
                0x5D => ContextMenu,
247
1
                _ => Unidentified,
248
            };
249
34
        }
250
34
        match scancode {
251
1
            0x01 => Escape,
252
1
            0x02 => Digit1,
253
            0x03 => Digit2,
254
            0x04 => Digit3,
255
            0x05 => Digit4,
256
            0x06 => Digit5,
257
            0x07 => Digit6,
258
            0x08 => Digit7,
259
            0x09 => Digit8,
260
            0x0A => Digit9,
261
1
            0x0B => Digit0,
262
            0x0C => Minus,
263
            0x0D => Equal,
264
1
            0x0E => Backspace,
265
1
            0x0F => Tab,
266
            0x10 => KeyQ,
267
1
            0x11 => KeyW,
268
            0x12 => KeyE,
269
            0x13 => KeyR,
270
            0x14 => KeyT,
271
            0x15 => KeyY,
272
            0x16 => KeyU,
273
            0x17 => KeyI,
274
            0x18 => KeyO,
275
            0x19 => KeyP,
276
            0x1A => BracketLeft,
277
            0x1B => BracketRight,
278
2
            0x1C => Enter,
279
1
            0x1D => ControlLeft,
280
1
            0x1E => KeyA,
281
            0x1F => KeyS,
282
            0x20 => KeyD,
283
            0x21 => KeyF,
284
            0x22 => KeyG,
285
            0x23 => KeyH,
286
            0x24 => KeyJ,
287
            0x25 => KeyK,
288
            0x26 => KeyL,
289
            0x27 => Semicolon,
290
            0x28 => Quote,
291
1
            0x29 => Backquote,
292
1
            0x2A => ShiftLeft,
293
            0x2B => Backslash,
294
1
            0x2C => KeyZ,
295
            0x2D => KeyX,
296
            0x2E => KeyC,
297
            0x2F => KeyV,
298
            0x30 => KeyB,
299
            0x31 => KeyN,
300
            0x32 => KeyM,
301
1
            0x33 => Comma,
302
            0x34 => Period,
303
2
            0x35 => Slash,
304
1
            0x36 => ShiftRight,
305
            0x37 => NumpadMultiply,
306
1
            0x38 => AltLeft,
307
1
            0x39 => Space,
308
1
            0x3A => CapsLock,
309
1
            0x3B => F1,
310
            0x3C => F2,
311
            0x3D => F3,
312
            0x3E => F4,
313
            0x3F => F5,
314
            0x40 => F6,
315
            0x41 => F7,
316
            0x42 => F8,
317
            0x43 => F9,
318
            0x44 => F10,
319
1
            0x45 => NumLock,
320
            0x46 => ScrollLock,
321
1
            0x47 => Numpad7,
322
1
            0x48 => Numpad8,
323
            0x49 => Numpad9,
324
            0x4A => NumpadSubtract,
325
1
            0x4B => Numpad4,
326
            0x4C => Numpad5,
327
1
            0x4D => Numpad6,
328
1
            0x4E => NumpadAdd,
329
            0x4F => Numpad1,
330
1
            0x50 => Numpad2,
331
            0x51 => Numpad3,
332
2
            0x52 => Numpad0,
333
1
            0x53 => NumpadDecimal,
334
1
            0x56 => IntlBackslash,
335
            0x57 => F11,
336
1
            0x58 => F12,
337
            0x59 => NumpadEqual,
338
            0x64 => F13,
339
            0x65 => F14,
340
            0x66 => F15,
341
            0x67 => F16,
342
            0x68 => F17,
343
            0x69 => F18,
344
            0x6A => F19,
345
            0x6B => F20,
346
            0x6C => F21,
347
            0x6D => F22,
348
            0x6E => F23,
349
            0x76 => F24,
350
            0x70 => KanaMode,
351
            0x73 => IntlRo,
352
            0x79 => Convert,
353
            0x7B => NonConvert,
354
            0x7D => IntlYen,
355
1
            _ => Unidentified,
356
        }
357
47
    }
358

            
359
    /// Carbon virtual keycode (`NSEvent.keyCode`) -> position.
360
    ///
361
    /// Cross-checked entry by entry against
362
    /// `macos_keycode_to_virtual_key`, the table this codebase already
363
    /// trusts for the LOGICAL key; the two agree on every code both name.
364
    #[must_use]
365
    // One arm per hardware code, and the code IS the documentation: collapsing
366
    // arms to get under the line count would hide which scancodes map where,
367
    // which is the only thing this table says.
368
    #[allow(clippy::too_many_lines)]
369
25
    pub const fn from_macos_keycode(keycode: u16) -> Self {
370
        use PhysicalKey::{
371
            AltLeft, AltRight, ArrowDown, ArrowLeft, ArrowRight, ArrowUp, Backquote, Backslash,
372
            Backspace, BracketLeft, BracketRight, CapsLock, Comma, ContextMenu, ControlLeft,
373
            ControlRight, Delete, Digit0, Digit1, Digit2, Digit3, Digit4, Digit5, Digit6, Digit7,
374
            Digit8, Digit9, End, Enter, Equal, Escape, Home, Insert, IntlBackslash, IntlRo,
375
            IntlYen, KeyA, KeyB, KeyC, KeyD, KeyE, KeyF, KeyG, KeyH, KeyI, KeyJ, KeyK, KeyL, KeyM,
376
            KeyN, KeyO, KeyP, KeyQ, KeyR, KeyS, KeyT, KeyU, KeyV, KeyW, KeyX, KeyY, KeyZ, Lang1,
377
            Lang2, MetaLeft, MetaRight, Minus, NumLock, Numpad0, Numpad1, Numpad2, Numpad3,
378
            Numpad4, Numpad5, Numpad6, Numpad7, Numpad8, Numpad9, NumpadAdd, NumpadComma,
379
            NumpadDecimal, NumpadDivide, NumpadEnter, NumpadEqual, NumpadMultiply, NumpadSubtract,
380
            PageDown, PageUp, Period, Quote, Semicolon, ShiftLeft, ShiftRight, Slash, Space, Tab,
381
            Unidentified, F1, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F2, F20, F3, F4,
382
            F5, F6, F7, F8, F9,
383
        };
384
25
        match keycode {
385
1
            0x00 => KeyA,
386
            0x01 => KeyS,
387
            0x02 => KeyD,
388
            0x03 => KeyF,
389
            0x04 => KeyH,
390
            0x05 => KeyG,
391
1
            0x06 => KeyZ,
392
            0x07 => KeyX,
393
            0x08 => KeyC,
394
            0x09 => KeyV,
395
            // kVK_ISO_Section — the extra ISO key, absent on ANSI boards.
396
1
            0x0A => IntlBackslash,
397
            0x0B => KeyB,
398
            0x0C => KeyQ,
399
1
            0x0D => KeyW,
400
            0x0E => KeyE,
401
            0x0F => KeyR,
402
            0x10 => KeyY,
403
            0x11 => KeyT,
404
1
            0x12 => Digit1,
405
            0x13 => Digit2,
406
            0x14 => Digit3,
407
            0x15 => Digit4,
408
            0x16 => Digit6,
409
            0x17 => Digit5,
410
            0x18 => Equal,
411
            0x19 => Digit9,
412
            0x1A => Digit7,
413
            0x1B => Minus,
414
            0x1C => Digit8,
415
1
            0x1D => Digit0,
416
            0x1E => BracketRight,
417
            0x1F => KeyO,
418
            0x20 => KeyU,
419
            0x21 => BracketLeft,
420
            0x22 => KeyI,
421
            0x23 => KeyP,
422
1
            0x24 => Enter,
423
            0x25 => KeyL,
424
            0x26 => KeyJ,
425
            0x27 => Quote,
426
            0x28 => KeyK,
427
            0x29 => Semicolon,
428
            0x2A => Backslash,
429
1
            0x2B => Comma,
430
1
            0x2C => Slash,
431
            0x2D => KeyN,
432
            0x2E => KeyM,
433
            0x2F => Period,
434
1
            0x30 => Tab,
435
1
            0x31 => Space,
436
1
            0x32 => Backquote,
437
1
            0x33 => Backspace,
438
1
            0x35 => Escape,
439
1
            0x36 => MetaRight,
440
1
            0x37 => MetaLeft,
441
1
            0x38 => ShiftLeft,
442
1
            0x39 => CapsLock,
443
            0x3A => AltLeft,
444
            0x3B => ControlLeft,
445
1
            0x3C => ShiftRight,
446
            0x3D => AltRight,
447
            0x3E => ControlRight,
448
            // 0x3F is kVK_Function (the `fn` key), which has no W3C `code`.
449
            0x40 => F17,
450
            0x41 => NumpadDecimal,
451
            0x43 => NumpadMultiply,
452
1
            0x45 => NumpadAdd,
453
            // kVK_ANSI_KeypadClear sits where NumLock does on a PC board.
454
            0x47 => NumLock,
455
            0x4B => NumpadDivide,
456
            0x4C => NumpadEnter,
457
            0x4E => NumpadSubtract,
458
            0x4F => F18,
459
            0x50 => F19,
460
            0x51 => NumpadEqual,
461
1
            0x52 => Numpad0,
462
            0x53 => Numpad1,
463
            0x54 => Numpad2,
464
            0x55 => Numpad3,
465
            0x56 => Numpad4,
466
            0x57 => Numpad5,
467
            0x58 => Numpad6,
468
            0x59 => Numpad7,
469
            0x5A => F20,
470
            0x5B => Numpad8,
471
            0x5C => Numpad9,
472
            0x5D => IntlYen,
473
            0x5E => IntlRo,
474
            0x5F => NumpadComma,
475
            0x60 => F5,
476
            0x61 => F6,
477
            0x62 => F7,
478
            0x63 => F3,
479
            0x64 => F8,
480
            0x65 => F9,
481
            0x66 => Lang2,
482
            0x67 => F11,
483
            0x68 => Lang1,
484
            0x69 => F13,
485
            0x6A => F16,
486
            0x6B => F14,
487
            0x6D => F10,
488
            0x6E => ContextMenu,
489
1
            0x6F => F12,
490
            0x71 => F15,
491
            0x72 => Insert,
492
            0x73 => Home,
493
            0x74 => PageUp,
494
            0x75 => Delete,
495
            0x76 => F4,
496
            0x77 => End,
497
            0x78 => F2,
498
            0x79 => PageDown,
499
1
            0x7A => F1,
500
            0x7B => ArrowLeft,
501
            0x7C => ArrowRight,
502
            0x7D => ArrowDown,
503
            0x7E => ArrowUp,
504
2
            _ => Unidentified,
505
        }
506
25
    }
507
}
508

            
509
#[cfg(test)]
510
mod tests {
511
    use super::*;
512

            
513
    /// The same physical key must get the same name from every platform's
514
    /// table — that is the entire point of the enum, and a table typo would
515
    /// otherwise only show up as a wrong binding on one OS.
516
    #[test]
517
1
    fn the_same_position_gets_the_same_name_on_every_platform() {
518
        // (name, evdev, windows set-1, macOS Carbon)
519
1
        let cases: &[(PhysicalKey, u32, u32, u16)] = &[
520
1
            (PhysicalKey::KeyA, 30, 0x1E, 0x00),
521
1
            (PhysicalKey::KeyW, 17, 0x11, 0x0D),
522
1
            (PhysicalKey::KeyZ, 44, 0x2C, 0x06),
523
1
            (PhysicalKey::Digit1, 2, 0x02, 0x12),
524
1
            (PhysicalKey::Digit0, 11, 0x0B, 0x1D),
525
1
            (PhysicalKey::Space, 57, 0x39, 0x31),
526
1
            (PhysicalKey::Enter, 28, 0x1C, 0x24),
527
1
            (PhysicalKey::Tab, 15, 0x0F, 0x30),
528
1
            (PhysicalKey::Escape, 1, 0x01, 0x35),
529
1
            (PhysicalKey::Backspace, 14, 0x0E, 0x33),
530
1
            (PhysicalKey::ShiftLeft, 42, 0x2A, 0x38),
531
1
            (PhysicalKey::ShiftRight, 54, 0x36, 0x3C),
532
1
            (PhysicalKey::CapsLock, 58, 0x3A, 0x39),
533
1
            (PhysicalKey::F1, 59, 0x3B, 0x7A),
534
1
            (PhysicalKey::F12, 88, 0x58, 0x6F),
535
1
            (PhysicalKey::Numpad0, 82, 0x52, 0x52),
536
1
            (PhysicalKey::NumpadAdd, 78, 0x4E, 0x45),
537
1
            (PhysicalKey::Comma, 51, 0x33, 0x2B),
538
1
            (PhysicalKey::Slash, 53, 0x35, 0x2C),
539
1
            (PhysicalKey::Backquote, 41, 0x29, 0x32),
540
1
            (PhysicalKey::IntlBackslash, 86, 0x56, 0x0A),
541
1
        ];
542
22
        for (want, evdev, win, mac) in cases {
543
21
            assert_eq!(PhysicalKey::from_evdev(*evdev), *want, "evdev {evdev}");
544
21
            assert_eq!(
545
21
                PhysicalKey::from_windows_scancode(*win, false),
546
                *want,
547
                "windows {win:#04X}",
548
            );
549
21
            assert_eq!(
550
21
                PhysicalKey::from_macos_keycode(*mac),
551
                *want,
552
                "macos {mac:#04X}"
553
            );
554
            // X11 is the evdev table shifted by the protocol's +8.
555
21
            assert_eq!(
556
21
                PhysicalKey::from_x11_keycode(evdev + 8),
557
                *want,
558
                "x11 {}",
559
                evdev + 8
560
            );
561
        }
562
1
    }
563

            
564
    /// Without the `E0` prefix these pairs are the SAME scancode, so dropping
565
    /// `extended` would silently merge them.
566
    #[test]
567
1
    fn the_windows_extended_bit_separates_the_duplicated_scancodes() {
568
1
        let pairs: &[(u32, PhysicalKey, PhysicalKey)] = &[
569
1
            (0x1C, PhysicalKey::Enter, PhysicalKey::NumpadEnter),
570
1
            (0x1D, PhysicalKey::ControlLeft, PhysicalKey::ControlRight),
571
1
            (0x35, PhysicalKey::Slash, PhysicalKey::NumpadDivide),
572
1
            (0x38, PhysicalKey::AltLeft, PhysicalKey::AltRight),
573
1
            (0x45, PhysicalKey::NumLock, PhysicalKey::Pause),
574
1
            (0x47, PhysicalKey::Numpad7, PhysicalKey::Home),
575
1
            (0x48, PhysicalKey::Numpad8, PhysicalKey::ArrowUp),
576
1
            (0x4B, PhysicalKey::Numpad4, PhysicalKey::ArrowLeft),
577
1
            (0x4D, PhysicalKey::Numpad6, PhysicalKey::ArrowRight),
578
1
            (0x50, PhysicalKey::Numpad2, PhysicalKey::ArrowDown),
579
1
            (0x52, PhysicalKey::Numpad0, PhysicalKey::Insert),
580
1
            (0x53, PhysicalKey::NumpadDecimal, PhysicalKey::Delete),
581
1
        ];
582
13
        for (sc, plain, ext) in pairs {
583
12
            assert_eq!(
584
12
                PhysicalKey::from_windows_scancode(*sc, false),
585
                *plain,
586
                "{sc:#04X}"
587
            );
588
12
            assert_eq!(
589
12
                PhysicalKey::from_windows_scancode(*sc, true),
590
                *ext,
591
                "E0 {sc:#04X}"
592
            );
593
12
            assert_ne!(plain, ext);
594
        }
595
1
    }
596

            
597
    /// An unknown code is reported as unidentified, never guessed at and never
598
    /// silently turned into a neighbouring key.
599
    #[test]
600
1
    fn an_unnamed_code_is_unidentified() {
601
1
        assert_eq!(PhysicalKey::from_evdev(0), PhysicalKey::Unidentified);
602
1
        assert_eq!(PhysicalKey::from_evdev(9999), PhysicalKey::Unidentified);
603
1
        assert_eq!(
604
1
            PhysicalKey::from_windows_scancode(0xFE, false),
605
            PhysicalKey::Unidentified
606
        );
607
1
        assert_eq!(
608
1
            PhysicalKey::from_windows_scancode(0x02, true),
609
            PhysicalKey::Unidentified
610
        );
611
1
        assert_eq!(
612
1
            PhysicalKey::from_macos_keycode(0xFF),
613
            PhysicalKey::Unidentified
614
        );
615
        // kVK_Function has no W3C `code` and must not be invented.
616
1
        assert_eq!(
617
1
            PhysicalKey::from_macos_keycode(0x3F),
618
            PhysicalKey::Unidentified
619
        );
620
1
    }
621

            
622
    /// X11 keycodes are `evdev + 8`; below that they cannot be evdev keys and
623
    /// must not wrap into a wrong one.
624
    #[test]
625
1
    fn an_x11_keycode_below_the_offset_cannot_wrap() {
626
9
        for kc in 0..8 {
627
8
            assert_eq!(
628
8
                PhysicalKey::from_x11_keycode(kc),
629
                PhysicalKey::Unidentified,
630
                "{kc}"
631
            );
632
        }
633
1
        assert_eq!(PhysicalKey::from_x11_keycode(8), PhysicalKey::Unidentified); // evdev 0
634
1
        assert_eq!(PhysicalKey::from_x11_keycode(9), PhysicalKey::Escape); // evdev 1
635
1
    }
636

            
637
    /// Left and right modifiers are distinct positions — the reason
638
    /// `PhysicalKey` splits them where `KeyModifiers` deliberately does not.
639
    #[test]
640
1
    fn left_and_right_modifiers_are_distinct_positions() {
641
1
        assert_ne!(PhysicalKey::from_evdev(29), PhysicalKey::from_evdev(97));
642
1
        assert_eq!(PhysicalKey::from_evdev(29), PhysicalKey::ControlLeft);
643
1
        assert_eq!(PhysicalKey::from_evdev(97), PhysicalKey::ControlRight);
644
1
        assert_eq!(PhysicalKey::from_macos_keycode(0x37), PhysicalKey::MetaLeft);
645
1
        assert_eq!(
646
1
            PhysicalKey::from_macos_keycode(0x36),
647
            PhysicalKey::MetaRight
648
        );
649
1
    }
650
}