-
Notifications
You must be signed in to change notification settings - Fork 0
Input
Input in the BearLibTerminal is handled with read, read str, has input, state and check functions.
This function returns the next event from input queue:
int key = terminal_read();
if (key == TK_ESCAPE) { // Quit }
If there is no more events in the queue, read will wait for an event to come. If this blocking behaviour is not desirable, you can check has input before calling read.
Similar to read but does not remove the event from the internal queue. The next peek or read call will return the same value. This is non-blocking function: if the queue is empty, it will not wait and return 0.
This function does a simple blocking read of a string without any parsing. User input is displayed at specified coordinates and also limited to specified length so it can be used in game interface. The function displays user input within current layer and restores the scene before return. It is an application's responsibility to keep the result on screen.
The function returns the size of a string if user has confirmed input by pressing Enter, or TK_INPUT_CANCELLED if operation was cancelled by pressing ESCAPE or closing the window.
wchar_t s[32] = L"Hello";
if (terminal_read_wstr(2, 1, s, sizeof(s)-1) >= 0) { terminal_wprintf(L"You entered: "%ls"", s); }
If read_str does not fit your needs, see TK CHAR/TK WCHAR states allowing for manual text input.
This function returns a boolean value indicating immediate input availability. If has_input returns true, the next call to read is guaranteed not to block.
while (terminal_has_input()) { int key = terminal_read(); }
This function queries the current numeric value of a library property. For input it is used to get different event properties, see the table below.
int key = terminal_read();
if (key == TK_MOUSE_MOVE) { int x = terminal_state(TK_MOUSE_X); int y = terminal_state(TK_MOUSE_Y); }
Note that values returned by state function may not reflect the exact real-time value of a property, but the “current” one in respect to the input queue and already dequeued events. More...
This is a simple wrapper around state function. In a lot of programming languages numerical and boolean types are not compatible so checking logically boolean properties with state may become unnecessary verbose. So you can use check to test some property.
int key = terminal_read();
if (key == TK_Z) { if (terminal_check(TK_SHIFT)) { // 'Z': zap a wand } else { // 'z': cast a spell } }
These options are configured by the set function:
terminal_set ( "input:" "cursor-symbol = 0x1F," "cursor-blink-rate = 500," "precise-mouse = false," "mouse-cursor = true," "filter=[keyboard];" );
Character/tile used as a cursor by read str function. Default is “_” (underscore) and can be changed to match application design, e. g. to full square or vertical bar.
Speed at which read str's cursor blinks, in milliseconds. Default is 500, i. e. twice a second.
A boolean flag controlling how mouse movement events are generated. When false, an event is generated only when cursor is moved from one cell to another. When true, any mouse movement generates an event. Default is false.
A boolean flag controlling mouse cursor visibility. Default is true.
It is a list of events the application is interested in. All other will be silently processed by the library, thus read will return only events from this list:
input.filter = [keyboard, mouse+]
A name in the list may specify:
- an event group: 'keyboard', 'mouse', 'keypad', 'arrows'.
- a single event (name as in the table below, but without 'TK_' prefix; case-insensitive).
- a set of alphanumeric keys, e. g. 'wasd' or '0123456789'; obviously it must differ from event group names.
Mentioning the events enables reading them. If there is a '+' (plus sign) added to the name, both keypresses and keyreleases will be read.
Default filter is 'keyboard'. To enable the mouse support, set the filter to 'keyboard, mouse'. System events like TK_CLOSE and TK_RESIZED are always enabled.
There is no input filtering if the list is empty. Therefore filtering is disabled by assigning 'none' or an empty list to the input.filter option.
| Group | Group | Description |
|---|---|---|
| Alphanumeric | TK_A … TK_Z | Event : indicates that corresponding key was pressed or released. If key was released, its code is combined with TK_KEY_RELEASED flag (more...). State: current state of the key (see input queue). |
| TK_0 … TK_9 | ||
| TK_SPACE | ||
| TK_MINUS | - | |
| TK_EQUALS | = | |
| TK_LBRACKET | [ | |
| TK_RBRACKET | ] | |
| TK_BACKSLASH | / | |
| TK_SEMICOLON | ; | |
| TK_APOSTROPHE | ' | |
| TK_GRAVE | ` | |
| TK_COMMA | , | |
| TK_PERIOD | . | |
| TK_SLASH | \ | |
| Functional, navigation, modifiers | TK_F1 … TK_F12 | |
| TK_RETURN | ||
| TK_ESCAPE | ||
| TK_BACKSPACE | ||
| TK_TAB | ||
| TK_PAUSE | ||
| TK_INSERT | ||
| TK_HOME | ||
| TK_PAGEUP | ||
| TK_DELETE | ||
| TK_END | ||
| TK_PAGEDOWN | ||
| TK_RIGHT | ||
| TK_LEFT | ||
| TK_DOWN | ||
| TK_UP | ||
| TK_SHIFT | ||
| TK_CONTROL | ||