27. Keyboard operation#
Every app built with the library can be used without a mouse. The library does most of it
(ui/kbnav.rs, the widgets); an app adds a few marks where its own
layout means something. python tools/kbwalk.py build/win/APP.exe [--keys f6] [--pre "cmd N"]
presses a key again and again (headless) and lists where the focus goes: unreachable widgets,
a lost focus, a focus outside its window, widgets without a semantic record.
What the library gives.
| Keys | Effect |
|---|---|
| Tab / Shift+Tab | next / previous Tab stop of the window (the top layer: an open dialog or flyout keeps it); Ctrl+Tab is left to the app. The stops of one pane stay together (a dock group: its tab strip, its buttons, its panel). When the focused widget went away, Tab goes on from its place. |
| arrows, Home / End | inside a focus group (one Tab stop): radios of one variable (the selection follows), tool buttons of one variable, the swatches of one box, focus_group_begin groups (tool bars, segment rows, button grids); one row or column wraps, a grid moves to the nearest member |
| Space / Enter | act on the focused widget; check boxes, radios and switches only take Space (Enter is the dialog's default button) |
| Esc | the widget's (a text box leaves the focus), then UIC_ESCAPE / the dialog's close; a tool window that got the keys by F6 gives them back |
| F6 / Shift+F6 | next / previous pane of the window (dock groups, the document area, both sides of a split view, focus_pane_begin), past the last one the next window: the tool windows borrow the keys from the main window until Esc, F6 or a click elsewhere |
| Ctrl+F6 / Ctrl+Shift+F6 | next / previous window directly (main window, tool windows, dialog windows that are not modal) |
| F8 / Shift+F8 | the window's splitters (split views, dock sides and groups): arrows move the focused one by 8 DIP, Shift+arrows by 1, Home / End to the ends; Enter, Esc or F8 past the last go back |
| menu key / Shift+F10 | context_menu of the focused widget (a focused list: its selected row) at the widget's lower left corner with the first row focused; a widget without one gets a right click (EV_RCLICK) and menu_popup opens at the widget |
| Alt, F10, Alt+letter | the menu bar (§6); accelerators of the command table everywhere |
| lists, grids, trees | Up / Down / Home / End / PgUp / PgDn select, Enter = double click (EV_DOUBLE), Space = the selected row's first check box (lists) or its check (trees); LF_REORDER lists: Ctrl+Up / Ctrl+Down / Ctrl+Home / Ctrl+End move the selected row (EV_DRAG with from / to, as a drop); the widgets of a custom row are Tab stops in the selected row only |
| sliders, number boxes, the colour picker | arrows / PgUp / PgDn / Home / End change the value; the picker's spectrum, hue and alpha bars are Tab stops (Shift = 10 steps) |
A dialog window starts with the focus on its first control (no ring until a key is used;
Esc closes it at once even when that control is a text box); a content dialog moves the
focus into itself and gives it back when it closes, popups and menus give it back to the
widget that had it, and a closed tool window or dialog window gives the keys back to its
owner. The focus ring is the style's focus_ring (every style draws one; high contrast: the
text colour) and appears only after a key was used.
What an app does.
- Use the library's widgets; a custom widget built on
behave/hit_rectpassesHB_FOCUS(it is a Tab stop and takes the focus on a press), reacts toEV_ACT(Space / Enter) and callsui.focus_ring_draw(ev)after its painting.HB_NOTAB: focusable but no Tab stop;HB_CURRENT: the member a group is entered at;HB_SPACE: Space acts, Enter does not;HB_OWNKEYS: Space and Enter are the widget's own;HB_KEYS: every key is the widget's while it has the focus, Tab, F6 and the accelerators too (a canvas that shows a web page, section 5: it takes them withui.key_take()and gives back the focus itself);HB_SPLITTER: a splitter F8 reaches --ui.splitter_keys(ev, along_x)says how far (DIP) the arrows move it. Give it asemrecord. - Mark tool bars and button grids as one Tab stop:
ui.focus_group_begin(id)..ui.focus_group_end()(id 0: the current box). A selection that follows the focus (tabs) asksui.group_arrived(id). - Mark the parts of a window the user switches between with F6:
ui.focus_pane_begin(id)..ui.focus_pane_end()(dock areas and split views do it themselves). - A decoration that looks like a control is no Tab stop:
ui.next_flags(ST_NOFOCUS)before a button or swatch. - Do not take F6, F8, Tab or the menu key as accelerators unless the app needs them (an accelerator wins: JFPainter keeps paint.net's F5 .. F8 for its windows, Ctrl+F6 still switches them).
- Test it:
key tab,key ctrl+f6,expect S focused,expect window ID keys(§12), andtools/kbwalk.pyfor the whole ring.
// a tool bar: one Tab stop, the arrows move between the buttons
ui.box_begin(0, BOX_H | BOX_XCENTER, 4);
ui.focus_group_begin(0);
for &(id, icon, tip) in TOOLS.iter() {
if ui.icon_button(id, icon, tip) & EV_ACT != 0 {
ui.cmd_run(id, 0);
}
}
ui.focus_group_end();
ui.box_end();
// a custom swatch grid: focusable cells, a ring, Space = click, menu key = right click
ui.focus_group_begin(ID_GRID);
for i in 0..n {
let ev = ui.hit_rect(ID_CELL0 + i, x(i), y(i), s, s, HB_FOCUS);
paint_cell(ui, i);
ui.focus_ring_draw(ev);
ui.sem(K_SWATCH, ID_CELL0 + i, Some(&label(i)), 0);
}
ui.focus_group_end();