Skip to main content

KeyEventArgs Class

Definition​

Namespace:Avalonia.Input
Assembly:Avalonia.Base
Package:Avalonia

Provides information specific to a keyboard event.

public class KeyEventArgs

Inheritance: EventArgs -> RoutedEventArgs -> KeyEventArgs

Constructors​

NameDescription
KeyEventArgsNo summary available.

KeyEventArgs Constructor​

public KeyEventArgs()

Properties​

NameDescription
AlternateKeyTextGets an alternate text of the key, for chords where Avalonia.Input.KeyEventArgs.KeyText is not the key's character: the text with Ctrl, Alt and Meta ignored and the other modifiers applied, or null if the key has no text. Like NSEvent.charactersIgnoringModifiers on macOS, the value keeps Shift.
ConsumedModifiersGets the pressed modifiers that the keyboard layout used to produce the key's text, or null if the platform doesn't report them.
HandledGets or sets a value indicating whether the routed event has already been handled. Inherited from RoutedEventArgs.
KeyGets the virtual-key for the associated event. A given physical key can result in different virtual keys depending on the current keyboard layout. This is the key that is generally referred to when creating keyboard shortcuts.
KeyDeviceTypeType of the device that fire the event
KeyModifiersNo summary available.
KeySymbolGets the unicode symbol of the key, or null if none is applicable.
KeyTextGets the text that the platform attached to this key event, with all active modifiers applied, when it is available. It includes C0 control characters and DEL.
PhysicalKeyGets the physical key for the associated event.
PlatformKeyInfoGets platform-specific data of the key event, or null if the platform doesn't provide any.
RouteGets or sets the routing strategy (direct, bubbling, or tunneling) of the routed event. Inherited from RoutedEventArgs.
RoutedEventGets or sets the routed event associated with these event args. Inherited from RoutedEventArgs.
SourceGets or sets the source object that raised the routed event. Inherited from RoutedEventArgs.

AlternateKeyText Property​

Gets an alternate text of the key, for chords where Avalonia.Input.KeyEventArgs.KeyText is not the key's character: the text with Ctrl, Alt and Meta ignored and the other modifiers applied, or null if the key has no text. Like NSEvent.charactersIgnoringModifiers on macOS, the value keeps Shift.

For example, Ctrl+Z produces "z", Ctrl+Shift+2 on a US layout produces "@", Ctrl+Я on a Russian layout produces "я", AltGr+Q on a German layout produces "@" on Windows and Linux, and Option+B on macOS produces "b".

public string AlternateKeyText { get; set; }

Remarks​

The rule in detail:

  • Shift, Caps Lock and AltGr (ISO Level 3) are applied.
  • Ctrl, Alt and Meta are ignored, except where they select the key's level. On Windows, AltGr is reported as Ctrl+Alt, and a Ctrl+Alt chord that produces printable text gives that text. A layout can also use Ctrl or Alt to select a level, and then the text is the text of that level.
  • On macOS, Option counts as Alt and is always ignored, so a terminal can use it as a Meta key. This is the same as NSEvent.charactersIgnoringModifiers: Option+Q on a German layout gives "q". Use Avalonia.Input.KeyEventArgs.KeyText for the character that Option types ("«").

A terminal emulator can use it to send ESC and the text for Alt chords, or a C0 control character for Ctrl chords that Avalonia.Input.KeyEventArgs.KeyText doesn't cover. Avalonia.Input.KeyEventArgs.ConsumedModifiers tells which of the pressed modifiers the layout used.

It is only set for Avalonia.Input.InputElement.KeyDownEvent.

See also​

ConsumedModifiers Property​

Gets the pressed modifiers that the keyboard layout used to produce the key's text, or null if the platform doesn't report them.

For example, Shift is consumed for Shift+2 ("@") on a US layout, and Ctrl and Alt are consumed for AltGr+Q ("@") on a German layout on Windows. Ctrl is not consumed for Ctrl+Z.

