2.4.1. Blocking keyboard input
This function waits for the user to press a key. It returns the code of the key pressed as well as the corresponding character. See TCOD_key_t.
If the flush parameter is true, every pending keypress event is discarded, then the function wait for a new keypress.
If flush is false, the function waits only if there are no pending keypress events, else it returns the first event in the keyboard buffer.
static TCOD_key_t TCODConsole::waitForKeypress(bool flush)
TCOD_key_t TCOD_console_wait_for_keypress(bool flush)
console_wait_for_keypress(flush)
static TCOD_key_t TCODConsole::waitForKeypress(bool flush)
tcod.console.waitForKeypress(flush)
Parameter | Description |
---|---|
flush | if true, all pending keypress events are flushed from the keyboard buffer. Else, return the first available event |
Example:
TCOD_key_t key = TCODConsole::waitForKeypress(true);
if ( key.c == 'i' ) { ... open inventory ... }
TCOD_key_t key = TCOD_console_wait_for_keypress(true);
if ( key.c == 'i' ) { ... open inventory ... }
key = libtcod.console_wait_for_keypress(True)
if key.c == ord('i') :
key = tcod.console.waitForKeypress(true)
if key.Character == 'i' then ... open inventory ... end
Waiting for any event (mouse or keyboard)
There's a more generic function that waits for an event from the user. The eventMask shows what events we're waiting for.
The return value indicate what event was actually triggered. Values in key and mouse structures are updated accordingly.
If flush is false, the function waits only if there are no pending events, else it returns the first event in the buffer.
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::waitForEvent(int eventMask, TCOD_key_t *key, TCOD_mouse_t *mouse, bool flush)
TCOD_event_t TCOD_sys_wait_for_event(int eventMask, TCOD_key_t *key, TCOD_mouse_t *mouse, bool flush)
sys_wait_for_event(eventMask,key,mouse,flush)
Parameter | Description |
---|---|
eventMask | event types to wait for (other types are discarded) |
key | updated in case of a key event. Can be null if eventMask contains no key event type |
mouse | updated in case of a mouse event. Can be null if eventMask contains no mouse event type |
flush | if true, all pending events are flushed from the buffer. Else, return the first available event |
Example:
TCOD_key_t key;
TCOD_mouse_t mouse;
TCOD_event_t ev = TCODSystem::waitForEvent(TCOD_EVENT_ANY,&key,&mouse,true);
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_wait_for_event(TCOD_EVENT_ANY,&key,&mouse,true);
if ( ev == TCOD_EVENT_KEY_PRESS && key.c == 'i' ) { ... open inventory ... }