libtcoddocumentation

2.4.2. Non blocking keyboard input

This function checks if the user has pressed a key. It returns the code of the key pressed as well as the corresponding character. See TCOD_key_t. If the user didn't press a key, this function returns the key code TCODK_NONE (NoKey for C# and Lua).
Note that key repeat only results in TCOD_KEY_PRESSED events.

static TCOD_key_t TCODConsole::checkForKeypress(int flags=TCOD_KEY_RELEASED)

TCOD_key_t key = TCOD_console_check_for_keypress(TCOD_KEY_PRESSED);
if
( key.vk == TCODK_NONE ) return; // no key pressed
if ( key.c == 'i' ) { ... open inventory ... }

key = libtcod.console_check_for_keypress()
if
key.vk == libtcod.KEY_NONE return # no key pressed
if key.c == ord('i') :

static TCODKey TCODConsole::checkForKeypress(int flags)

tcod.console.checkForKeypress(flags)

ParameterDescription
flagsA filter for key events (C# and Lua in parenthesis):
TCOD_KEY_PRESSED (KeyPressed) : only keypress events are returned
TCOD_KEY_RELEASED (KeyReleased): only key release events are returnes
TCOD_KEY_PRESSED|TCOD_KEY_RELEASED (KeyPressed+KeyReleased): events of both types are returned.
Example:

TCOD_key_t key = TCODConsole::checkForKeypress();
if
( key.vk == TCODK_NONE ) return; // no key pressed
if ( key.c == 'i' ) { ... open inventory ... }

key = tcod.console.checkForKeypress()
if
key.KeyCode == tcod.NoKey then return end -- no key pressed
if key.Character == 'i' then ... open inventory ... end


You can also get the status of any special key at any time with :

static bool TCODConsole::isKeyPressed(TCOD_keycode_t key)

bool TCOD_console_is_key_pressed(TCOD_keycode_t key)

console_is_key_pressed(key)

static bool TCODConsole::isKeyPressed(TCODKeyCode key)

tcod.console.isKeyPressed(key)

ParameterDescription
keyAny key code defined in keycode_t except TCODK_CHAR (Char) and TCODK_NONE (NoKey)

Checking for any event (mouse or keyboard)

There's a more generic function that checks if an event from the user is in the buffer. The eventMask shows what events we're waiting for.
The return value indicate what event was actually found. Values in key and mouse structures are updated accordingly.

typedef enum {
        TCOD_EVENT_KEY_PRESS
=1,
        TCOD_EVENT_KEY_RELEASE
=2,
        TCOD_EVENT_KEY
=TCOD_EVENT_KEY_PRESS|TCOD_EVENT_KEY_RELEASE,
        TCOD_EVENT_MOUSE_MOVE
=4,
        TCOD_EVENT_MOUSE_PRESS
=8,
        TCOD_EVENT_MOUSE_RELEASE
=16,
        TCOD_EVENT_MOUSE
=TCOD_EVENT_MOUSE_MOVE|TCOD_EVENT_MOUSE_PRESS|TCOD_EVENT_MOUSE_RELEASE,
        TCOD_EVENT_ANY
=TCOD_EVENT_KEY|TCOD_EVENT_MOUSE,
    }
TCOD_event_t;
    static
TCOD_event_t TCODSystem::checkForEvent(int eventMask, TCOD_key_t *key, TCOD_mouse_t *mouse)

TCOD_event_t TCOD_sys_check_for_event(int eventMask, TCOD_key_t *key, TCOD_mouse_t *mouse)

sys_check_for_event(eventMask,key,mouse)

ParameterDescription
eventMaskevent types to wait for (other types are discarded)
keyupdated in case of a key event. Can be null if eventMask contains no key event type
mouseupdated in case of a mouse event. Can be null if eventMask contains no mouse event type
Example:

TCOD_key_t key;
TCOD_mouse_t
mouse;
TCOD_event_t
ev = TCODSystem::checkForEvent(TCOD_EVENT_ANY,&key,&mouse);
if
( ev == TCOD_EVENT_KEY_PRESS && key.c == 'i' ) { ... open inventory ... }

TCOD_key_t key;
TCOD_mouse_t
mouse;
TCOD_event_t
ev = TCOD_sys_check_for_event(TCOD_EVENT_ANY,&key,&mouse);
if
( ev == TCOD_EVENT_KEY_PRESS && key.c == 'i' ) { ... open inventory ... }