summaryrefslogtreecommitdiff
path: root/src/input/key.zig
blob: 54c7491ae24334aed33b355cde4d6e7cbdcf054c (plain)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
const std = @import("std");
const builtin = @import("builtin");
const Allocator = std.mem.Allocator;
const cimgui = @import("cimgui");
const OptionAsAlt = @import("config.zig").OptionAsAlt;

/// A generic key input event. This is the information that is necessary
/// regardless of apprt in order to generate the proper terminal
/// control sequences for a given key press.
///
/// Some apprts may not be able to provide all of this information, such
/// as GLFW. In this case, the apprt should provide as much information
/// as it can and it should be expected that the terminal behavior
/// will not be totally correct.
pub const KeyEvent = struct {
    /// The action: press, release, etc.
    action: Action = .press,

    /// The keycode of the physical key that was pressed. This is agnostic
    /// to the layout. Layout-dependent matching can only be done via the
    /// UTF-8 or unshifted codepoint.
    key: Key = .unidentified,

    /// Mods are the modifiers that are pressed.
    mods: Mods = .{},

    /// The mods that were consumed in order to generate the text
    /// in utf8. This has the mods set that were consumed, so to
    /// get the set of mods that are effective you must negate
    /// mods with this.
    ///
    /// This field is meaningless if utf8 is empty.
    consumed_mods: Mods = .{},

    /// Composing is true when this key event is part of a dead key
    /// composition sequence and we're in the middle of it.
    composing: bool = false,

    /// The utf8 sequence that was generated by this key event.
    /// This will be an empty string if there is no text generated.
    /// If composing is true and this is non-empty, this is preedit
    /// text.
    utf8: []const u8 = "",

    /// The codepoint for this key when it is unshifted. For example,
    /// shift+a is "A" in UTF-8 but unshifted would provide 'a'.
    unshifted_codepoint: u21 = 0,

    /// Returns the effective modifiers for this event. The effective
    /// modifiers are the mods that should be considered for keybindings.
    pub fn effectiveMods(self: KeyEvent) Mods {
        if (self.utf8.len == 0) return self.mods;
        return self.mods.unset(self.consumed_mods);
    }

    /// Returns a unique hash for this key event to be used for tracking
    /// uniquess specifically with bindings. This omits fields that are
    /// irrelevant for bindings.
    pub fn bindingHash(self: KeyEvent) u64 {
        var hasher = std.hash.Wyhash.init(0);

        // These are all the fields that are explicitly part of Trigger.
        std.hash.autoHash(&hasher, self.key);
        std.hash.autoHash(&hasher, self.unshifted_codepoint);
        std.hash.autoHash(&hasher, self.mods.binding());

        // Notes on unmapped things and why:
        //
        // - action: we don't have action-specific bindings right now
        //   AND we want to know if a key resulted in a binding regardless
        //   of action because a press should also ignore a release and so on.
        //
        // We can add to this if there is other confusion.

        return hasher.final();
    }
};

