File: keycode.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (323 lines) | stat: -rw-r--r-- 10,471 bytes parent folder | download | duplicates (2)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef AGS_SHARED_AC_KEYCODE_H
#define AGS_SHARED_AC_KEYCODE_H

#include "ags/shared/core/platform.h"
#include "ags/shared/core/types.h"

namespace AGS3 {

#define EXTENDED_KEY_CODE ('\0')
#define EXTENDED_KEY_CODE_MACOS ('?')

// Constant used to define Alt+Key codes
#define AGS_EXT_KEY_SHIFT  300
#define AGS_EXT_KEY_ALPHA(key)  (AGS_EXT_KEY_SHIFT + (key - eAGSKeyCodeCtrlA) + 1)

// These are based on eKeyCode values in AGS Script.
// The actual values are based on scan codes of the old backend (allegro 3 and/or 4),
// which in turn mostly match ASCII values (at least for ones below 128), including
// Ctrl + letter combination codes.
// More codes are added at much higher ranges, for example Alt + letter combo codes
// are defined as 300 + letter's order.
// It should be specifically noted that eAGSKeyCode is directly conversible to ASCII
// at the range of 1 - 128, and AGS script makes use of this.
// Another important thing to note is that letter codes are always sent into script
// callbacks (like "on_key_pressed") in capitalized form, and that's how they are
// declared in script API (that's why in these callbacks user would have to check
// the Shift key state if they want to know if it's A or Shift + A).
enum eAGSKeyCode {
	eAGSKeyCodeNone = 0,

	eAGSKeyCodeCtrlA = 1,
	eAGSKeyCodeCtrlB = 2,
	eAGSKeyCodeCtrlC = 3,
	eAGSKeyCodeCtrlD = 4,
	eAGSKeyCodeCtrlE = 5,
	eAGSKeyCodeCtrlF = 6,
	eAGSKeyCodeCtrlG = 7,
	eAGSKeyCodeCtrlH = 8,
	eAGSKeyCodeCtrlI = 9,
	eAGSKeyCodeCtrlJ = 10,
	eAGSKeyCodeCtrlK = 11,
	eAGSKeyCodeCtrlL = 12,
	eAGSKeyCodeCtrlM = 13,
	eAGSKeyCodeCtrlN = 14,
	eAGSKeyCodeCtrlO = 15,
	eAGSKeyCodeCtrlP = 16,
	eAGSKeyCodeCtrlQ = 17,
	eAGSKeyCodeCtrlR = 18,
	eAGSKeyCodeCtrlS = 19,
	eAGSKeyCodeCtrlT = 20,
	eAGSKeyCodeCtrlU = 21,
	eAGSKeyCodeCtrlV = 22,
	eAGSKeyCodeCtrlW = 23,
	eAGSKeyCodeCtrlX = 24,
	eAGSKeyCodeCtrlY = 25,
	eAGSKeyCodeCtrlZ = 26,

	eAGSKeyCodeBackspace = 8, // matches Ctrl + H
	eAGSKeyCodeTab = 9,       // matches Ctrl + I
	eAGSKeyCodeReturn = 13,   // matches Ctrl + M
	eAGSKeyCodeEscape = 27,

	/* printable chars - from eAGSKeyCodeSpace to eAGSKeyCode_z */
	eAGSKeyCodeSpace = 32,
	eAGSKeyCodeExclamationMark = 33,
	eAGSKeyCodeDoubleQuote = 34,
	eAGSKeyCodeHash = 35,
	eAGSKeyCodeDollar = 36,
	eAGSKeyCodePercent = 37,
	eAGSKeyCodeAmpersand = 38,
	eAGSKeyCodeSingleQuote = 39,
	eAGSKeyCodeOpenParenthesis = 40,
	eAGSKeyCodeCloseParenthesis = 41,
	eAGSKeyCodeAsterisk = 42,
	eAGSKeyCodePlus = 43,
	eAGSKeyCodeComma = 44,
	eAGSKeyCodeHyphen = 45,
	eAGSKeyCodePeriod = 46,
	eAGSKeyCodeForwardSlash = 47,

	eAGSKeyCode0 = 48,
	eAGSKeyCode1 = 49,
	eAGSKeyCode2 = 50,
	eAGSKeyCode3 = 51,
	eAGSKeyCode4 = 52,
	eAGSKeyCode5 = 53,
	eAGSKeyCode6 = 54,
	eAGSKeyCode7 = 55,
	eAGSKeyCode8 = 56,
	eAGSKeyCode9 = 57,

	eAGSKeyCodeColon = 58,
	eAGSKeyCodeSemiColon = 59,
	eAGSKeyCodeLessThan = 60,
	eAGSKeyCodeEquals = 61,
	eAGSKeyCodeGreaterThan = 62,
	eAGSKeyCodeQuestionMark = 63,
	eAGSKeyCodeAt = 64, // '@'

