keycodes.go RawHistoryFolder
1// Package input provides keyboard event handling and modal input mode management.
2package input
3
4// Hardware keycodes for the number row keys.
5// These are XKB keycodes (evdev + 8) representing physical key positions,
6// independent of keyboard layout. This enables shortcuts like Alt+1 to work
7// on non-QWERTY layouts (AZERTY, QWERTZ, etc.) where the number keys produce
8// different characters without Shift.
9//
10// For example, on French AZERTY:
11// - Physical "1" key produces "&" (keyval=ampersand)
12// - But the hardware keycode is still 10
13// - So Alt + physical "1" can be detected via keycode even though keyval differs
14const (
15 KeycodeDigit1 uint = 10 // Physical '1' key position
16 KeycodeDigit2 uint = 11 // Physical '2' key position
17 KeycodeDigit3 uint = 12 // Physical '3' key position
18 KeycodeDigit4 uint = 13 // Physical '4' key position
19 KeycodeDigit5 uint = 14 // Physical '5' key position
20 KeycodeDigit6 uint = 15 // Physical '6' key position
21 KeycodeDigit7 uint = 16 // Physical '7' key position
22 KeycodeDigit8 uint = 17 // Physical '8' key position
23 KeycodeDigit9 uint = 18 // Physical '9' key position
24 KeycodeDigit0 uint = 19 // Physical '0' key position
25
26 KeycodeBracketLeft uint = 34 // Physical '[' key position (US layout)
27 KeycodeBracketRight uint = 35 // Physical ']' key position (US layout)
28)
29
30// KeycodeToTabAction maps hardware keycodes for the number row to tab switch actions.
31// This enables Alt+1-9/0 shortcuts to work on non-QWERTY keyboards by matching
32// the physical key position rather than the translated keyval.
33var KeycodeToTabAction = map[uint]Action{
34 KeycodeDigit1: ActionSwitchTabIndex1,
35 KeycodeDigit2: ActionSwitchTabIndex2,
36 KeycodeDigit3: ActionSwitchTabIndex3,
37 KeycodeDigit4: ActionSwitchTabIndex4,
38 KeycodeDigit5: ActionSwitchTabIndex5,
39 KeycodeDigit6: ActionSwitchTabIndex6,
40 KeycodeDigit7: ActionSwitchTabIndex7,
41 KeycodeDigit8: ActionSwitchTabIndex8,
42 KeycodeDigit9: ActionSwitchTabIndex9,
43 KeycodeDigit0: ActionSwitchTabIndex10,
44}
45
46var KeycodeToBracketAction = map[uint]struct {
47 NoShift Action
48 WithShift Action
49}{
50 KeycodeBracketLeft: {
51 NoShift: ActionConsumeOrExpelLeft,
52 WithShift: ActionConsumeOrExpelUp,
53 },
54 KeycodeBracketRight: {
55 NoShift: ActionConsumeOrExpelRight,
56 WithShift: ActionConsumeOrExpelDown,
57 },
58}
59
60// KeycodeToDigitIndex converts a hardware keycode to a digit index (0-9).
61// Returns the index and true if the keycode is a number row key, or -1 and false otherwise.
62// This is useful for components like omnibox that need Ctrl+1-9/0 shortcuts.
63//
64// Mapping:
65// - Keycode 10 (physical '1') -> index 0
66// - Keycode 11 (physical '2') -> index 1
67// - ...
68// - Keycode 18 (physical '9') -> index 8
69// - Keycode 19 (physical '0') -> index 9
70func KeycodeToDigitIndex(keycode uint) (index int, ok bool) {
71 if keycode >= KeycodeDigit1 && keycode <= KeycodeDigit0 {
72 return int(keycode - KeycodeDigit1), true
73 }
74 return -1, false
75}