/// A bitmask for all key modifiers.
///
/// IMPORTANT: Any changes here update include/ghostty.h
pub const Mods = packed struct(Mods.Backing) {
    pub const Backing = u16;

    shift: bool = false,
    ctrl: bool = false,
    alt: bool = false,
    super: bool = false,
    caps_lock: bool = false,
    num_lock: bool = false,
    sides: side = .{},
    _padding: u6 = 0,

    /// Tracks the side that is active for any given modifier. Note
    /// that this doesn't confirm a modifier is pressed; you must check
    /// the bool for that in addition to this.
    ///
    /// Not all platforms support this, check apprt for more info.
    pub const side = packed struct(u4) {
        shift: Side = .left,
        ctrl: Side = .left,
        alt: Side = .left,
        super: Side = .left,
    };

    pub const Side = enum(u1) { left, right };

    /// Integer value of this struct.
    pub fn int(self: Mods) Backing {
        return @bitCast(self);
    }

    /// Returns true if no modifiers are set.
    pub fn empty(self: Mods) bool {
        return self.int() == 0;
    }

    /// Returns true if two mods are equal.
    pub fn equal(self: Mods, other: Mods) bool {
        return self.int() == other.int();
    }

    /// Return mods that are only relevant for bindings.
    pub fn binding(self: Mods) Mods {
        return .{
            .shift = self.shift,
            .ctrl = self.ctrl,
            .alt = self.alt,
            .super = self.super,
        };
    }

    /// Perform `self &~ other` to remove the other mods from self.
    pub fn unset(self: Mods, other: Mods) Mods {
        return @bitCast(self.int() & ~other.int());
    }

    /// Returns the mods without locks set.
    pub fn withoutLocks(self: Mods) Mods {
        var copy = self;
        copy.caps_lock = false;
        copy.num_lock = false;
        return copy;
    }

    /// Return the mods to use for key translation. This handles settings
    /// like macos-option-as-alt. The translation mods should be used for
    /// translation but never sent back in for the key callback.
    pub fn translation(self: Mods, option_as_alt: OptionAsAlt) Mods {
        var result = self;

        // macos-option-as-alt for darwin
        if (comptime builtin.target.os.tag.isDarwin()) alt: {
            // Alt has to be set only on the correct side
            switch (option_as_alt) {
                .false => break :alt,
                .true => {},
                .left => if (self.sides.alt == .right) break :alt,
                .right => if (self.sides.alt == .left) break :alt,
            }

            // Unset alt
            result.alt = false;
        }

        return result;
    }

    /// Checks to see if super is on (MacOS) or ctrl.
    pub fn ctrlOrSuper(self: Mods) bool {
        if (comptime builtin.target.os.tag.isDarwin()) {
            return self.super;
        }
        return self.ctrl;
    }

    // For our own understanding
    test {
        const testing = std.testing;
        try testing.expectEqual(@as(Backing, @bitCast(Mods{})), @as(Backing, 0b0));
        try testing.expectEqual(
            @as(Backing, @bitCast(Mods{ .shift = true })),
            @as(Backing, 0b0000_0001),
        );
    }

    test "translation macos-option-as-alt" {
        if (comptime !builtin.target.os.tag.isDarwin()) return error.SkipZigTest;

        const testing = std.testing;

        // Unset
        {
            const mods: Mods = .{};
            const result = mods.translation(.true);
            try testing.expectEqual(result, mods);
        }

        // Set
        {
            const mods: Mods = .{ .alt = true };
            const result = mods.translation(.true);
            try testing.expectEqual(Mods{}, result);
        }

        // Set but disabled
        {
            const mods: Mods = .{ .alt = true };
            const result = mods.translation(.false);
            try testing.expectEqual(result, mods);
        }

        // Set wrong side
        {
            const mods: Mods = .{ .alt = true, .sides = .{ .alt = .right } };
            const result = mods.translation(.left);
            try testing.expectEqual(result, mods);
        }
        {
            const mods: Mods = .{ .alt = true, .sides = .{ .alt = .left } };
            const result = mods.translation(.right);
            try testing.expectEqual(result, mods);
        }

        // Set with other mods
        {
            const mods: Mods = .{ .alt = true, .shift = true };
            const result = mods.translation(.true);
            try testing.expectEqual(Mods{ .shift = true }, result);
        }
    }
};

/// The action associated with an input event. This is backed by a c_int
/// so that we can use the enum as-is for our embedding API.
///
/// IMPORTANT: Any changes here update include/ghostty.h
pub const Action = enum(c_int) {
    release,
    press,
    repeat,
};