	/* Notice that default letter codes match capital ASCII letters */
	eAGSKeyCodeA = 65, // 'A'
	eAGSKeyCodeB = 66, // 'B', etc
	eAGSKeyCodeC = 67,
	eAGSKeyCodeD = 68,
	eAGSKeyCodeE = 69,
	eAGSKeyCodeF = 70,
	eAGSKeyCodeG = 71,
	eAGSKeyCodeH = 72,
	eAGSKeyCodeI = 73,
	eAGSKeyCodeJ = 74,
	eAGSKeyCodeK = 75,
	eAGSKeyCodeL = 76,
	eAGSKeyCodeM = 77,
	eAGSKeyCodeN = 78,
	eAGSKeyCodeO = 79,
	eAGSKeyCodeP = 80,
	eAGSKeyCodeQ = 81,
	eAGSKeyCodeR = 82,
	eAGSKeyCodeS = 83,
	eAGSKeyCodeT = 84,
	eAGSKeyCodeU = 85,
	eAGSKeyCodeV = 86,
	eAGSKeyCodeW = 87,
	eAGSKeyCodeX = 88,
	eAGSKeyCodeY = 89,
	eAGSKeyCodeZ = 90, // 'Z'

	eAGSKeyCodeOpenBracket = 91,
	eAGSKeyCodeBackSlash = 92,
	eAGSKeyCodeCloseBracket = 93,
	eAGSKeyCodeCaret = 94, // '^'
	eAGSKeyCodeUnderscore = 95,
	eAGSKeyCodeBackquote = 96, // '`'

	/* Small ASCII letter codes are declared here for consistency, but unused in script callbacks */
	eAGSKeyCode_a = 97, // 'a'
	eAGSKeyCode_b = 98, // 'b', etc
	eAGSKeyCode_c = 99,
	eAGSKeyCode_d = 100,
	eAGSKeyCode_e = 101,
	eAGSKeyCode_f = 102,
	eAGSKeyCode_g = 103,
	eAGSKeyCode_h = 104,
	eAGSKeyCode_i = 105,
	eAGSKeyCode_j = 106,
	eAGSKeyCode_k = 107,
	eAGSKeyCode_l = 108,
	eAGSKeyCode_m = 109,
	eAGSKeyCode_n = 110,
	eAGSKeyCode_o = 111,
	eAGSKeyCode_p = 112,
	eAGSKeyCode_q = 113,
	eAGSKeyCode_r = 114,
	eAGSKeyCode_s = 115,
	eAGSKeyCode_t = 116,
	eAGSKeyCode_u = 117,
	eAGSKeyCode_v = 118,
	eAGSKeyCode_w = 119,
	eAGSKeyCode_x = 120,
	eAGSKeyCode_y = 121,
	eAGSKeyCode_z = 122, // 'z'

	/* extended symbol codes */
	eAGSKeyCodeF1 = AGS_EXT_KEY_SHIFT + 59,
	eAGSKeyCodeF2 = AGS_EXT_KEY_SHIFT + 60,
	eAGSKeyCodeF3 = AGS_EXT_KEY_SHIFT + 61,
	eAGSKeyCodeF4 = AGS_EXT_KEY_SHIFT + 62,
	eAGSKeyCodeF5 = AGS_EXT_KEY_SHIFT + 63,
	eAGSKeyCodeF6 = AGS_EXT_KEY_SHIFT + 64,
	eAGSKeyCodeF7 = AGS_EXT_KEY_SHIFT + 65,
	eAGSKeyCodeF8 = AGS_EXT_KEY_SHIFT + 66,
	eAGSKeyCodeF9 = AGS_EXT_KEY_SHIFT + 67,
	eAGSKeyCodeF10 = AGS_EXT_KEY_SHIFT + 68,
	eAGSKeyCodeF11 = AGS_EXT_KEY_SHIFT + 133,
	eAGSKeyCodeF12 = AGS_EXT_KEY_SHIFT + 134,

	eAGSKeyCodeHome = AGS_EXT_KEY_SHIFT + 71,
	eAGSKeyCodeUpArrow = AGS_EXT_KEY_SHIFT + 72,
	eAGSKeyCodePageUp = AGS_EXT_KEY_SHIFT + 73,
	eAGSKeyCodeLeftArrow = AGS_EXT_KEY_SHIFT + 75,
	eAGSKeyCodeNumPad5 = AGS_EXT_KEY_SHIFT + 76,
	eAGSKeyCodeRightArrow = AGS_EXT_KEY_SHIFT + 77,
	eAGSKeyCodeEnd = AGS_EXT_KEY_SHIFT + 79,
	eAGSKeyCodeDownArrow = AGS_EXT_KEY_SHIFT + 80,
	eAGSKeyCodePageDown = AGS_EXT_KEY_SHIFT + 81,
	eAGSKeyCodeInsert = AGS_EXT_KEY_SHIFT + 82,
	eAGSKeyCodeDelete = AGS_EXT_KEY_SHIFT + 83,

