Key codes
Every key and mouse button the engine knows has a name on natiny.input, so
nothing has to be written as a bare number:
Lua
if natiny.input.get_key_down(natiny.input.KEY_W) then
y = y - speed * dt
end
if natiny.input.get_mouse_button_pressed(natiny.input.MOUSE_BUTTON_LEFT) then
fire()
end
The names are what get_key_down,
get_key_pressed, get_key_released and the
get_mouse_button_down functions take. Lifting them into locals
once, outside the loop, keeps the frame's table lookups down:
Lua
local KEY_W, KEY_S = natiny.input.KEY_W, natiny.input.KEY_S
local KEY_A, KEY_D = natiny.input.KEY_A, natiny.input.KEY_D
A key's value is a plain number, the same number on every platform, and the C
API spells the same constant NATINY_INPUT_KEY_W. Values run to 348, well
inside the 512 the engine tracks; anything outside that range simply reads as
not pressed rather than an error.
Printable keys - the value is the character's ASCII code
| Constant |
Value |
Key |
natiny.input.KEY_SPACE |
32 |
Space |
natiny.input.KEY_APOSTROPHE |
39 |
' |
natiny.input.KEY_COMMA |
44 |
, |
natiny.input.KEY_MINUS |
45 |
- |
natiny.input.KEY_PERIOD |
46 |
. |
natiny.input.KEY_SLASH |
47 |
/ |
natiny.input.KEY_0 |
48 |
0 |
natiny.input.KEY_1 |
49 |
1 |
natiny.input.KEY_2 |
50 |
2 |
natiny.input.KEY_3 |
51 |
3 |
natiny.input.KEY_4 |
52 |
4 |
natiny.input.KEY_5 |
53 |
5 |
natiny.input.KEY_6 |
54 |
6 |
natiny.input.KEY_7 |
55 |
7 |
natiny.input.KEY_8 |
56 |
8 |
natiny.input.KEY_9 |
57 |
9 |
natiny.input.KEY_SEMICOLON |
59 |
; |
natiny.input.KEY_EQUAL |
61 |
= |
natiny.input.KEY_A |
65 |
A |
natiny.input.KEY_B |
66 |
B |
natiny.input.KEY_C |
67 |
C |
natiny.input.KEY_D |
68 |
D |
natiny.input.KEY_E |
69 |
E |
natiny.input.KEY_F |
70 |
F |
natiny.input.KEY_G |
71 |
G |
natiny.input.KEY_H |
72 |
H |
natiny.input.KEY_I |
73 |
I |
natiny.input.KEY_J |
74 |
J |
natiny.input.KEY_K |
75 |
K |
natiny.input.KEY_L |
76 |
L |
natiny.input.KEY_M |
77 |
M |
natiny.input.KEY_N |
78 |
N |
natiny.input.KEY_O |
79 |
O |
natiny.input.KEY_P |
80 |
P |
natiny.input.KEY_Q |
81 |
Q |
natiny.input.KEY_R |
82 |
R |
natiny.input.KEY_S |
83 |
S |
natiny.input.KEY_T |
84 |
T |
natiny.input.KEY_U |
85 |
U |
natiny.input.KEY_V |
86 |
V |
natiny.input.KEY_W |
87 |
W |
natiny.input.KEY_X |
88 |
X |
natiny.input.KEY_Y |
89 |
Y |
natiny.input.KEY_Z |
90 |
Z |
natiny.input.KEY_LEFT_BRACKET |
91 |
[ |
natiny.input.KEY_BACKSLASH |
92 |
\ |
natiny.input.KEY_RIGHT_BRACKET |
93 |
] |
natiny.input.KEY_GRAVE_ACCENT |
96 |
` |
natiny.input.KEY_WORLD_1 |
161 |
Non-US key #1 |
natiny.input.KEY_WORLD_2 |
162 |
Non-US key #2 |
Editing and navigation
| Constant |
Value |
Key |
natiny.input.KEY_ESCAPE |
256 |
Escape |
natiny.input.KEY_ENTER |
257 |
Enter / Return |
natiny.input.KEY_TAB |
258 |
Tab |
natiny.input.KEY_BACKSPACE |
259 |
Backspace |
natiny.input.KEY_INSERT |
260 |
Insert |
natiny.input.KEY_DELETE |
261 |
Delete |
natiny.input.KEY_RIGHT |
262 |
Arrow right |
natiny.input.KEY_LEFT |
263 |
Arrow left |
natiny.input.KEY_DOWN |
264 |
Arrow down |
natiny.input.KEY_UP |
265 |
Arrow up |
natiny.input.KEY_PAGE_UP |
266 |
Page Up |
natiny.input.KEY_PAGE_DOWN |
267 |
Page Down |
natiny.input.KEY_HOME |
268 |
Home |
natiny.input.KEY_END |
269 |
End |
Locks and system keys
| Constant |
Value |
Key |
natiny.input.KEY_CAPS_LOCK |
280 |
Caps Lock |
natiny.input.KEY_SCROLL_LOCK |
281 |
Scroll Lock |
natiny.input.KEY_NUM_LOCK |
282 |
Num Lock |
natiny.input.KEY_PRINT_SCREEN |
283 |
Print Screen |
natiny.input.KEY_PAUSE |
284 |
Pause / Break |
Function keys
| Constant |
Value |
Key |
natiny.input.KEY_F1 |
290 |
F1 |
natiny.input.KEY_F2 |
291 |
F2 |
natiny.input.KEY_F3 |
292 |
F3 |
natiny.input.KEY_F4 |
293 |
F4 |
natiny.input.KEY_F5 |
294 |
F5 |
natiny.input.KEY_F6 |
295 |
F6 |
natiny.input.KEY_F7 |
296 |
F7 |
natiny.input.KEY_F8 |
297 |
F8 |
natiny.input.KEY_F9 |
298 |
F9 |
natiny.input.KEY_F10 |
299 |
F10 |
natiny.input.KEY_F11 |
300 |
F11 |
natiny.input.KEY_F12 |
301 |
F12 |
natiny.input.KEY_F13 |
302 |
F13 |
natiny.input.KEY_F14 |
303 |
F14 |
natiny.input.KEY_F15 |
304 |
F15 |
natiny.input.KEY_F16 |
305 |
F16 |
natiny.input.KEY_F17 |
306 |
F17 |
natiny.input.KEY_F18 |
307 |
F18 |
natiny.input.KEY_F19 |
308 |
F19 |
natiny.input.KEY_F20 |
309 |
F20 |
natiny.input.KEY_F21 |
310 |
F21 |
natiny.input.KEY_F22 |
311 |
F22 |
natiny.input.KEY_F23 |
312 |
F23 |
natiny.input.KEY_F24 |
313 |
F24 |
natiny.input.KEY_F25 |
314 |
F25 |
Keypad
| Constant |
Value |
Key |
natiny.input.KEY_KP_0 |
320 |
Keypad 0 |
natiny.input.KEY_KP_1 |
321 |
Keypad 1 |
natiny.input.KEY_KP_2 |
322 |
Keypad 2 |
natiny.input.KEY_KP_3 |
323 |
Keypad 3 |
natiny.input.KEY_KP_4 |
324 |
Keypad 4 |
natiny.input.KEY_KP_5 |
325 |
Keypad 5 |
natiny.input.KEY_KP_6 |
326 |
Keypad 6 |
natiny.input.KEY_KP_7 |
327 |
Keypad 7 |
natiny.input.KEY_KP_8 |
328 |
Keypad 8 |
natiny.input.KEY_KP_9 |
329 |
Keypad 9 |
natiny.input.KEY_KP_DECIMAL |
330 |
Keypad . |
natiny.input.KEY_KP_DIVIDE |
331 |
Keypad / |
natiny.input.KEY_KP_MULTIPLY |
332 |
Keypad * |
natiny.input.KEY_KP_SUBTRACT |
333 |
Keypad - |
natiny.input.KEY_KP_ADD |
334 |
Keypad + |
natiny.input.KEY_KP_ENTER |
335 |
Keypad Enter |
natiny.input.KEY_KP_EQUAL |
336 |
Keypad = |
Modifiers
| Constant |
Value |
Key |
natiny.input.KEY_LEFT_SHIFT |
340 |
Left Shift |
natiny.input.KEY_LEFT_CONTROL |
341 |
Left Ctrl |
natiny.input.KEY_LEFT_ALT |
342 |
Left Alt |
natiny.input.KEY_LEFT_SUPER |
343 |
Left Super / Win / Cmd |
natiny.input.KEY_RIGHT_SHIFT |
344 |
Right Shift |
natiny.input.KEY_RIGHT_CONTROL |
345 |
Right Ctrl |
natiny.input.KEY_RIGHT_ALT |
346 |
Right Alt / AltGr |
natiny.input.KEY_RIGHT_SUPER |
347 |
Right Super / Win / Cmd |
natiny.input.KEY_MENU |
348 |
Menu / context |
| Constant |
Value |
Key |
natiny.input.MOUSE_BUTTON_LEFT |
0 |
Left button |
natiny.input.MOUSE_BUTTON_RIGHT |
1 |
Right button |
natiny.input.MOUSE_BUTTON_MIDDLE |
2 |
Middle button - the wheel |
natiny.input.MOUSE_BUTTON_4 |
3 |
Fourth button |
natiny.input.MOUSE_BUTTON_5 |
4 |
Fifth button |
natiny.input.MOUSE_BUTTON_6 |
5 |
Sixth button |
natiny.input.MOUSE_BUTTON_7 |
6 |
Seventh button |
natiny.input.MOUSE_BUTTON_8 |
7 |
Eighth button |
Notes
- The printable keys carry their own ASCII code, which is why
KEY_A is 65
and not 0. That is a physical key on a US layout, not the character it
types - for text, read get_char_at instead, which gives what the
keyboard's own layout produced.
- The values follow GLFW, which is what the desktop backends report. On the
web the browser's
keyCode is translated to the same numbers, so a name
means the same key everywhere.
- Four constants never arrive from a browser, which has no
keyCode of its
own for them: KEY_KP_EQUAL, KEY_F25, KEY_WORLD_1 and KEY_WORLD_2.
- A browser also reports one code for each modifier pair, so on the web the
left-hand name stands for both:
KEY_LEFT_SHIFT is true for either Shift,
and KEY_RIGHT_SHIFT never fires. KEY_KP_ENTER arrives as KEY_ENTER
for the same reason.
KEY_PRINT_SCREEN reaches a desktop window on release only, which is what
the operating system passes on.
See also