/// The set of key codes that Ghostty is aware of. These represent
/// physical keys on the keyboard. The logical key (or key string)
/// is the string that is generated by the key event and that is up
/// to the apprt to provide.
///
/// Note that these are layout-independent. For example, the "a"
/// key on a US keyboard is the same as the "ф" key on a Russian
/// keyboard, but both will report the "a" enum value in the key
/// event. These values are based on the W3C standard. See:
/// https://www.w3.org/TR/uievents-code
///
/// Layout-dependent strings are provided in the KeyEvent struct as
/// UTF-8 and are produced by the associated apprt. Ghostty core has
/// no mechanism to map input events to strings without the apprt.
///
/// IMPORTANT: Any changes here update include/ghostty.h ghostty_input_key_e
pub const Key = enum(c_int) {
    unidentified,

    // "Writing System Keys" § 3.1.1
    backquote,
    backslash,
    bracket_left,
    bracket_right,
    comma,
    digit_0,
    digit_1,
    digit_2,
    digit_3,
    digit_4,
    digit_5,
    digit_6,
    digit_7,
    digit_8,
    digit_9,
    equal,
    intl_backslash,
    intl_ro,
    intl_yen,
    key_a,
    key_b,
    key_c,
    key_d,
    key_e,
    key_f,
    key_g,
    key_h,
    key_i,
    key_j,
    key_k,
    key_l,
    key_m,
    key_n,
    key_o,
    key_p,
    key_q,
    key_r,
    key_s,
    key_t,
    key_u,
    key_v,
    key_w,
    key_x,
    key_y,
    key_z,
    minus,
    period,
    quote,
    semicolon,
    slash,

    // "Functional Keys" § 3.1.2
    alt_left,
    alt_right,
    backspace,
    caps_lock,
    context_menu,
    control_left,
    control_right,
    enter,
    meta_left,
    meta_right,
    shift_left,
    shift_right,
    space,
    tab,
    convert,
    kana_mode,
    non_convert,

    // "Control Pad Section" § 3.2
    delete,
    end,
    help,
    home,
    insert,
    page_down,
    page_up,

    // "Arrow Pad Section" § 3.3
    arrow_down,
    arrow_left,
    arrow_right,
    arrow_up,

    // "Numpad Section" § 3.4
    num_lock,
    numpad_0,
    numpad_1,
    numpad_2,
    numpad_3,
    numpad_4,
    numpad_5,
    numpad_6,
    numpad_7,
    numpad_8,
    numpad_9,
    numpad_add,
    numpad_backspace,
    numpad_clear,
    numpad_clear_entry,
    numpad_comma,
    numpad_decimal,
    numpad_divide,
    numpad_enter,
    numpad_equal,
    numpad_memory_add,
    numpad_memory_clear,
    numpad_memory_recall,
    numpad_memory_store,
    numpad_memory_subtract,
    numpad_multiply,
    numpad_paren_left,
    numpad_paren_right,
    numpad_subtract,

    // > For numpads that provide keys not listed here, a code value string
    // > should be created by starting with "Numpad" and appending an
    // > appropriate description of the key.
    //
    // These numpad entries are distinguished by various encoding protocols
    // (legacy and Kitty) so we support them here in case the apprt can
    // produce them.
    numpad_separator,
    numpad_up,
    numpad_down,
    numpad_right,
    numpad_left,
    numpad_begin,
    numpad_home,
    numpad_end,
    numpad_insert,
    numpad_delete,
    numpad_page_up,
    numpad_page_down,

    // "Function Section" § 3.5
    escape,
    f1,
    f2,
    f3,
    f4,
    f5,
    f6,
    f7,
    f8,
    f9,
    f10,
    f11,
    f12,
    f13,
    f14,
    f15,
    f16,
    f17,
    f18,
    f19,
    f20,
    f21,
    f22,
    f23,
    f24,
    f25,
    @"fn",
    fn_lock,
    print_screen,
    scroll_lock,
    pause,

    // "Media Keys" § 3.6
    browser_back,
    browser_favorites,
    browser_forward,
    browser_home,
    browser_refresh,
    browser_search,
    browser_stop,
    eject,
    launch_app_1,
    launch_app_2,
    launch_mail,
    media_play_pause,
    media_select,
    media_stop,
    media_track_next,
    media_track_previous,
    power,
    sleep,
    audio_volume_down,
    audio_volume_mute,
    audio_volume_up,
    wake_up,

    // "Legacy, Non-standard, and Special Keys" § 3.7
    copy,
    cut,
    paste,

    /// Converts an ASCII character to a key, if possible. This returns
    /// null if the character is unknown.
    ///
    /// Note that this can't distinguish between physical keys, i.e. '0'
    /// may be from the number row or the keypad, but it always maps
    /// to '.zero'.
    ///
    /// This is what we want, we want people to create keybindings that
    /// are independent of the physical key.
    pub fn fromASCII(ch: u8) ?Key {
        return switch (ch) {
            inline else => |comptime_ch| {
                return comptime result: {
                    @setEvalBranchQuota(100_000);
                    for (codepoint_map) |entry| {
                        // No ASCII characters should ever map to a keypad key
                        if (entry[1].keypad()) continue;

                        if (entry[0] == @as(u21, @intCast(comptime_ch))) {
                            break :result entry[1];
                        }
                    }

                    break :result null;
                };
            },
        };
    }

    /// Converts a W3C key code to a Ghostty key enum value.
    ///
    /// All required W3C key codes are supported, but there are a number of
    /// non-standard key codes that are not supported. In the case the value is
    /// invalid or unsupported, this function will return null.
    pub fn fromW3C(code: []const u8) ?Key {
        var result: [128]u8 = undefined;

        // If the code is bigger than our buffer it can't possibly match.
        if (code.len > result.len) return null;

        // First just check the whole thing lowercased, this is the simple case
        if (std.meta.stringToEnum(
            Key,
            std.ascii.lowerString(&result, code),
        )) |key| return key;

        // We need to convert FooBar to foo_bar
        var fbs = std.io.fixedBufferStream(&result);
        const w = fbs.writer();
        for (code, 0..) |ch, i| switch (ch) {
            'a'...'z' => w.writeByte(ch) catch return null,

            // Caps and numbers trigger underscores
            'A'...'Z', '0'...'9' => {
                if (i > 0) w.writeByte('_') catch return null;
                w.writeByte(std.ascii.toLower(ch)) catch return null;
            },

            // We don't know of any key codes that aren't alphanumeric.
            else => return null,
        };

        return std.meta.stringToEnum(Key, fbs.getWritten());
    }

    /// Converts a Ghostty key enum value to a W3C key code.
    pub fn w3c(self: Key) []const u8 {
        return switch (self) {
            inline else => |tag| comptime w3c: {
                @setEvalBranchQuota(50_000);

                const name = @tagName(tag);

                var buf: [128]u8 = undefined;
                var fbs = std.io.fixedBufferStream(&buf);
                const w = fbs.writer();
                var i: usize = 0;
                while (i < name.len) {
                    if (i == 0) {
                        w.writeByte(std.ascii.toUpper(name[i])) catch unreachable;
                    } else if (name[i] == '_') {
                        i += 1;
                        w.writeByte(std.ascii.toUpper(name[i])) catch unreachable;
                    } else {
                        w.writeByte(name[i]) catch unreachable;
                    }

                    i += 1;
                }

                const written = buf;
                const result = written[0..fbs.getWritten().len];
                break :w3c result;
            },
        };
    }

    /// True if this key represents a printable character.
    pub fn printable(self: Key) bool {
        return switch (self) {
            inline else => |tag| {
                return comptime result: {
                    @setEvalBranchQuota(10_000);
                    for (codepoint_map) |entry| {
                        if (entry[1] == tag) break :result true;
                    }

                    break :result false;
                };
            },
        };
    }

    /// True if this key is a modifier.
    pub fn modifier(self: Key) bool {
        return switch (self) {
            .shift_left,
            .control_left,
            .alt_left,
            .meta_left,
            .shift_right,
            .control_right,
            .alt_right,
            .meta_right,
            => true,

            else => false,
        };
    }

    /// Whether this key should be remappable by the operating system.
    ///
    /// On certain OSes (namely Linux and the BSDs) certain keys like the
    /// functional keys are expected to be remappable by the user, such as
    /// in the very common use case of swapping the Caps Lock key with the
    /// Escape key with the XKB option `caps:swapescape`.
    ///
    /// However, the way XKB implements this is by essentially acting as a
    /// software key remapper that destroys all information about the original
    /// physical key, leading to very annoying bugs like #7309 where the
    /// physical key `XKB_KEY_c` gets remapped into `XKB_KEY_Cyrillic_tse`,
    /// which causes all of our physical key handling to completely break down.
    /// _Very naughty._
    ///
    /// As a compromise, given that writing system keys (§3.1.1) comprise the
    /// majority of keys that "change meaning [...] based on the current locale
    /// and keyboard layout", we allow all other keys to be remapped by default
    /// since they should be fairly harmless. We might consider making this
    /// configurable, but for now this should at least placate most people.
    pub fn shouldBeRemappable(self: Key) bool {
        return switch (self) {
            // "Writing System Keys" § 3.1.1
            .backquote,
            .backslash,
            .bracket_left,
            .bracket_right,
            .comma,
            .digit_0,
            .digit_1,
            .digit_2,
            .digit_3,
            .digit_4,
            .digit_5,
            .digit_6,
            .digit_7,
            .digit_8,
            .digit_9,
            .equal,
            .intl_backslash,
            .intl_ro,
            .intl_yen,
            .key_a,
            .key_b,
            .key_c,
            .key_d,
            .key_e,
            .key_f,
            .key_g,
            .key_h,
            .key_i,
            .key_j,
            .key_k,
            .key_l,
            .key_m,
            .key_n,
            .key_o,
            .key_p,
            .key_q,
            .key_r,
            .key_s,
            .key_t,
            .key_u,
            .key_v,
            .key_w,
            .key_x,
            .key_y,
            .key_z,
            .minus,
            .period,
            .quote,
            .semicolon,
            .slash,
            => false,

            else => true,
        };
    }

    /// Returns true if this is a keypad key.
    pub fn keypad(self: Key) bool {
        return switch (self) {
            inline else => |tag| {
                const name = @tagName(tag);
                const result = comptime std.mem.startsWith(u8, name, "numpad_");
                return result;
            },
        };
    }

    // Returns the codepoint representing this key, or null if the key is not
    // printable
    pub fn codepoint(self: Key) ?u21 {
        return switch (self) {
            inline else => |tag| {
                return comptime result: {
                    @setEvalBranchQuota(10_000);
                    for (codepoint_map) |entry| {
                        if (entry[1] == tag) break :result entry[0];
                    }

                    break :result null;
                };
            },
        };
    }

    /// Returns the cimgui key constant for this key.
    pub fn imguiKey(self: Key) ?c_uint {
        return switch (self) {
            .key_a => cimgui.c.ImGuiKey_A,
            .key_b => cimgui.c.ImGuiKey_B,
            .key_c => cimgui.c.ImGuiKey_C,
            .key_d => cimgui.c.ImGuiKey_D,
            .key_e => cimgui.c.ImGuiKey_E,
            .key_f => cimgui.c.ImGuiKey_F,
            .key_g => cimgui.c.ImGuiKey_G,
            .key_h => cimgui.c.ImGuiKey_H,
            .key_i => cimgui.c.ImGuiKey_I,
            .key_j => cimgui.c.ImGuiKey_J,
            .key_k => cimgui.c.ImGuiKey_K,
            .key_l => cimgui.c.ImGuiKey_L,
            .key_m => cimgui.c.ImGuiKey_M,
            .key_n => cimgui.c.ImGuiKey_N,
            .key_o => cimgui.c.ImGuiKey_O,
            .key_p => cimgui.c.ImGuiKey_P,
            .key_q => cimgui.c.ImGuiKey_Q,
            .key_r => cimgui.c.ImGuiKey_R,
            .key_s => cimgui.c.ImGuiKey_S,
            .key_t => cimgui.c.ImGuiKey_T,
            .key_u => cimgui.c.ImGuiKey_U,
            .key_v => cimgui.c.ImGuiKey_V,
            .key_w => cimgui.c.ImGuiKey_W,
            .key_x => cimgui.c.ImGuiKey_X,
            .key_y => cimgui.c.ImGuiKey_Y,
            .key_z => cimgui.c.ImGuiKey_Z,

            .digit_0 => cimgui.c.ImGuiKey_0,
            .digit_1 => cimgui.c.ImGuiKey_1,
            .digit_2 => cimgui.c.ImGuiKey_2,
            .digit_3 => cimgui.c.ImGuiKey_3,
            .digit_4 => cimgui.c.ImGuiKey_4,
            .digit_5 => cimgui.c.ImGuiKey_5,
            .digit_6 => cimgui.c.ImGuiKey_6,
            .digit_7 => cimgui.c.ImGuiKey_7,
            .digit_8 => cimgui.c.ImGuiKey_8,
            .digit_9 => cimgui.c.ImGuiKey_9,

            .semicolon => cimgui.c.ImGuiKey_Semicolon,
            .space => cimgui.c.ImGuiKey_Space,
            .quote => cimgui.c.ImGuiKey_Apostrophe,
            .comma => cimgui.c.ImGuiKey_Comma,
            .backquote => cimgui.c.ImGuiKey_GraveAccent,
            .period => cimgui.c.ImGuiKey_Period,
            .slash => cimgui.c.ImGuiKey_Slash,
            .minus => cimgui.c.ImGuiKey_Minus,
            .equal => cimgui.c.ImGuiKey_Equal,
            .bracket_left => cimgui.c.ImGuiKey_LeftBracket,
            .bracket_right => cimgui.c.ImGuiKey_RightBracket,
            .backslash => cimgui.c.ImGuiKey_Backslash,

            .arrow_up => cimgui.c.ImGuiKey_UpArrow,
            .arrow_down => cimgui.c.ImGuiKey_DownArrow,
            .arrow_left => cimgui.c.ImGuiKey_LeftArrow,
            .arrow_right => cimgui.c.ImGuiKey_RightArrow,
            .home => cimgui.c.ImGuiKey_Home,
            .end => cimgui.c.ImGuiKey_End,
            .insert => cimgui.c.ImGuiKey_Insert,
            .delete => cimgui.c.ImGuiKey_Delete,
            .caps_lock => cimgui.c.ImGuiKey_CapsLock,
            .scroll_lock => cimgui.c.ImGuiKey_ScrollLock,
            .num_lock => cimgui.c.ImGuiKey_NumLock,
            .page_up => cimgui.c.ImGuiKey_PageUp,
            .page_down => cimgui.c.ImGuiKey_PageDown,
            .escape => cimgui.c.ImGuiKey_Escape,
            .enter => cimgui.c.ImGuiKey_Enter,
            .tab => cimgui.c.ImGuiKey_Tab,
            .backspace => cimgui.c.ImGuiKey_Backspace,
            .print_screen => cimgui.c.ImGuiKey_PrintScreen,
            .pause => cimgui.c.ImGuiKey_Pause,
            .context_menu => cimgui.c.ImGuiKey_Menu,

            .f1 => cimgui.c.ImGuiKey_F1,
            .f2 => cimgui.c.ImGuiKey_F2,
            .f3 => cimgui.c.ImGuiKey_F3,
            .f4 => cimgui.c.ImGuiKey_F4,
            .f5 => cimgui.c.ImGuiKey_F5,
            .f6 => cimgui.c.ImGuiKey_F6,
            .f7 => cimgui.c.ImGuiKey_F7,
            .f8 => cimgui.c.ImGuiKey_F8,
            .f9 => cimgui.c.ImGuiKey_F9,
            .f10 => cimgui.c.ImGuiKey_F10,
            .f11 => cimgui.c.ImGuiKey_F11,
            .f12 => cimgui.c.ImGuiKey_F12,

            .numpad_0 => cimgui.c.ImGuiKey_Keypad0,
            .numpad_1 => cimgui.c.ImGuiKey_Keypad1,
            .numpad_2 => cimgui.c.ImGuiKey_Keypad2,
            .numpad_3 => cimgui.c.ImGuiKey_Keypad3,
            .numpad_4 => cimgui.c.ImGuiKey_Keypad4,
            .numpad_5 => cimgui.c.ImGuiKey_Keypad5,
            .numpad_6 => cimgui.c.ImGuiKey_Keypad6,
            .numpad_7 => cimgui.c.ImGuiKey_Keypad7,
            .numpad_8 => cimgui.c.ImGuiKey_Keypad8,
            .numpad_9 => cimgui.c.ImGuiKey_Keypad9,
            .numpad_decimal => cimgui.c.ImGuiKey_KeypadDecimal,
            .numpad_divide => cimgui.c.ImGuiKey_KeypadDivide,
            .numpad_multiply => cimgui.c.ImGuiKey_KeypadMultiply,
            .numpad_subtract => cimgui.c.ImGuiKey_KeypadSubtract,
            .numpad_add => cimgui.c.ImGuiKey_KeypadAdd,
            .numpad_enter => cimgui.c.ImGuiKey_KeypadEnter,
            .numpad_equal => cimgui.c.ImGuiKey_KeypadEqual,
            // We map KP_SEPARATOR to Comma because traditionally a numpad would
            // have a numeric separator key. Most modern numpads do not
            .numpad_left => cimgui.c.ImGuiKey_LeftArrow,
            .numpad_right => cimgui.c.ImGuiKey_RightArrow,
            .numpad_up => cimgui.c.ImGuiKey_UpArrow,
            .numpad_down => cimgui.c.ImGuiKey_DownArrow,
            .numpad_page_up => cimgui.c.ImGuiKey_PageUp,
            .numpad_page_down => cimgui.c.ImGuiKey_PageUp,
            .numpad_home => cimgui.c.ImGuiKey_Home,
            .numpad_end => cimgui.c.ImGuiKey_End,
            .numpad_insert => cimgui.c.ImGuiKey_Insert,
            .numpad_delete => cimgui.c.ImGuiKey_Delete,
            .numpad_begin => cimgui.c.ImGuiKey_NamedKey_BEGIN,

            .shift_left => cimgui.c.ImGuiKey_LeftShift,
            .control_left => cimgui.c.ImGuiKey_LeftCtrl,
            .alt_left => cimgui.c.ImGuiKey_LeftAlt,
            .meta_left => cimgui.c.ImGuiKey_LeftSuper,
            .shift_right => cimgui.c.ImGuiKey_RightShift,
            .control_right => cimgui.c.ImGuiKey_RightCtrl,
            .alt_right => cimgui.c.ImGuiKey_RightAlt,
            .meta_right => cimgui.c.ImGuiKey_RightSuper,

            // These keys aren't represented in cimgui
            .f13,
            .f14,
            .f15,
            .f16,
            .f17,
            .f18,
            .f19,
            .f20,
            .f21,
            .f22,
            .f23,
            .f24,
            .f25,
            .intl_backslash,
            .intl_ro,
            .intl_yen,
            .convert,
            .kana_mode,
            .non_convert,
            .numpad_separator,
            .numpad_backspace,
            .numpad_clear,
            .numpad_clear_entry,
            .numpad_comma,
            .numpad_memory_add,
            .numpad_memory_clear,
            .numpad_memory_recall,
            .numpad_memory_store,
            .numpad_memory_subtract,
            .numpad_paren_left,
            .numpad_paren_right,
            .@"fn",
            .fn_lock,
            .browser_back,
            .browser_favorites,
            .browser_forward,
            .browser_home,
            .browser_refresh,
            .browser_search,
            .browser_stop,
            .eject,
            .launch_app_1,
            .launch_app_2,
            .launch_mail,
            .media_play_pause,
            .media_select,
            .media_stop,
            .media_track_next,
            .media_track_previous,
            .power,
            .sleep,
            .audio_volume_down,
            .audio_volume_mute,
            .audio_volume_up,
            .wake_up,
            .help,
            .copy,
            .cut,
            .paste,
            => null,

            .unidentified,
            => null,
        };
    }

    /// true if this key is one of the left or right versions of super (MacOS)
    /// or ctrl.
    pub fn ctrlOrSuper(self: Key) bool {
        if (comptime builtin.target.os.tag.isDarwin()) {
            return self == .meta_left or self == .meta_right;
        }
        return self == .control_left or self == .control_right;
    }

    /// true if this key is either left or right shift.
    pub fn leftOrRightShift(self: Key) bool {
        return self == .shift_left or self == .shift_right;
    }

    /// true if this key is either left or right alt.
    pub fn leftOrRightAlt(self: Key) bool {
        return self == .alt_left or self == .alt_right;
    }

    test "fromASCII should not return keypad keys" {
        const testing = std.testing;
        try testing.expect(Key.fromASCII('0').? == .digit_0);
        try testing.expect(Key.fromASCII('*') == null);
    }

    test "keypad keys" {
        const testing = std.testing;
        try testing.expect(Key.numpad_0.keypad());
        try testing.expect(!Key.digit_1.keypad());
    }

    test "w3c" {
        // All our keys should convert to and from the W3C format.
        // We don't support every key in the W3C spec, so we only
        // check the enum fields.
        const testing = std.testing;
        inline for (@typeInfo(Key).@"enum".fields) |field| {
            const key = @field(Key, field.name);
            const w3c_name = key.w3c();
            try testing.expectEqual(key, Key.fromW3C(w3c_name).?);
        }
    }

    const codepoint_map: []const struct { u21, Key } = &.{
        .{ 'a', .key_a },
        .{ 'b', .key_b },
        .{ 'c', .key_c },
        .{ 'd', .key_d },
        .{ 'e', .key_e },
        .{ 'f', .key_f },
        .{ 'g', .key_g },
        .{ 'h', .key_h },
        .{ 'i', .key_i },
        .{ 'j', .key_j },
        .{ 'k', .key_k },
        .{ 'l', .key_l },
        .{ 'm', .key_m },
        .{ 'n', .key_n },
        .{ 'o', .key_o },
        .{ 'p', .key_p },
        .{ 'q', .key_q },
        .{ 'r', .key_r },
        .{ 's', .key_s },
        .{ 't', .key_t },
        .{ 'u', .key_u },
        .{ 'v', .key_v },
        .{ 'w', .key_w },
        .{ 'x', .key_x },
        .{ 'y', .key_y },
        .{ 'z', .key_z },
        .{ '0', .digit_0 },
        .{ '1', .digit_1 },
        .{ '2', .digit_2 },
        .{ '3', .digit_3 },
        .{ '4', .digit_4 },
        .{ '5', .digit_5 },
        .{ '6', .digit_6 },
        .{ '7', .digit_7 },
        .{ '8', .digit_8 },
        .{ '9', .digit_9 },
        .{ ';', .semicolon },
        .{ ' ', .space },
        .{ '\'', .quote },
        .{ ',', .comma },
        .{ '`', .backquote },
        .{ '.', .period },
        .{ '/', .slash },
        .{ '-', .minus },
        .{ '=', .equal },
        .{ '[', .bracket_left },
        .{ ']', .bracket_right },
        .{ '\\', .backslash },

        // Control characters
        .{ '\t', .tab },

        // Keypad entries. We just assume keypad with the numpad_ prefix
        // so that has some special meaning. These must also always be last,
        // so that our `fromASCII` function doesn't accidentally map them
        // over normal numerics and other keys.
        .{ '0', .numpad_0 },
        .{ '1', .numpad_1 },
        .{ '2', .numpad_2 },
        .{ '3', .numpad_3 },
        .{ '4', .numpad_4 },
        .{ '5', .numpad_5 },
        .{ '6', .numpad_6 },
        .{ '7', .numpad_7 },
        .{ '8', .numpad_8 },
        .{ '9', .numpad_9 },
        .{ '.', .numpad_decimal },
        .{ '/', .numpad_divide },
        .{ '*', .numpad_multiply },
        .{ '-', .numpad_subtract },
        .{ '+', .numpad_add },
        .{ '=', .numpad_equal },
    };
};

/// This sets either "ctrl" or "super" to true (but not both)
/// on mods depending on if the build target is Mac or not. On
/// Mac, we default to super (i.e. super+c for copy) and on
/// non-Mac we default to ctrl (i.e. ctrl+c for copy).
pub fn ctrlOrSuper(mods: Mods) Mods {
    var copy = mods;
    if (comptime builtin.target.os.tag.isDarwin()) {
        copy.super = true;
    } else {
        copy.ctrl = true;
    }

    return copy;
}

test "ctrlOrSuper" {
    const testing = std.testing;
    var m: Mods = ctrlOrSuper(.{});

    try testing.expect(m.ctrlOrSuper());
}