Functions
- key.isDown(key)
- Returns the boolean state of the key. key is one of the key constants.
Callbacks
- keypressed(key)
- Gets called when a key is pressed. Argument is the key constant of the pressed key.
- keyreleased(key)
- Gets calles when a key is released. Argument is the key constants of the released key.
Key constants
The first table shows virtual key constants. These keys do not exist. You should not try to use them with the callbacks because they will never occur. Instead of e.g. key.SHIFT, test for key.LSHIFT or key.RSHIFT
They should be used with key.isDown() to test for some special keys which happen to be twice on a standard keyboard. key.isDown(key.SHIFT) will return true if either of the two shift keys is pressed.
key.SHIFT | either of the shift keys |
key.CTRL | either of the ctrl keys |
key.ALT | either of the alt keys |
key.META | either of the meta keys |
key.SUPER | either of the windows keys |
The following table shows all key constants usable with Scrupp.
key.BACKSPACE | '\b' | backspace |
key.TAB | '\t' | tab |
key.CLEAR | clear | |
key.RETURN | '\r' | return |
key.PAUSE | pause | |
key.ESCAPE | '^[' | escape |
key.SPACE | ' ' | space |
key.EXCLAIM | '!' | exclaim |
key.QUOTEDBL | '"' | quotedbl |
key.HASH | '#' | hash |
key.DOLLAR | '$' | dollar |
key.AMPERSAND | '&' | ampersand |
key.QUOTE | ''' | quote |
key.LEFTPAREN | '(' | left parenthesis |
key.RIGHTPAREN | ')' | right parenthesis |
key.ASTERISK | '*' | asterisk |
key.PLUS | '+' | plus sign |
key.COMMA | ',' | comma |
key.MINUS | '-' | minus sign |
key.PERIOD | '.' | period |
key.SLASH | '/' | forward slash |
key.0 | '0' | 0 |
key.1 | '1' | 1 |
key.2 | '2' | 2 |
key.3 | '3' | 3 |
key.4 | '4' | 4 |
key.5 | '5' | 5 |
key.6 | '6' | 6 |
key.7 | '7' | 7 |
key.8 | '8' | 8 |
key.9 | '9' | 9 |
key.COLON | ':' | colon |
key.SEMICOLON | ';' | semicolon |
key.LESS | '<' | less-than sign |
key.EQUALS | '=' | equals sign |
key.GREATER | '>' | greater-than sign |
key.QUESTION | '?' | question mark |
key.AT | '@' | at |
key.LEFTBRACKET | '[' | left bracket |
key.BACKSLASH | '\' | backslash |
key.RIGHTBRACKET | ']' | right bracket |
key.CARET | '^' | caret |
key.UNDERSCORE | '_' | underscore |
key.BACKQUOTE | '`' | grave |
key.a | 'a' | a |
key.b | 'b' | b |
key.c | 'c' | c |
key.d | 'd' | d |
key.e | 'e' | e |
key.f | 'f' | f |
key.g | 'g' | g |
key.h | 'h' | h |
key.i | 'i' | i |
key.j | 'j' | j |
key.k | 'k' | k |
key.l | 'l' | l |
key.m | 'm' | m |
key.n | 'n' | n |
key.o | 'o' | o |
key.p | 'p' | p |
key.q | 'q' | q |
key.r | 'r' | r |
key.s | 's' | s |
key.t | 't' | t |
key.u | 'u' | u |
key.v | 'v' | v |
key.w | 'w' | w |
key.x | 'x' | x |
key.y | 'y' | y |
key.z | 'z' | z |
key.DELETE | '^?' | delete |
key.KP0 | keypad 0 | |
key.KP1 | keypad 1 | |
key.KP2 | keypad 2 | |
key.KP3 | keypad 3 | |
key.KP4 | keypad 4 | |
key.KP5 | keypad 5 | |
key.KP6 | keypad 6 | |
key.KP7 | keypad 7 | |
key.KP8 | keypad 8 | |
key.KP9 | keypad 9 | |
key.KP_PERIOD | '.' | keypad period |
key.KP_DIVIDE | '/' | keypad divide |
key.KP_MULTIPLY | '*' | keypad multiply |
key.KP_MINUS | '-' | keypad minus |
key.KP_PLUS | '+' | keypad plus |
key.KP_ENTER | '\r' | keypad enter |
key.KP_EQUALS | '=' | keypad equals |
key.UP | up arrow | |
key.DOWN | down arrow | |
key.RIGHT | right arrow | |
key.LEFT | left arrow | |
key.INSERT | insert | |
key.HOME | home | |
key.END | end | |
key.PAGEUP | page up | |
key.PAGEDOWN | page down | |
key.F1 | F1 | |
key.F2 | F2 | |
key.F3 | F3 | |
key.F4 | F4 | |
key.F5 | F5 | |
key.F6 | F6 | |
key.F7 | F7 | |
key.F8 | F8 | |
key.F9 | F9 | |
key.F10 | F10 | |
key.F11 | F11 | |
key.F12 | F12 | |
key.F13 | F13 | |
key.F14 | F14 | |
key.F15 | F15 | |
key.NUMLOCK | numlock | |
key.CAPSLOCK | capslock | |
key.SCROLLOCK | scrollock | |
key.RSHIFT | right shift | |
key.LSHIFT | left shift | |
key.RCTRL | right ctrl | |
key.LCTRL | left ctrl | |
key.RALT | right alt | |
key.LALT | left alt | |
key.RMETA | right meta | |
key.LMETA | left meta | |
key.LSUPER | left windows key | |
key.RSUPER | right windows key | |
key.MODE | mode shift | |
key.HELP | help | |
key.PRINT | print-screen | |
key.SYSREQ | SysRq | |
key.BREAK | break | |
key.MENU | menu | |
key.POWER | power | |
key.EURO | euro |
The source of this table is the SDL man page for SDLKey.
Example
game.init("Keyboard Test", 600, 400, 32, false) main = { render = function(dt) -- we need at least an empty render function end, keypressed = function(k) -- check if a valid letter key is pressed; -- string.byte is a Lua function that returns -- the ASCII value of a char if k>=string.byte('a') and k<=string.byte('z') then -- if a letter key is pressed, then the argument k -- is a number (the ASCII code); -- string.char is a Lua function that returns -- the char of such a code local ch = string.char(k) -- check whether any of the two shift keys is pressed if key.isDown(key.SHIFT) then -- make the letter uppercase ch = string.upper(ch) end -- print the letter of the key; -- string.char is a Lua function that returns -- the char of a ASCII value print(ch .. " pressed") end -- the escape key exits the demo if k == key.ESCAPE then game.exit() end end, keyreleased = function(k) print("key released") end }