-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_mappings.go
186 lines (174 loc) · 3.55 KB
/
key_mappings.go
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
package uinput
import (
"fmt"
"strings"
)
func DefaultKeymap() (keymap []EventCode) {
for _, value := range runeKeyCodes {
keymap = append(keymap, value)
}
for _, value := range shiftedRuneKeyCodes {
keymap = append(keymap, value)
}
for _, value := range namedKeyCodes {
keymap = append(keymap, value)
}
return keymap
}
// runeKeyCodes contains runes that can be typed with a single key
// (in the default QWERTY layout).
var runeKeyCodes = map[rune]EventCode{
'1': KEY_1,
'2': KEY_2,
'3': KEY_3,
'4': KEY_4,
'5': KEY_5,
'6': KEY_6,
'7': KEY_7,
'8': KEY_8,
'9': KEY_9,
'0': KEY_0,
'-': KEY_MINUS,
'=': KEY_EQUAL,
'\b': KEY_BACKSPACE,
'\t': KEY_TAB,
'q': KEY_Q,
'w': KEY_W,
'e': KEY_E,
'r': KEY_R,
't': KEY_T,
'y': KEY_Y,
'u': KEY_U,
'i': KEY_I,
'o': KEY_O,
'p': KEY_P,
'[': KEY_LEFTBRACE,
']': KEY_RIGHTBRACE,
'\n': KEY_ENTER,
'a': KEY_A,
's': KEY_S,
'd': KEY_D,
'f': KEY_F,
'g': KEY_G,
'h': KEY_H,
'j': KEY_J,
'k': KEY_K,
'l': KEY_L,
';': KEY_SEMICOLON,
'\'': KEY_APOSTROPHE,
'`': KEY_GRAVE,
'\\': KEY_BACKSLASH,
'z': KEY_Z,
'x': KEY_X,
'c': KEY_C,
'v': KEY_V,
'b': KEY_B,
'n': KEY_N,
'm': KEY_M,
',': KEY_COMMA,
'.': KEY_DOT,
'/': KEY_SLASH,
' ': KEY_SPACE,
}
// runeKeyCodes contains runes that can be typed by holding Shift and pressing a
// single key (in the default QWERTY layout).
var shiftedRuneKeyCodes = map[rune]EventCode{
'!': KEY_1,
'@': KEY_2,
'#': KEY_3,
'$': KEY_4,
'%': KEY_5,
'^': KEY_6,
'&': KEY_7,
'*': KEY_8,
'(': KEY_9,
')': KEY_0,
'_': KEY_MINUS,
'+': KEY_EQUAL,
'Q': KEY_Q,
'W': KEY_W,
'E': KEY_E,
'R': KEY_R,
'T': KEY_T,
'Y': KEY_Y,
'U': KEY_U,
'I': KEY_I,
'O': KEY_O,
'P': KEY_P,
'{': KEY_LEFTBRACE,
'}': KEY_RIGHTBRACE,
'A': KEY_A,
'S': KEY_S,
'D': KEY_D,
'F': KEY_F,
'G': KEY_G,
'H': KEY_H,
'J': KEY_J,
'K': KEY_K,
'L': KEY_L,
':': KEY_SEMICOLON,
'"': KEY_APOSTROPHE,
'~': KEY_GRAVE,
'|': KEY_BACKSLASH,
'Z': KEY_Z,
'X': KEY_X,
'C': KEY_C,
'V': KEY_V,
'B': KEY_B,
'N': KEY_N,
'M': KEY_M,
'<': KEY_COMMA,
'>': KEY_DOT,
'?': KEY_SLASH,
}
// namedKeyCodes contains multi-character names describing keys that may be used in accelerators.
var namedKeyCodes = map[string]EventCode{
"alt": KEY_LEFTALT,
"ctrl": KEY_LEFTCTRL,
"search": KEY_LEFTMETA,
"shift": KEY_LEFTSHIFT,
"backspace": KEY_BACKSPACE,
"enter": KEY_ENTER,
"space": KEY_SPACE,
"tab": KEY_TAB,
"f1": KEY_F1,
"f2": KEY_F2,
"f3": KEY_F3,
"f4": KEY_F4,
"f5": KEY_F5,
"f6": KEY_F6,
"f7": KEY_F7,
"f8": KEY_F8,
"f9": KEY_F9,
"f10": KEY_F10,
"f11": KEY_F11,
"f12": KEY_F12,
"playpause": KEY_PLAYPAUSE,
}
// parseAccel parses a string in the format accepted by the Accel function.
// It returns keycodes in the order in which they appear in the string.
func parseAccel(accel string) ([]EventCode, error) {
var keys []EventCode
for _, name := range strings.Split(accel, "+") {
if len(name) == 0 {
return nil, fmt.Errorf("[error] empty key")
}
lname := strings.ToLower(name)
if code, ok := namedKeyCodes[lname]; ok {
keys = append(keys, code)
continue
}
if runes := []rune(lname); len(runes) == 1 {
if code, ok := runeKeyCodes[runes[0]]; ok {
// Require whitespace chars to be spelled out.
if code == KEY_BACKSPACE || code == KEY_TAB || code == KEY_ENTER || code == KEY_SPACE {
return nil, fmt.Errorf("[error] must spell out key name instead of using %q", name)
}
keys = append(keys, code)
continue
}
}
return nil, fmt.Errorf("[error] unknown key %q", name)
}
return keys, nil
}