struct KeyboardEvent
A single keyboard input event, either a key press, a key release, or one codepoint of insertable text. Events preserve the exact order they were produced in, including how text and keys interleave.
Instance Fields and Properties
| Key key | The key for press and release events, and none for text events. Mouse buttons arrive here too, as the mouse key values. |
| KeyMod modifiers | The modifier keys held when this event was produced. A modifier’s own press event includes itself, its release event does not. |
| string Text | This event’s text, as a string. Emoji and other codepoints outside the Basic Multilingual Plane don’t fit in a single C# char, so this is the safe way to read one. Empty for key events. |
| KeyboardEventType type | What kind of event this is, and which of the fields below apply. |
Examples
Reading the keyboard event queue
Input.KeyboardConsume reads this frame’s key presses, releases, and
text in the exact order the user produced them, which is the right
foundation for custom text editing. Text events carry the layout and
language sensitive characters to insert, while key events carry the
editing intent that text can’t express, and each auto-repeat of a held
key is its own press event.
Reading consumes, so a focused UI.Input earlier in the frame will
empty the queue. If observing is all you need, Input.KeyboardEventAt
reads by index without consuming anything.
static string typed = "";
static void ReadKeyboardEvents()
{
while (Input.KeyboardConsume(out KeyboardEvent e))
{
switch (e.type)
{
case KeyboardEventType.Text:
// Text may be two chars, for emoji and other codepoints
// too large for a single C# char.
typed += e.Text;
break;
case KeyboardEventType.KeyPress:
if (e.key == Key.Backspace && typed.Length > 0)
{
// Erase the whole codepoint, which may be two chars
int last = char.IsLowSurrogate(typed[typed.Length-1]) ? 2 : 1;
typed = typed.Remove(typed.Length - last);
}
break;
}
}
}
Found an issue with these docs, or have some additional questions? Create an Issue on Github!