ui::KeyEvent¶
Header: winui/core/Input.hpp.
One keyboard event, delivered to the focused element by Window::handle_key().
Text input is not a KeyEvent: typed characters arrive separately as UTF-8 text. A KeyEvent carries only the semantic Key plus the modifier state.
Example:
// In an element of your own, which gets the keys while it has focus:
class EditorControl : public ui::Element
{
protected:
bool key_override(const ui::KeyEvent& e) override
{
if (e.action != ui::KeyAction::Down)
{
return false;
}
if (e.key == ui::Key::Enter)
{
commit();
return true;
}
if (e.key == ui::Key::N && e.modifiers == ui::ModifierKeys{.ctrl = true, .shift = true}) // Ctrl+Shift+N
{
new_document();
return true;
}
return false;
}
private:
void commit();
void new_document();
};
// Or for the whole window, before the focused element sees the key:
ui::Property<std::string> search_text;
window.on_key_preview([&](const ui::KeyEvent& e)
{
if (e.action == ui::KeyAction::Down && e.key == ui::Key::Escape)
{
search_text = ""; // cancel the search
return ui::Handled; // it goes no further
}
return ui::NotHandled;
});
| Field | Type | Default | |
|---|---|---|---|
key |
Key |
Key::Unknown |
The key pressed or released. |
action |
KeyAction |
KeyAction::Down |
Press (including auto-repeat) or release. |
modifiers |
ModifierKeys |
The modifier keys held alongside key. Shift extends a TextBox selection and reverses Tab; Ctrl or Alt makes a shortcut. The key combinations the system owns (Alt+F4, Alt+Space, Alt+Tab, Alt+Esc) never arrive. |