Skip to content

Input and Shortcuts

Dustin Horne edited this page Mar 5, 2026 · 1 revision

Input and Shortcuts

Namespace: ParentElement.RichText.Core.Input

Classes and structures for keyboard event handling, character mapping, and shortcut registration.


ShortcutHandler

Maintains a map of Shortcut → Func<Task> and executes actions on demand. The editor's Shortcuts property exposes the instance shared by DocumentController.

public class ShortcutHandler

Methods

Method Returns Description
Map(Shortcut, Func<Task>) void Register an async action for a key combination. Re-mapping an existing shortcut replaces the previous action
Execute(Shortcut) Task<bool> Execute the action registered for the shortcut. Returns true if a match was found

Example

var shortcuts = editor.Shortcuts;

// Ctrl+S → save
shortcuts.Map(new Shortcut(KeyCode.S, control: true), async () =>
{
    await SaveAsync();
});

// Ctrl+Shift+Z → redo (overrides any existing mapping)
shortcuts.Map(new Shortcut(KeyCode.Z, shift: true, control: true), async () =>
{
    await editor.Redo();
});

// Execute manually (also called internally by OnKeyEvent)
bool handled = await shortcuts.Execute(keyInfo.AsShortcut());

Shortcut

An immutable key + modifier combination used as the key type in ShortcutHandler.

public struct Shortcut

Constructor

public Shortcut(KeyCode baseKey, bool shift = false, bool control = false, bool alt = false)

Properties

Property Type Description
BaseKey KeyCode The primary key
ControlKey bool Whether Ctrl is part of the combination
ShiftKey bool Whether Shift is part of the combination
AltKey bool Whether Alt is part of the combination

Common Shortcuts

new Shortcut(KeyCode.B, control: true)          // Ctrl+B
new Shortcut(KeyCode.Z, control: true)          // Ctrl+Z (undo)
new Shortcut(KeyCode.Z, shift: true, control: true) // Ctrl+Shift+Z (redo)
new Shortcut(KeyCode.C, control: true)          // Ctrl+C (copy)
new Shortcut(KeyCode.V, control: true)          // Ctrl+V (paste)
new Shortcut(KeyCode.A, control: true)          // Ctrl+A (select all)

KeyInfo

A snapshot of a single keyboard event, including the key, modifier state, and resolved character.

public struct KeyInfo

Constructor

public KeyInfo(KeyCode keyCode, bool shift, bool control, bool alt)

Properties

Property Type Description
KeyCode KeyCode The logical key that was pressed
Shift bool Whether Shift was held
Control bool Whether Ctrl was held
Alt bool Whether Alt was held
Character char? Unicode character produced, or null when Ctrl or Alt is held
HasCommandModifiers bool true when Ctrl or Alt is held
HasModifiers bool true when any modifier (Shift, Ctrl, Alt) is held
IsPrintableCharacter bool true when the key produces a visible glyph without Ctrl/Alt

Methods

Method Returns Description
AsShortcut() Shortcut Create a Shortcut representing the same key + modifier state

Example

// In your UI key handler:
async void OnKeyDown(Key platformKey, KeyModifiers mods)
{
    var info = new KeyInfo(
        keyCode: MapPlatformKey(platformKey),
        shift:   mods.HasFlag(KeyModifiers.Shift),
        control: mods.HasFlag(KeyModifiers.Control),
        alt:     mods.HasFlag(KeyModifiers.Alt));

    bool handled = await editor.OnKeyEvent(info);
}

KeyMap

A static utility class that maps KeyCode values to Unicode characters.

public static class KeyMap

Methods

Method Returns Description
IsPrintableCharacter(KeyCode, bool shiftModifier = false) bool Returns true if the key can produce a visible character. Digit keys return false when shiftModifier is true
GetCharacter(KeyCode, bool shift, bool capsLock = false) char? Returns the Unicode character for the key/modifier combination, or null if the key is non-printable
IsModifierKey(KeyCode) bool Returns true for Shift, Ctrl, and Alt key codes (all variants)

Character Mappings

Key No Shift Shift
A–Z a–z A–Z
D0–D9 0–9 (null — not printable with shift)
OemMinus - _
OemPlus = +
OemOpenBrackets [ {
OemCloseBrackets ] }
OemSemicolon ; :
OemQuotes ' "
OemComma , <
OemPeriod . >
OemQuestion / ?
OemTilde ` ~
Space

KeyCode

A comprehensive enum of logical key codes with ASCII-compatible integer values.

public enum KeyCode

Key Groups

Group Members
Control keys Back (8), Tab (9), Enter (13), Escape (27), Delete (46), Insert (45)
Modifiers Shift, Control, Alt, LShiftKey, RShiftKey, LControlKey, RControlKey, LAltKey, RAltKey, CapsLock
Navigation Left, Right, Up, Down, Home, End, PageUp, PageDown
Digits D0–D9 (ASCII 48–57)
Letters A–Z (ASCII 65–90)
NumPad NumPad0–NumPad9, Multiply, Add, Subtract, Divide, Separator
Function F1–F24
OEM / Punctuation OemSemicolon, OemPlus, OemComma, OemMinus, OemPeriod, OemQuestion, OemTilde, OemOpenBrackets, OemPipe, OemCloseBrackets, OemQuotes, OemBackslash

Special Values

Value Integer Description
None -1 Represents an unknown or unset key
Space 32 Spacebar

See also: IDocumentController · DocumentController

Clone this wiki locally