ScummVM API documentation
keycode.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef AGS_SHARED_AC_KEYCODE_H
23 #define AGS_SHARED_AC_KEYCODE_H
24 
25 #include "ags/shared/core/platform.h"
26 #include "ags/shared/core/types.h"
27 
28 namespace AGS3 {
29 
30 #define EXTENDED_KEY_CODE ('\0')
31 #define EXTENDED_KEY_CODE_MACOS ('?')
32 
33 // Constant used to define Alt+Key codes
34 #define AGS_EXT_KEY_SHIFT 300
35 #define AGS_EXT_KEY_ALPHA(key) (AGS_EXT_KEY_SHIFT + (key - eAGSKeyCodeCtrlA) + 1)
36 
37 // These are based on eKeyCode values in AGS Script.
38 // The actual values are based on scan codes of the old backend (allegro 3 and/or 4),
39 // which in turn mostly match ASCII values (at least for ones below 128), including
40 // Ctrl + letter combination codes.
41 // More codes are added at much higher ranges, for example Alt + letter combo codes
42 // are defined as 300 + letter's order.
43 // It should be specifically noted that eAGSKeyCode is directly conversible to ASCII
44 // at the range of 1 - 128, and AGS script makes use of this.
45 // Another important thing to note is that letter codes are always sent into script
46 // callbacks (like "on_key_pressed") in capitalized form, and that's how they are
47 // declared in script API (that's why in these callbacks user would have to check
48 // the Shift key state if they want to know if it's A or Shift + A).
49 enum eAGSKeyCode {
50  eAGSKeyCodeNone = 0,
51 
52  eAGSKeyCodeCtrlA = 1,
53  eAGSKeyCodeCtrlB = 2,
54  eAGSKeyCodeCtrlC = 3,
55  eAGSKeyCodeCtrlD = 4,
56  eAGSKeyCodeCtrlE = 5,
57  eAGSKeyCodeCtrlF = 6,
58  eAGSKeyCodeCtrlG = 7,
59  eAGSKeyCodeCtrlH = 8,
60  eAGSKeyCodeCtrlI = 9,
61  eAGSKeyCodeCtrlJ = 10,
62  eAGSKeyCodeCtrlK = 11,
63  eAGSKeyCodeCtrlL = 12,
64  eAGSKeyCodeCtrlM = 13,
65  eAGSKeyCodeCtrlN = 14,
66  eAGSKeyCodeCtrlO = 15,
67  eAGSKeyCodeCtrlP = 16,
68  eAGSKeyCodeCtrlQ = 17,
69  eAGSKeyCodeCtrlR = 18,
70  eAGSKeyCodeCtrlS = 19,
71  eAGSKeyCodeCtrlT = 20,
72  eAGSKeyCodeCtrlU = 21,
73  eAGSKeyCodeCtrlV = 22,
74  eAGSKeyCodeCtrlW = 23,
75  eAGSKeyCodeCtrlX = 24,
76  eAGSKeyCodeCtrlY = 25,
77  eAGSKeyCodeCtrlZ = 26,
78 
79  eAGSKeyCodeBackspace = 8, // matches Ctrl + H
80  eAGSKeyCodeTab = 9, // matches Ctrl + I
81  eAGSKeyCodeReturn = 13, // matches Ctrl + M
82  eAGSKeyCodeEscape = 27,
83 
84  /* printable chars - from eAGSKeyCodeSpace to eAGSKeyCode_z */
85  eAGSKeyCodeSpace = 32,
86  eAGSKeyCodeExclamationMark = 33,
87  eAGSKeyCodeDoubleQuote = 34,
88  eAGSKeyCodeHash = 35,
89  eAGSKeyCodeDollar = 36,
90  eAGSKeyCodePercent = 37,
91  eAGSKeyCodeAmpersand = 38,
92  eAGSKeyCodeSingleQuote = 39,
93  eAGSKeyCodeOpenParenthesis = 40,
94  eAGSKeyCodeCloseParenthesis = 41,
95  eAGSKeyCodeAsterisk = 42,
96  eAGSKeyCodePlus = 43,
97  eAGSKeyCodeComma = 44,
98  eAGSKeyCodeHyphen = 45,
99  eAGSKeyCodePeriod = 46,
100  eAGSKeyCodeForwardSlash = 47,
101 
102  eAGSKeyCode0 = 48,
103  eAGSKeyCode1 = 49,
104  eAGSKeyCode2 = 50,
105  eAGSKeyCode3 = 51,
106  eAGSKeyCode4 = 52,
107  eAGSKeyCode5 = 53,
108  eAGSKeyCode6 = 54,
109  eAGSKeyCode7 = 55,
110  eAGSKeyCode8 = 56,
111  eAGSKeyCode9 = 57,
112 
113  eAGSKeyCodeColon = 58,
114  eAGSKeyCodeSemiColon = 59,
115  eAGSKeyCodeLessThan = 60,
116  eAGSKeyCodeEquals = 61,
117  eAGSKeyCodeGreaterThan = 62,
118  eAGSKeyCodeQuestionMark = 63,
119  eAGSKeyCodeAt = 64, // '@'
120 
121  /* Notice that default letter codes match capital ASCII letters */
122  eAGSKeyCodeA = 65, // 'A'
123  eAGSKeyCodeB = 66, // 'B', etc
124  eAGSKeyCodeC = 67,
125  eAGSKeyCodeD = 68,
126  eAGSKeyCodeE = 69,
127  eAGSKeyCodeF = 70,
128  eAGSKeyCodeG = 71,
129  eAGSKeyCodeH = 72,
130  eAGSKeyCodeI = 73,
131  eAGSKeyCodeJ = 74,
132  eAGSKeyCodeK = 75,
133  eAGSKeyCodeL = 76,
134  eAGSKeyCodeM = 77,
135  eAGSKeyCodeN = 78,
136  eAGSKeyCodeO = 79,
137  eAGSKeyCodeP = 80,
138  eAGSKeyCodeQ = 81,
139  eAGSKeyCodeR = 82,
140  eAGSKeyCodeS = 83,
141  eAGSKeyCodeT = 84,
142  eAGSKeyCodeU = 85,
143  eAGSKeyCodeV = 86,
144  eAGSKeyCodeW = 87,
145  eAGSKeyCodeX = 88,
146  eAGSKeyCodeY = 89,
147  eAGSKeyCodeZ = 90, // 'Z'
148 
149  eAGSKeyCodeOpenBracket = 91,
150  eAGSKeyCodeBackSlash = 92,
151  eAGSKeyCodeCloseBracket = 93,
152  eAGSKeyCodeCaret = 94, // '^'
153  eAGSKeyCodeUnderscore = 95,
154  eAGSKeyCodeBackquote = 96, // '`'
155 
156  /* Small ASCII letter codes are declared here for consistency, but unused in script callbacks */
157  eAGSKeyCode_a = 97, // 'a'
158  eAGSKeyCode_b = 98, // 'b', etc
159  eAGSKeyCode_c = 99,
160  eAGSKeyCode_d = 100,
161  eAGSKeyCode_e = 101,
162  eAGSKeyCode_f = 102,
163  eAGSKeyCode_g = 103,
164  eAGSKeyCode_h = 104,
165  eAGSKeyCode_i = 105,
166  eAGSKeyCode_j = 106,
167  eAGSKeyCode_k = 107,
168  eAGSKeyCode_l = 108,
169  eAGSKeyCode_m = 109,
170  eAGSKeyCode_n = 110,
171  eAGSKeyCode_o = 111,
172  eAGSKeyCode_p = 112,
173  eAGSKeyCode_q = 113,
174  eAGSKeyCode_r = 114,
175  eAGSKeyCode_s = 115,
176  eAGSKeyCode_t = 116,
177  eAGSKeyCode_u = 117,
178  eAGSKeyCode_v = 118,
179  eAGSKeyCode_w = 119,
180  eAGSKeyCode_x = 120,
181  eAGSKeyCode_y = 121,
182  eAGSKeyCode_z = 122, // 'z'
183 
184  /* extended symbol codes */
185  eAGSKeyCodeF1 = AGS_EXT_KEY_SHIFT + 59,
186  eAGSKeyCodeF2 = AGS_EXT_KEY_SHIFT + 60,
187  eAGSKeyCodeF3 = AGS_EXT_KEY_SHIFT + 61,
188  eAGSKeyCodeF4 = AGS_EXT_KEY_SHIFT + 62,
189  eAGSKeyCodeF5 = AGS_EXT_KEY_SHIFT + 63,
190  eAGSKeyCodeF6 = AGS_EXT_KEY_SHIFT + 64,
191  eAGSKeyCodeF7 = AGS_EXT_KEY_SHIFT + 65,
192  eAGSKeyCodeF8 = AGS_EXT_KEY_SHIFT + 66,
193  eAGSKeyCodeF9 = AGS_EXT_KEY_SHIFT + 67,
194  eAGSKeyCodeF10 = AGS_EXT_KEY_SHIFT + 68,
195  eAGSKeyCodeF11 = AGS_EXT_KEY_SHIFT + 133,
196  eAGSKeyCodeF12 = AGS_EXT_KEY_SHIFT + 134,
197 
198  eAGSKeyCodeHome = AGS_EXT_KEY_SHIFT + 71,
199  eAGSKeyCodeUpArrow = AGS_EXT_KEY_SHIFT + 72,
200  eAGSKeyCodePageUp = AGS_EXT_KEY_SHIFT + 73,
201  eAGSKeyCodeLeftArrow = AGS_EXT_KEY_SHIFT + 75,
202  eAGSKeyCodeNumPad5 = AGS_EXT_KEY_SHIFT + 76,
203  eAGSKeyCodeRightArrow = AGS_EXT_KEY_SHIFT + 77,
204  eAGSKeyCodeEnd = AGS_EXT_KEY_SHIFT + 79,
205  eAGSKeyCodeDownArrow = AGS_EXT_KEY_SHIFT + 80,
206  eAGSKeyCodePageDown = AGS_EXT_KEY_SHIFT + 81,
207  eAGSKeyCodeInsert = AGS_EXT_KEY_SHIFT + 82,
208  eAGSKeyCodeDelete = AGS_EXT_KEY_SHIFT + 83,
209 
210  // [sonneveld] These are only used by debugging and abort keys.
211  // They're based on allegro4 codes ...
212  eAGSKeyCodeAltV = AGS_EXT_KEY_ALPHA(eAGSKeyCodeV),
213  eAGSKeyCodeAltX = AGS_EXT_KEY_ALPHA(eAGSKeyCodeX),
214  eAGSKeyCodeAltY = AGS_EXT_KEY_ALPHA(eAGSKeyCodeY),
215  eAGSKeyCodeAltZ = AGS_EXT_KEY_ALPHA(eAGSKeyCodeZ),
216 
217  // The beginning of "service key list": mod keys and other special keys
218  // not normally intended to affect the default game logic
219  eAGSKeyCode_FirstServiceKey = 391,
220 
221  // not certain if necessary anymore (and not certain what was the origin of this value)
222  eAGSKeyCodeAltTab = AGS_EXT_KEY_SHIFT + 99,
223 
224  // Mod-key codes
225  // *probably* made-up numbers, not derived from allegro scan codes.
226  eAGSKeyCodeLShift = 403,
227  eAGSKeyCodeRShift = 404,
228  eAGSKeyCodeLCtrl = 405,
229  eAGSKeyCodeRCtrl = 406,
230  eAGSKeyCodeLAlt = 407,
231 
232  // [sonneveld]
233  // The following are the AGS_EXT_KEY_SHIFT, derived from applying arithmetic to the original keycodes.
234  // These do not have a corresponding ags key enum, do not appear in the manual and may not be accessible because of OS contraints.
235  eAGSKeyCodeRAlt = 420,
236  // TODO: judging that above works (at least on Win), following might also work,
237  // but idk which ones may be necessary; still keeping here this excerpt from an old code
238  // if they'd want to be restored (also add them to script API then!).
239  // Also see allegro 4's keyboard.h, where these were declared.
240  /*
241  case 392: __allegro_KEY_PRTSCR
242  case 393: __allegro_KEY_PAUSE
243  case 394: __allegro_KEY_ABNT_C1 // The ABNT_C1 (Brazilian) key
244  case 395: __allegro_KEY_YEN)
245  case 396: __allegro_KEY_KANA
246  case 397: __allegro_KEY_CONVERT
247  case 398: __allegro_KEY_NOCONVERT
248  case 400: __allegro_KEY_CIRCUMFLEX
249  case 402: __allegro_KEY_KANJI
250  case 421: __allegro_KEY_LWIN
251  case 422: __allegro_KEY_RWIN
252  case 423: __allegro_KEY_MENU
253  case 424: __allegro_KEY_SCRLOCK
254  case 425: __allegro_KEY_NUMLOCK
255  case 426: __allegro_KEY_CAPSLOCK
256  */
257 
258  // Mask defines the key code position if packed in the int32;
259  // takes only 12 bits, as minimal necessary to accommodate historical codes.
260  eAGSKeyMask = 0x0FFF
261 };
262 
263 // AGS key modifiers
264 enum eAGSKeyMod {
265  eAGSModLShift = 0x00010000,
266  eAGSModRShift = 0x00020000,
267  eAGSModLCtrl = 0x00040000,
268  eAGSModRCtrl = 0x00080000,
269  eAGSModLAlt = 0x00100000,
270  eAGSModRAlt = 0x00200000,
271  eAGSModNum = 0x00400000,
272  eAGSModCaps = 0x00800000,
273 
274  // Mask defines the key mod position if packed in the int32;
275  // the upper 8 bits are reserved for "input type" codes;
276  // potentially may take 4 bits below (4th pos), as KeyMask takes only 12.
277  eAGSModMask = 0x00FF0000
278 };
279 
280 // Combined key code and a textual representation in UTF-8
281 struct KeyInput {
282  const static size_t UTF8_ARR_SIZE = 5;
283 
284  eAGSKeyCode Key = eAGSKeyCodeNone; // actual key code
285  eAGSKeyCode CompatKey = eAGSKeyCodeNone; // old-style key code, combined with mods
286  int Mod = 0; // key modifiers
287  int UChar = 0; // full character value (supports unicode)
288  char Text[UTF8_ARR_SIZE]{}; // character in a string format
289 
290  KeyInput() = default;
291 };
292 
293 // AGS own mouse button codes;
294 // These correspond to MouseButton enum in script and plugin API (sans special values)
295 enum eAGSMouseButton
296 {
297  kMouseNone = 0,
298  kMouseLeft = 1,
299  kMouseRight = 2,
300  kMouseMiddle = 3,
301  kNumMouseButtons
302 };
303 
304 // Tells if the AGS keycode refers to the modifier key (ctrl, alt, etc)
305 inline bool IsAGSModKey(eAGSKeyCode keycode) {
306  return (keycode >= eAGSKeyCodeLShift && keycode <= eAGSKeyCodeLAlt) || keycode == eAGSKeyCodeRAlt;
307 }
308 
309 // Tells if the AGS keycode refers to the service key (modifier, PrintScreen and similar);
310 // this lets distinct keys that normally should not affect the game
311 inline bool IsAGSServiceKey(eAGSKeyCode keycode) {
312  return keycode >= eAGSKeyCode_FirstServiceKey;
313 }
314 
315 // Converts eAGSKeyCode to script API code, for "on_key_press" and similar callbacks
316 eAGSKeyCode AGSKeyToScriptKey(eAGSKeyCode keycode);
317 // Converts eAGSKeyCode to ASCII text representation with the range check; returns 0 on failure
318 // Not unicode compatible.
319 char AGSKeyToText(eAGSKeyCode keycode);
320 
321 } // namespace AGS3
322 
323 #endif
Definition: keycode.h:281
Definition: ags.h:40