Class KeyboardEventArgs
The data for a key event or a typed character. A key event carries the key and its modifiers; text input carries Character.
public sealed class KeyboardEventArgs : EventArgs
- Inheritance
-
KeyboardEventArgs
- Inherited Members
Constructors
KeyboardEventArgs(Key, bool, bool, bool, bool, bool)
Creates the data for a key event. The modifiers default to not held.
public KeyboardEventArgs(Key key, bool isShiftPressed = false, bool isAltPressed = false, bool isCtrlPressed = false, bool isCommandPressed = false, bool isRepeat = false)
Parameters
keyKeyisShiftPressedboolisAltPressedboolisCtrlPressedboolisCommandPressedboolisRepeatbool
KeyboardEventArgs(char)
Creates the data for a typed character.
public KeyboardEventArgs(char character)
Parameters
characterchar
Properties
Character
The character typed. Only text input carries one; for a key event it is '\0'.
public char Character { get; }
Property Value
Handled
Set to true by a handler that has used the event, so that later handlers can skip it.
public bool Handled { get; set; }
Property Value
IsAltPressed
Whether Alt was held.
public bool IsAltPressed { get; }
Property Value
IsCommandPressed
Command on macOS, Windows key elsewhere.
public bool IsCommandPressed { get; }
Property Value
Remarks
Surfaced because it is where the shortcuts live on a Mac: Cmd+V, not Ctrl+V. Without it, anything checking IsCtrlPressed for a shortcut simply does nothing on macOS, with no error to notice. Pair the two -- see IsShortcutModifierPressed -- rather than picking one per platform at the call site.
IsCtrlPressed
Whether Ctrl was held.
public bool IsCtrlPressed { get; }
Property Value
IsRepeat
True when the OS generated this from a key being held down rather than pressed.
public bool IsRepeat { get; }
Property Value
Remarks
Handlers where holding should keep acting -- backspace, arrow keys -- can ignore this and repeat freely. Handlers where it should not -- a button's Enter, a text box committing -- must check it, or holding the key fires them at the OS repeat rate.
IsShiftPressed
Whether Shift was held.
public bool IsShiftPressed { get; }
Property Value
IsShortcutModifierPressed
Whether the "this is a shortcut, not typing" modifier is held: Ctrl or Command.
public bool IsShortcutModifierPressed { get; }
Property Value
Remarks
Accepting both everywhere, rather than the platform's own, on purpose. A Mac keyboard plugged into a Windows machine and a user who learned the other convention both still work, and there is no shortcut here where Ctrl and Cmd mean different things.
KeyCode
The key that went down or up. Key.None for text input.
public Key KeyCode { get; }