Debian Codegraph @

Nasal/canvas/KeyBinding.nas

raw
1 PrintableKeys = {
2 0: 0x30,
3 1: 0x31,
4 2: 0x32,
5 3: 0x33,
6 4: 0x34,
7 5: 0x35,
8 6: 0x36,
9 7: 0x37,
10 8: 0x38,
11 9: 0x39,
12 A: 0x61,
13 a: 0x61,
14 B: 0x62,
15 b: 0x62,
16 C: 0x63,
17 c: 0x63,
18 D: 0x64,
19 d: 0x64,
20 E: 0x65,
21 e: 0x65,
22 F: 0x66,
23 f: 0x66,
24 G: 0x67,
25 g: 0x67,
26 H: 0x68,
27 h: 0x68,
28 I: 0x69,
29 i: 0x69,
30 J: 0x6a,
31 j: 0x6a,
32 K: 0x6b,
33 k: 0x6b,
34 L: 0x6c,
35 l: 0x6c,
36 M: 0x6d,
37 m: 0x6d,
38 N: 0x6e,
39 n: 0x6e,
40 O: 0x6f,
41 o: 0x6f,
42 P: 0x70,
43 p: 0x70,
44 Q: 0x71,
45 q: 0x71,
46 R: 0x72,
47 r: 0x72,
48 S: 0x73,
49 s: 0x73,
50 T: 0x74,
51 t: 0x74,
52 U: 0x75,
53 u: 0x75,
54 V: 0x76,
55 v: 0x76,
56 W: 0x77,
57 w: 0x77,
58 X: 0x78,
59 x: 0x78,
60 Y: 0x79,
61 y: 0x79,
62 Z: 0x7a,
63 z: 0x7a,
64 "!": 0x21,
65 Exclaim: 0x21,
66 '"': 0x22,
67 DoubleQuote: 0x22,
68 "#": 0x23,
69 Hash: 0x23,
70 "$": 0x24,
71 Dollar: 0x24,
72 "&": 0x26,
73 Ampersand: 0x26,
74 "'": 0x27,
75 SingleQuote: 0x27,
76 "(": 0x28,
77 OpeningParenthesis: 0x28,
78 ")": 0x29,
79 ClosingParenthesis: 0x29,
80 "*": 0x2a,
81 Asterisk: 0x2A,
82 "+": 0x2b,
83 Plus: 0x2B,
84 ",": 0x2c,
85 Comma: 0x2C,
86 "-": 0x2d,
87 Minus: 0x2D,
88 ".": 0x2e,
89 Period: 0x2E,
90 "/": 0x2f,
91 Slash: 0x2F,
92 ",": 0x3a,
93 Colon: 0x3A,
94 ";": 0x3b,
95 SemiColon: 0x3B,
96 "<": 0x3c,
97 Less: 0x3C,
98 "=": 0x3d,
99 Equals: 0x3D,
100 ">": 0x3e,
101 Greater: 0x3E,
102 "?": 0x3f,
103 Question: 0x3F,
104 "@": 0x40,
105 At: 0x40,
106 "[": 0x5b,
107 OpeningBracket: 0x5B,
108 "\\": 0x5c,
109 BackSlash: 0x5C,
110 "]": 0x5d,
111 ClosingBracket: 0x5D,
112 "^": 0x5e,
113 Caret: 0x5E,
114 "_": 0x5f,
115 Underscore: 0x5F,
116 "`": 0x60,
117 BackQuote: 0x60,
118 };
119
120 FunctionKeys = {
121 Space: 0x20,
122 Backspace: 0xFF08,
123 Tab: 0xFF09,
124 Linefeed: 0xFF0A,
125 Clear: 0xFF0B,
126 Return: 0xFF0D,
127 Pause: 0xFF13,
128 Sys_Req: 0xFF15,
129 "Esc": 0xff1b,
130 Escape: 0xFF1B,
131 "Del": 0xffff,
132 Delete: 0xFFFF,
133 Home: 0xFF50,
134 Left: 0xFF51,
135 Up: 0xFF52,
136 Right: 0xFF53,
137 Down: 0xFF54,
138 Prior: 0xFF55,
139 Page_Up: 0xFF55,
140 Next: 0xFF56,
141 Page_Down: 0xFF56,
142 End: 0xFF57,
143 Begin: 0xFF58,
144 Select: 0xFF60,
145 Print: 0xFF61,
146 Execute: 0xFF62,
147 Insert: 0xFF63,
148 Undo: 0xFF65,
149 Redo: 0xFF66,
150 Menu: 0xFF67,
151 Find: 0xFF68,
152 Cancel: 0xFF69,
153 Help: 0xFF6A,
154 Break: 0xFF6B,
155
156 # Any key binding using these would be pretty confusing …
157 Mode_switch: 0xFF7E,
158 Script_switch: 0xFF7E,
159 Num_Lock: 0xFF7F,
160 Scroll_Lock: 0xFF14,
161
162 # Keypad keys probably should not be used because their normal key equivalent will be passed by OSG AFAIK
163 KP_Space: 0xFF80,
164 KP_Tab: 0xFF89,
165 KP_Enter: 0xFF8D,
166 KP_F1: 0xFF91,
167 KP_F2: 0xFF92,
168 KP_F3: 0xFF93,
169 KP_F4: 0xFF94,
170 KP_Home: 0xFF95,
171 KP_Left: 0xFF96,
172 KP_Up: 0xFF97,
173 KP_Right: 0xFF98,
174 KP_Down: 0xFF99,
175 KP_Prior: 0xFF9A,
176 KP_Page_Up: 0xFF9A,
177 KP_Next: 0xFF9B,
178 KP_Page_Down: 0xFF9B,
179 KP_End: 0xFF9C,
180 KP_Begin: 0xFF9D,
181 KP_Insert: 0xFF9E,
182 KP_Delete: 0xFF9F,
183 KP_Equal: 0xFFBD,
184 KP_Multiply: 0xFFAA,
185 KP_Add: 0xFFAB,
186 KP_Separator: 0xFFAC,
187 KP_Subtract: 0xFFAD,
188 KP_Decimal: 0xFFAE,
189 KP_Divide: 0xFFAF,
190 KP_0: 0xFFB0,
191 KP_1: 0xFFB1,
192 KP_2: 0xFFB2,
193 KP_3: 0xFFB3,
194 KP_4: 0xFFB4,
195 KP_5: 0xFFB5,
196 KP_6: 0xFFB6,
197 KP_7: 0xFFB7,
198 KP_8: 0xFFB8,
199 KP_9: 0xFFB9,
200 F1: 0xFFBE,
201 F2: 0xFFBF,
202 F3: 0xFFC0,
203 F4: 0xFFC1,
204 F5: 0xFFC2,
205 F6: 0xFFC3,
206 F7: 0xFFC4,
207 F8: 0xFFC5,
208 F9: 0xFFC6,
209 F10: 0xFFC7,
210 F11: 0xFFC8,
211 F12: 0xFFC9,
212
213 # most keyboards only have F1 - F12, so better not use any of the below FunctionKeys
214 F13: 0xFFCA,
215 F14: 0xFFCB,
216 F15: 0xFFCC,
217 F16: 0xFFCD,
218 F17: 0xFFCE,
219 F18: 0xFFCF,
220 F19: 0xFFD0,
221 F20: 0xFFD1,
222 F21: 0xFFD2,
223 F22: 0xFFD3,
224 F23: 0xFFD4,
225 F24: 0xFFD5,
226 F25: 0xFFD6,
227 F26: 0xFFD7,
228 F27: 0xFFD8,
229 F28: 0xFFD9,
230 F29: 0xFFDA,
231 F30: 0xFFDB,
232 F31: 0xFFDC,
233 F32: 0xFFDD,
234 F33: 0xFFDE,
235 F34: 0xFFDF,
236 F35: 0xFFE0,
237
238 # Do not use the below keys !
239 Shift_L: 0xFFE1,
240 Shift_R: 0xFFE2,
241 Control_L: 0xFFE3,
242 Control_R: 0xFFE4,
243 Caps_Lock: 0xFFE5,
244 Shift_Lock: 0xFFE6,
245 Meta_L: 0xFFE7,
246 Meta_R: 0xFFE8,
247 Alt_L: 0xFFE9,
248 Alt_R: 0xFFEA,
249 #Super_L: 0xFFEB,
250 #Super_R: 0xFFEC,
251 #Hyper_L: 0xFFED,
252 #Hyper_R: 0xFFEE,
253 };
254
255 _ModifierKeys = {
256 Shift_L: 0x0001,
257 Shift_R: 0x0002,
258 Control_L: 0x0004,
259 Control_R: 0x0008,
260 Alt_L: 0x0010,
261 Alt_R: 0x0020,
262 Meta_L: 0x0040,
263 Meta_R: 0x0080,
264
265 # These do not seem to be available in OSG events
266 #Super_L: 0x0100,
267 #Super_R: 0x0200,
268 #Hyper_L: 0x0400,
269 #Hyper_R: 0x0800,
270 #Num_Lock: 0x1000,
271 Caps_Lock: 0x2000,
272 };
273
274 ModifierKeys = {
275 Ctrl: (_ModifierKeys.Control_L | _ModifierKeys.Control_R),
276 Shift: (_ModifierKeys.Shift_L | _ModifierKeys.Shift_R),
277 Alt: (_ModifierKeys.Alt_L | _ModifierKeys.Alt_R),
278 Meta: (_ModifierKeys.Meta_L | _ModifierKeys.Meta_R),
279 };
280
281 # These do not seem to be available in OSG events
282 #ModifierKeys.Super = (ModifierKeys.Super_L | ModifierKeys.Super_R);
283 #ModifierKeys.Hyper = (ModifierKeys.Hyper_L | ModifierKeys.Hyper_R);
284
285 var parseShortcut = func(s)
286 {
287 if (!s or !isstr(s) or size(s) == 0) {
288 return nil;
289 }
290
291 var baseKey = nil;
292 var modifiers = [];
293 var input = split("+", s);
294 foreach (var key; input) {
295 if (string.match(key, "<*>")) {
296 append(modifiers, substr(key, 1, size(key) - 2));
297 } else {
298 baseKey = key;
299 }
300 }
301
302 var modMask = 0;
303 foreach (var mod; modifiers) {
304 var mm = ModifierKeys[mod];
305 if (mm == nil) {
306 logprint(LOG_ALERT, "Unknown modifier key '" ~ mod ~ "' !");
307 } else {
308 modMask = modMask + mm;
309 }
310 }
311
312 var keyCode = PrintableKeys[baseKey];
313 if (keyCode == nil) {
314 keyCode = FunctionKeys[baseKey];
315 }
316
317 if (keyCode == nil) {
318 logprint(LOG_ALERT, "Unknown key '" ~ baseKey ~ "'");
319 return nil;
320 }
321
322 return [modMask, keyCode];
323 }
324
325
326 var findKeyName = func(key) {
327 foreach (var mod; keys(ModifierKeys)) {
328 if (ModifierKeys[mod] == key) {
329 return mod;
330 }
331 }
332 foreach (var printable; keys(PrintableKeys)) {
333 if (PrintableKeys[printable] == key) {
334 return printable;
335 }
336 }
337 foreach (var function; keys(FunctionKeys)) {
338 if (FunctionKeys[function] == key) {
339 return function;
340 }
341 }
342 return nil;
343 };
344
345 var namesToKeys = func(strings...)
346 {
347 var res = [];
348 foreach (var s; strings) {
349 var key = ModifierKeys[s] or PrintableKeys[s] or FunctionKeys[s];
350 if (!key) {
351 logprint(LOG_ALERT, "keyboard.namesToKeys: unknown key '" ~ key ~ "' - skipping !");
352 continue;
353 }
354 append(res, key);
355 }
356 return res;
357 };
358
359 # here we're extendin the built-in KeyBinding class defined in NasalCanvas.cxx,
360 # with some helper functions which rely on the tables defined above.
361
362 KeyBinding['fromShortcut'] = func(shortcutString, cb = nil)
363 {
364 if (!isstr(shortcutString)) {
365 return nil;
366 }
367
368 var res = KeyBinding.new();
369 var t = parseShortcut(shortcutString);
370
371 if (t) {
372 res.modifiers = t[0];
373 res.keyCode = t[1];
374 }
375
376 if (!isfunc(cb)) {
377 logprint(LOG_ALERT, "callback argument to KeyBinding.fromShortcut is not a function")
378 } else {
379 res.addBinding(cb);
380 }
381
382 return res;
383 };
384
385 KeyBinding['repr'] = func(kb)
386 {
387 if (!kb) {
388 return "";
389 }
390 var names = [];
391 foreach (var mod; keys(ModifierKeys)) {
392 if (kb.modifiers & ModifierKeys[mod]) {
393 append(names, "<" ~ mod ~ ">");
394 }
395 }
396 append(names, findKeyName(kb.keyCode));
397 return string.join(" + ", names);
398 };
399