Keys and focus#
CEUI is driven by the keypad. One widget at a time has focus, the arrow keys move it, and [enter] activates it.
The keys#
Key |
Meaning |
|---|---|
Arrows |
Move focus, unless the focused widget uses that arrow |
|
Activate the focused widget |
|
Close the top modal or menu. In a text field it clears the text first. Otherwise it is left for your program, usually as “back” |
|
The five softkeys, |
|
Cycle a text field’s mode |
|
Delete in text fields and numbers |
|
Page up and down in a list, or in a scroll area that has no focusable widget |
Arrows and [del] repeat while held: the first repeat comes after UI_REPEAT_DELAY_MS (400 ms), then one every
UI_REPEAT_MS (60 ms). Every frame handles one key event. Presses that arrive during a long frame are queued, up to
4.
Which widgets use which arrows#
An arrow that the focused widget does not use moves focus. Each widget declares the arrows it uses:
Widget |
Arrows it keeps |
|---|---|
Slider, choice, number, tabs, text field |
Left and right |
List |
Left and right for paging, up or down while the selection can still move that way |
Text area |
Left and right, up or down while the caret can still move between lines |
Scroll area with no focusable widget |
Left and right for paging |
Everything else |
None |
So in a list, [down] moves the selection until the last row, and the next [down] leaves the list.
Choosing the focus yourself#
ui_focus_here() gives focus to the next widget. Use it once, for example to start a form on its first field:
if (form.focus_name) {
ui_focus_here();
form.focus_name = false;
}
ui_input("Name", form.name, sizeof form.name, UI_INPUT_UPPER);
If you call ui_focus_here() in the frame a layer closes, it wins over the restored focus.
ui_focused() says whether the widget just placed has focus. Use it for help text that follows the focus:
ui_input("Password", form.password, sizeof form.password, UI_INPUT_SECRET);
if (ui_focused()) ui_caption("At least 4 characters.");
Ids#
Each focusable widget has an id, so CEUI can find the focused widget again in the next frame. The id is:
the address of the widget’s state variable when it has one (
&sound; for a radio button, the address plus the option);otherwise a hash of its label, for buttons;
mixed with the id of the container it is in. Ids stay the same when widgets above appear or disappear, so focus does not jump.
Two cases need help. A button whose label changes would get a new id each time and lose focus, and two buttons with
the same label in one container would share an id. ui_next_id(n) sets the id of the next widget instead:
ui_next_id(DOWNLOAD_ID);
if (ui_button(downloading ? "Pause" : "Download")) downloading = !downloading;
The number only has to be unique among the widgets in the same container.
Disabling widgets#
ui_enabled(false) draws every following widget in its disabled colors and makes it ignore keys, until
ui_enabled(true). Disabled widgets cannot take focus, so the arrows skip them. Every frame starts enabled.
ui_check("Lock the choices", &locked);
ui_enabled(!locked);
ui_choice("Mode", &mode, modes, 3);
ui_enabled(true);
The lock check box disables the choices below it.#
Reading keys yourself#
ui_key() returns the key event of this frame if no widget used it, or 0. ui_key_pressed(key) returns true and
uses the key up when it matches, so no other code sees it afterwards. Key codes are the ones of os_GetCSC
(sk_Up, sk_Enter, sk_Clear, sk_Graph, from <ti/getcsc.h>, which ui.h includes).
Check your shortcuts after the widgets:
while (ui_frame()) {
ui_titlebar(screen->title, NULL);
screen->show();
if (ui_key_pressed(sk_Clear)) screen = &home;
}
A widget that used the key has already set it to 0, so [clear] in a text field empties the field first, and only
[clear] on an empty field reaches your code.
Keys during long work#
Presses are read while CEUI waits, every fourth widget and before every rectangle it draws. A press that starts and
ends while your own code runs for a long time between widgets is missed. Call ui_listen() from that code now and
then to catch it. See long work between widgets.