public Nullable<Avalonia.Input.KeyModifiers> ConsumedModifiers { get; set; }

Remarks​

KeyModifiers & ~ConsumedModifiers gives the modifiers that are left for the application, for example to decide whether a key types text or is a shortcut.

On X11 and Wayland, the value follows the rules of GTK (xkbcommon's XKB_CONSUMED_MODE_GTK): a modifier is consumed only when it changes the key's keysym. On macOS, Option is consumed when it changes the character, and Ctrl and Command are never consumed. On Windows, a modifier is consumed when releasing it changes the key's character in the layout. It is only set for Avalonia.Input.InputElement.KeyDownEvent.

Key Property​

Gets the virtual-key for the associated event. A given physical key can result in different virtual keys depending on the current keyboard layout. This is the key that is generally referred to when creating keyboard shortcuts.

For example, when pressing the key located at the Z position on standard US English QWERTY keyboard, this property returns:

public Avalonia.Input.Key Key { get; set; }

Remarks​

This property should be used for letter-related shortcuts only. Prefer using Avalonia.Input.KeyEventArgs.PhysicalKey if you need to refer to a key given its position on the keyboard (a common usage is moving the player with WASD-like keys in games), or Avalonia.Input.KeyEventArgs.KeySymbol if you want to know which character the key will output. Avoid using this for shortcuts related to punctuation keys, as they differ wildly depending on keyboard layouts.

See also​

KeyDeviceType Property​

Type of the device that fire the event

public Avalonia.Input.KeyDeviceType KeyDeviceType { get; set; }

KeyModifiers Property​

public Avalonia.Input.KeyModifiers KeyModifiers { get; set; }

KeySymbol Property​

Gets the unicode symbol of the key, or null if none is applicable.

For example, when pressing the key located at the Z position on standard US English QWERTY keyboard, this property returns:

  • z for an English (QWERTY) layout
  • w for a French (AZERTY) layout
  • y for a German (QWERTZ) layout
  • я for a Russian (JCUKEN) layout
public string KeySymbol { get; set; }

KeyText Property​

Gets the text that the platform attached to this key event, with all active modifiers applied, when it is available. It includes C0 control characters and DEL.

For example, Ctrl+Z produces "\x1a" on most layouts. On a German layout, AltGr+Q produces "@" on Windows and Linux, and Option+L produces "@" on macOS.

public string KeyText { get; set; }

Remarks​

This property is intended for encoding keys, for example in terminal emulators. It is not a replacement for Avalonia.Input.InputElement.TextInputEvent:

  • Null doesn't mean that no text input follows. Some platforms don't provide the value, and keys that an input method handles have none.
  • It is not a preview of, or a guarantee for, the committed text.
  • It can contain several characters, surrogate pairs and NUL characters.
  • The result of a dead key or compose sequence depends on the platform: some platforms report the composed text, others report the text before the input method handles the event.

The value comes from the WM_CHAR messages of the key on Windows, XLookupString on X11, xkb_state_key_get_utf8 on Wayland and NSEvent.characters on macOS. It matches the text of Qt's QKeyEvent. On macOS, Option types characters, so Option+Q on a German layout gives "«".

It is only set for Avalonia.Input.InputElement.KeyDownEvent.

See also​

PhysicalKey Property​

Gets the physical key for the associated event.

This value is independent of the current keyboard layout and usually correspond to the key printed on a standard US English QWERTY keyboard.

public Avalonia.Input.PhysicalKey PhysicalKey { get; set; }

Remarks​

Use this property if you need to refer to a key given its position on the keyboard (a common usage is moving the player with WASD-like keys).

See also​

PlatformKeyInfo Property​

Gets platform-specific data of the key event, or null if the platform doesn't provide any.

public Nullable<Avalonia.Input.PlatformKeyInfo> PlatformKeyInfo { get; set; }