	// [sonneveld] These are only used by debugging and abort keys.
	// They're based on allegro4 codes ...
	eAGSKeyCodeAltV = AGS_EXT_KEY_ALPHA(eAGSKeyCodeV),
	eAGSKeyCodeAltX = AGS_EXT_KEY_ALPHA(eAGSKeyCodeX),
	eAGSKeyCodeAltY = AGS_EXT_KEY_ALPHA(eAGSKeyCodeY),
	eAGSKeyCodeAltZ = AGS_EXT_KEY_ALPHA(eAGSKeyCodeZ),

	// The beginning of "service key list": mod keys and other special keys
	// not normally intended to affect the default game logic
	eAGSKeyCode_FirstServiceKey = 391,

	// not certain if necessary anymore (and not certain what was the origin of this value)
	eAGSKeyCodeAltTab = AGS_EXT_KEY_SHIFT + 99,

	// Mod-key codes
	// *probably* made-up numbers, not derived from allegro scan codes.
	eAGSKeyCodeLShift = 403,
	eAGSKeyCodeRShift = 404,
	eAGSKeyCodeLCtrl = 405,
	eAGSKeyCodeRCtrl = 406,
	eAGSKeyCodeLAlt = 407,

	// [sonneveld]
	// The following are the AGS_EXT_KEY_SHIFT, derived from applying arithmetic to the original keycodes.
	// These do not have a corresponding ags key enum, do not appear in the manual and may not be accessible because of OS contraints.
	eAGSKeyCodeRAlt = 420,
	// TODO: judging that above works (at least on Win), following might also work,
	// but idk which ones may be necessary; still keeping here this excerpt from an old code
	// if they'd want to be restored (also add them to script API then!).
	// Also see allegro 4's keyboard.h, where these were declared.
	/*
	case 392: __allegro_KEY_PRTSCR
	case 393: __allegro_KEY_PAUSE
	case 394: __allegro_KEY_ABNT_C1  // The ABNT_C1 (Brazilian) key
	case 395: __allegro_KEY_YEN)
	case 396: __allegro_KEY_KANA
	case 397: __allegro_KEY_CONVERT
	case 398: __allegro_KEY_NOCONVERT
	case 400: __allegro_KEY_CIRCUMFLEX
	case 402: __allegro_KEY_KANJI
	case 421: __allegro_KEY_LWIN
	case 422: __allegro_KEY_RWIN
	case 423: __allegro_KEY_MENU
	case 424: __allegro_KEY_SCRLOCK
	case 425: __allegro_KEY_NUMLOCK
	case 426: __allegro_KEY_CAPSLOCK
	*/

	// Mask defines the key code position if packed in the int32;
	// takes only 12 bits, as minimal necessary to accommodate historical codes.
	eAGSKeyMask = 0x0FFF
};

// AGS key modifiers
enum eAGSKeyMod {
	eAGSModLShift = 0x00010000,
	eAGSModRShift = 0x00020000,
	eAGSModLCtrl  = 0x00040000,
	eAGSModRCtrl  = 0x00080000,
	eAGSModLAlt   = 0x00100000,
	eAGSModRAlt   = 0x00200000,
	eAGSModNum    = 0x00400000,
	eAGSModCaps   = 0x00800000,

	// Mask defines the key mod position if packed in the int32;
	// the upper 8 bits are reserved for "input type" codes;
	// potentially may take 4 bits below (4th pos), as KeyMask takes only 12.
	eAGSModMask   = 0x00FF0000
};

// Combined key code and a textual representation in UTF-8
struct KeyInput {
	const static size_t UTF8_ARR_SIZE = 5;

	eAGSKeyCode Key = eAGSKeyCodeNone; // actual key code
	eAGSKeyCode CompatKey = eAGSKeyCodeNone; // old-style key code, combined with mods
	int         Mod = 0; // key modifiers
	int         UChar = 0; // full character value (supports unicode)
	char        Text[UTF8_ARR_SIZE]{}; // character in a string format

	KeyInput() = default;
};

// AGS own mouse button codes;
// These correspond to MouseButton enum in script and plugin API (sans special values)
enum eAGSMouseButton
{
	kMouseNone = 0,
	kMouseLeft = 1,
	kMouseRight = 2,
	kMouseMiddle = 3,
	kNumMouseButtons
};

// Tells if the AGS keycode refers to the modifier key (ctrl, alt, etc)
inline bool IsAGSModKey(eAGSKeyCode keycode) {
	return (keycode >= eAGSKeyCodeLShift && keycode <= eAGSKeyCodeLAlt) || keycode == eAGSKeyCodeRAlt;
}

// Tells if the AGS keycode refers to the service key (modifier, PrintScreen and similar);
// this lets distinct keys that normally should not affect the game
inline bool IsAGSServiceKey(eAGSKeyCode keycode) {
	return keycode >= eAGSKeyCode_FirstServiceKey;
}

// Converts eAGSKeyCode to script API code, for "on_key_press" and similar callbacks
eAGSKeyCode AGSKeyToScriptKey(eAGSKeyCode keycode);
// Converts eAGSKeyCode to ASCII text representation with the range check; returns 0 on failure
// Not unicode compatible.
char AGSKeyToText(eAGSKeyCode keycode);

} // namespace AGS3

#endif