gpui-cc Course handbook
CHAPTER 04 / STAGE 15COURSE

Activate by key without hard-coding a keycode

Map native symbols to the same actions as pointer input.

key-activate
On this page

Learn: keys, symbols, modifiers, and actions are different

An X11 keycode identifies a physical key in a server keymap; a key symbol is the mapped meaning under current modifiers. The same TAB key can yield Tab or ISO_Left_Tab. Your framework should route semantic events such as “traverse previous” and “activate,” not expose physical numbers to the app.

Run it: inspect the server’s Tab mapping

inspect-keymap.cpp reads the XKB map, looks up the key by the name TAB, and sets a simulated Shift modifier in its own state. It does not inject any keyboard input into the server.

Terminal
mkdir -p build/examples
c++ -std=c++20 course/examples/inspect-keymap.cpp -o build/examples/inspect-keymap \
  $(pkg-config --cflags --libs xcb xkbcommon xkbcommon-x11)
./build/examples/inspect-keymap

On a standard Xvfb layout expect Unshifted: Tab and With Shift: ISO_Left_Tab. A real custom keymap may differ; use private xvfb-run -a for the reference case. The sample uses unique_ptr with custom deleters: each owner releases its resource in reverse declaration order. The keymap and state are destroyed before the XCB connection. Try it: in a scratch copy replace the two modifier cases with the unshifted case twice, and see the two identical symbols. Read the sample source before integrating.

Build it: translate and route through the existing action

Your challenge: Normalize physical input using the live server’s mapping. Tab/Shift+Tab traverse; Return or Space activates the focused eligible control exactly once per physical press, not on key repeat.

Select key press/release and focus events; negotiate XKB with xkb_x11_setup_xkb_extension, get the device ID, create a device keymap and state (xkb_x11_state_new_from_device). Use xkb_state_key_get_one_sym and effective modifiers; accept ISO_Left_Tab when mapped and map Shift+Tab to backwards. For a native design, keep the state in sync with server XKB state notifications and refresh on device keymap changes; the sample’s synthetic modifier update is not a replacement for those events. See libxkbcommon’s X11 integration guide.

Prefer XKB detectable autorepeat if your backend supports and verifies it, so repeated presses can be suppressed using a set of down keycodes that is cleared on release and deactivation. If unsupported, explicitly distinguish the server’s conventional release/press repeat pair or report the unsupported profile rather than silently double-activating. Test what the chosen server actually delivers. Dispatch activation through the same semantic action used by pointer input; there should not be a separate keyboard copy of the app mutation.

Check it

Replay normalized Tab, Shift+Tab, Return, Space, unrelated symbols, repeated presses, and deactivation. Confirm only the focused eligible control activates once for each physical press. Releasing and pressing again should activate again. Compare mouse and keyboard actions. On a desktop or isolated server with a keyboard test profile, confirm the app’s actual key events traverse and activate as specified; platform behavior is separate evidence from the normalized trace. Change a mapping in a disposable server profile when validating support for remapped keys.

Terminal
python3 course/check_core.py key-activate

Hints and troubleshooting

Hint: Shift+Tab jumps forwards, or holding Space repeats

Log keycode, logical symbol, effective modifiers, and whether the press was already tracked. Pay attention to which event releases the tracked key. On a missing dependency, check pkg-config --modversion xkbcommon-x11, not just base xkbcommon.