Keyboard and input
ui::input is one frame of pointer, wheel, text and key input. ui::input_of returns the one the context was begun with.
What input carries
- The pointer position, whether it is inside the window, and down, pressed, released and double click flags.
- The wheel,
wheel_xandwheel_y, already in logical units and signed the way the platform scrolls.wheel_precisemeans trackpad deltas rather than notches. The app shell sets it when a mac trackpad or smooth wheel sent any part of the frame's scroll, or on Windows when a wheel delta was not a whole notch, so lists and rollers can move by distance instead of by row. A trackpad moves one logical pixel per point everywhere: the shell turns its travel intowheel_xandwheel_ydirectly, and only mouse wheel notches are scaled by rows. wheel_points_y, the same vertical trackpad travel aswheel_yin logical pixels, one per point under the fingers, and 0 when the platform only sends notches.wheel_phase, what the trackpad gesture did this frame, asui::padbits:fingers1 while the fingers move,lifted2 when they lift,momentum4 while the system coasts after a lift, andmomentum_end8 when that stops. It comes from the macOS event phases and from Windows Direct Manipulation, and stays 0 on a platform that cannot tell.chars, the codepoints typed this frame, for text entry.keys, the keys pressed this frame, asui::vknumbers.- The
ctrl,shift,altandcmdmodifiers. context_pressed, a secondary click this frame: the right button, or control click on a mac.right_down, the secondary button held, where a mac control click counts. A press or release of it asks for a frame, and on Windows the window keeps the pointer captured while it is down, so a right drag keeps reporting outside the window.pinch, a trackpad pinch this frame as a scale step. Positive spreads the fingers. A pinch asks for a frame like the wheel does. It comes from a mac trackpad, and on Windows from a precision touchpad through Direct Manipulation. Under the shell a pinch zooms the page, sopinchstays 0 unless the element under it calledtake_pinchor the app setno_zoom. See pinch zoom.key_mods, the modifiers held as each key landed, one byte per key: ctrl 1, shift 2, alt 4, cmd 8. Null applies the four flags above to every key.now, monotonic wall seconds for the caret blink and click counting. Zero reads the clock.commitandcommit_len, IME text committed this frame, andpreedit,preedit_lenandpreedit_caret, the live composition. Both are UTF-8. See text editing and IME.
The arrays are counted by char_count and key_count. UI_INPUT_QUEUE, 32, is how many keys focus routing reads in one frame, and how many keys ui::post_key can queue. Keys past it in one frame are dropped with a warning.
Sideways scrolling
wheel_x uses the same units and sign as wheel_y: a notch is the same distance, and a positive value moves toward the start. The app shell fills it from:
| Source | Platform |
|---|---|
| Trackpad swipes and sideways mouse scrolling, from the event's horizontal delta | macOS |
Tilt wheels, from WM_MOUSEHWHEEL | Windows |
| Shift with the wheel, as every Windows app does. Shift with Ctrl stays a scale step. | Windows |
On a mac, a sideways delta with Ctrl held is ignored. Sideways input also sets wheel_precise the same way the vertical wheel does, and it wakes an idle app like any other input.
Who gets a key
A focused control takes the keys it claims before your app sees them. A slider takes the arrows, a text field takes typing and editing keys, and a button takes enter and space. What is left reaches ui::input as usual. While a blocking scope such as a modal is open, ui::captured is true, and app shortcuts should wait. See focus, scopes and modals.
ui::vk
The key numbers are the same on every platform, so one switch serves Windows and macOS. They follow the Windows virtual key values.
| Constant | Value | Constant | Value |
|---|---|---|---|
ui::vk::back | 0x08 | ui::vk::tab | 0x09 |
ui::vk::enter | 0x0d | ui::vk::escape | 0x1b |
ui::vk::space | 0x20 | ui::vk::page_up | 0x21 |
ui::vk::page_down | 0x22 | ui::vk::end | 0x23 |
ui::vk::home | 0x24 | ui::vk::left | 0x25 |
ui::vk::up | 0x26 | ui::vk::right | 0x27 |
ui::vk::down | 0x28 | ui::vk::insert | 0x2d |
ui::vk::del | 0x2e | ui::vk::f1 | 0x70 |
#include "isolenui/ui/ui.h"
// input carries this frame's typed characters and key presses, the keys are
// the ui::vk numbers on every platform so one table serves windows and macos
struct slide_nav {
uint32_t slide = 0;
uint32_t count = 1;
bool blank = false;
};
static void handle_keys(ui::ctx* u, slide_nav* nav)
{
const ui::input* in = ui::input_of(u);
for (uint32_t i = 0; i < in->key_count; ++i) {
switch (in->keys[i]) {
case ui::vk::right:
case ui::vk::page_down:
case ui::vk::space:
nav->slide = nav->slide + 1 < nav->count ? nav->slide + 1 : nav->slide;
break;
case ui::vk::left:
case ui::vk::page_up:
case ui::vk::back:
nav->slide = nav->slide ? nav->slide - 1 : 0;
break;
case ui::vk::home:
nav->slide = 0;
break;
case ui::vk::end:
nav->slide = nav->count - 1;
break;
case ui::vk::escape:
nav->blank = !nav->blank;
break;
default:
break;
}
}
// ctrl on windows and cmd on macos arrive as separate flags
const bool jump = (in->ctrl || in->cmd) && in->shift;
ui::textf(u, ui::mono(u), "slide %u of %u%s, %u chars typed", nav->slide + 1,
nav->count, jump ? ", jump held" : "", in->char_count);
}
// the wheel is already in logical pixels and signed the way the platform
// scrolls, precise means a trackpad rather than notches
static float wheel_zoom(const ui::input* in, float zoom)
{
const float step = in->wheel_precise ? 0.002f : 0.1f / 40.0f;
return (in->ctrl || in->cmd) ? zoom * (1.0f + in->wheel_y * step) : zoom;
}