Skip to content

ui::KeyCombo

Header: winui/core/Input.hpp.

A key and its modifiers, without the action it runs. Menu items, app-bar buttons and commands carry their shortcut in it. The key combination fires app-wide (Element::handle_global_shortcut()) and its text() is shown beside the item. Key::Unknown is no shortcut. Written the way the menu shows it, ui::Ctrl + ui::Shift + ui::Key::N, or as a bare key, ui::Key::F4. Alt key combinations work, except the ones the system owns (Alt+F4, Alt+Space, Alt+Tab, Alt+Esc), which never reach the window.

Example:

const ui::KeyCombo save = ui::Ctrl + ui::Key::S;

const std::string hint = save.text(); // "Ctrl+S", what a menu shows beside the label

// In a key handler: is this the key combination?
window.on_key_preview([save](const ui::KeyEvent& event)
{
    if (save.matches(event))
    {
        save_document();
        return ui::Handled;
    }
    return ui::NotHandled;
});

key

Key key = Key::Unknown

The key; Unknown means "no shortcut".

modifiers

ModifierKeys modifiers {}

Exactly the modifiers that must be held.

KeyCombo

constexpr KeyCombo() = default

No shortcut.

KeyCombo

constexpr KeyCombo(Key k)

The key alone, so a field takes ui::Key::F4.

KeyCombo

constexpr KeyCombo(Key k, ModifierKeys mods)

The key with mods held: ui::KeyCombo{ui::Key::S, {.ctrl = true}}.

empty

bool empty() const

True for Key::Unknown, i.e. no shortcut assigned.

matches

bool matches(const KeyEvent& e) const

True if e is a key-down of exactly this key combination: the same key and exactly the same modifiers. Always false for an empty() combination and for key-up events.

text

std::string text() const

"Ctrl+S" (shortcut_text()); empty for an empty() combination.