Input

RmlUi receives mouse and keyboard input from an RmlInputHandler, a DataNode attached below the mouse watcher. The handler collects the pointer position, button presses, key presses and text input that flow through the data graph, and each region it is assigned to (RmlRegion.set_input_handler()) feeds them to its context every frame, translated into the region’s own pixel coordinates. One handler may serve several regions.

PT(RmlInputHandler) handler = new RmlInputHandler("rml-input");
window->get_mouse().attach_new_node(handler);
region->set_input_handler(handler);

When the pointer leaves the window the handler sends RmlUi a mouse-leave, so hover and active states are cleared rather than left latched.

A region receives the input that arrives after it is given the handler. A handler that serves several regions gives each all of it, in each region’s own coordinates.

The keyboard mapping is RmlInputHandler.get_rml_key(), from an engine ButtonHandle to RmlUi’s key identifier; it returns 0 for buttons RmlUi has no name for.

Text input

Keystrokes are queued as they arrive during the frame and are delivered to the context – and thus to the focused element – when the region updates, during the cull traversal. Two things follow:

  • Calling RmlElement.focus() on a text field in response to the key that opened it hands that same key to the field. Defer the focus by one frame, taskMgr.doMethodLater(0, ...) runs on the next.

  • The engine’s own key events keep arriving whatever RmlUi has focused. A form is most reliably submitted from the engine’s "enter" event, reading the field’s RmlElement.value, rather than from the document’s submit event.

Touch screens

On a touch screen (GraphicsWindow.has_touch_screen()) the finger that drives the window’s pointer is handed to RmlUi as a touch, through RmlUi’s own touch input (Context::ProcessTouchStart and friends), rather than as a mouse:

  • A tap presses and clicks what it lands on, also when the finger goes down and up within a single frame.

  • A drag that starts on a scrollable element (overflow: auto or scroll) scrolls it, and when the finger lifts while moving the content carries on with RmlUi’s inertia. Once the finger has moved more than a few pixels the press is no longer a click, so a list can be scrolled from one of its buttons.

  • When the finger lifts, the pointer leaves the interface: no :hover style stays behind on the last element touched.

  • One finger at a time: while a touch is down, other fingers are ignored – they do not press anything, nor move the touch. The touch follows its own finger even when another finger has become the window’s pointer.

The handler tells a touch from a mouse press by the window’s fingers (GraphicsWindow.get_num_touches()); a mouse press on the same device is a mouse press, and desktop mouse input is unchanged. A tap that comes and goes within one frame is not seen as a finger and arrives as a mouse click, which has the same effect.

Gamepad navigation

A gamepad can move through a document and press its buttons. It is turned on per handler (and thus per region) by giving the handler the gamepads to read:

InputDeviceManager *mgr = InputDeviceManager::get_global_ptr();
InputDeviceSet pads = mgr->get_devices(InputDevice::DeviceClass::GAMEPAD);
for (size_t i = 0; i < pads.size(); ++i) {
  handler->add_gamepad(pads[i]);
}

The handler reads the devices’ buttons and left stick every frame without taking their events, so the game can still attach the same device to the data graph (base.attachInputDevice) – while a menu is up, the game should ignore what the gamepad does. RmlInputHandler.add_gamepad() turns RmlInputHandler.gamepad_navigation on; with it on, gamepad buttons that reach the handler any other way (through the data graph, or RmlInputHandler.button_down()) navigate as well. The on-screen TouchGamepad is a gamepad like any other.

D-pad, left stick

move the focus up, down, left, right

A (face_a)

Enter: clicks the focused element

B (face_b)

Escape: a keydown with the escape key

right / left shoulder

the next / previous element in tab order

A held direction repeats, after 0.4 seconds and then every 0.12 (RmlInputHandler.set_nav_repeat()). The focus moves the way RmlUi’s keyboard navigation moves it, so the document decides:

  • Only focusable elements take the focus: give buttons and the like tab-index: auto. With nothing focused yet, the first direction focuses the first element in tab order.

  • The nav properties (nav-up, nav-right, nav-down, nav-left, or the nav shorthand) choose where each direction goes: auto finds the nearest element that way on the screen, horizontal and vertical restrict that to a row or a column, tree-order follows the tab order, and #id names the element. On an element without them a direction steps through the tab order (down and right forward), unless the element uses the arrow key itself.

  • An element focused by the gamepad (or the keyboard) has the :focus-visible pseudo-class, as well as :focus: style it to show where the focus is.

button { tab-index: auto; nav: auto; }
button:focus-visible { border: 2dp #ffd75e; }

B arrives in the document as Escape, so a keydown listener can go back a screen:

The keyboard already navigates the same way: the arrow keys follow the nav properties, Tab and Shift-Tab the tab order, and Enter or Space clicks.

Feeding input yourself

The handler’s pointer_move(), pointer_leave(), button_down(), button_up(), keystroke(), wheel() and touch_start(), touch_move(), touch_end() and touch_cancel() give it input that did not come through the data graph – a pointer the application maps onto an interface in the scene, or a test. Positions are in window pixels, y down; they are delivered with the rest when the region next updates.

The mouse cursor

RmlUi documents can ask for a cursor (cursor: pointer and friends); RmlContext.enable_mouse_cursor() decides whether such requests reach the window. RmlContext.hover_element and RmlContext.focus_element tell what the pointer is over and what has keyboard focus; RmlContext.get_element_at_point() hit-tests any point in region pixels.

Regions on off-screen buffers

The handler transmits window coordinates. For a region on the main window, or on a buffer whose texture fills the window, that is the right thing; for an interface rendered onto an object in the scene, as in the console sample, the mapping from the pointer to the object’s surface is the application’s – the sample sidesteps it by forwarding key events from the engine to the document and not using the mouse at all.