KeyEventArgs
Provides information specific to a keyboard event.Properties
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.Use this property for letter-related shortcuts only. For example, when pressing the key at the Z position:
- Returns
Key.Zfor English (QWERTY) layout - Returns
Key.Wfor French (AZERTY) layout - Returns
Key.Yfor German (QWERTZ) layout
The key modifiers (Ctrl, Shift, Alt, Meta) that were active when the event was fired.
The physical key for the associated event. This value is independent of the current keyboard layout and usually corresponds to the key printed on a standard US English QWERTY keyboard.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 in games.
The unicode symbol of the key, or null if none is applicable. For example, when pressing the key at the Z position:
- Returns
"z"for English (QWERTY) layout - Returns
"w"for French (AZERTY) layout - Returns
"y"for German (QWERTZ) layout - Returns
"я"for Russian (JCUKEN) layout
Type of the device that fired the event.
Key Enumeration
Defines the keys available on a keyboard. The enumeration includes:Standard Keys
- Navigation:
Left,Right,Up,Down,Home,End,PageUp,PageDown - Editing:
Back,Delete,Insert,Enter,Return,Tab,Escape,Space - Function Keys:
F1throughF24 - Modifier Keys:
LeftShift,RightShift,LeftCtrl,RightCtrl,LeftAlt,RightAlt
Number and Letter Keys
- Numbers:
D0throughD9(main keyboard) - Letters:
AthroughZ - Numpad:
NumPad0throughNumPad9,Multiply,Add,Subtract,Divide,Decimal
Special Keys
- System:
LWin,RWin,Apps,Sleep - Media:
MediaPlayPause,MediaStop,MediaNextTrack,MediaPreviousTrack,VolumeUp,VolumeDown,VolumeMute - Browser:
BrowserBack,BrowserForward,BrowserRefresh,BrowserStop,BrowserSearch
Platform-Specific Keys
- macOS:
FnLeftArrow,FnRightArrow,FnUpArrow,FnDownArrow - Remote Control:
MediaRed,MediaGreen,MediaYellow,MediaBlue,MediaMenu, etc.
Usage Examples
Handling Keyboard Events
Using Physical Keys for Game Input
Handling Text Input with KeySymbol
Creating Key Gestures
KeyModifiers Enumeration
Defines keyboard modifier keys:None- No modifiersAlt- Alt keyControl- Control key (Cmd on macOS)Shift- Shift keyMeta- Windows/Command key
Best Practices
- For Keyboard Shortcuts: Use the
Keyproperty for letter-based shortcuts - For Position-Based Input: Use
PhysicalKeyfor game controls and position-dependent functionality - For Text Processing: Use
KeySymbolto get the actual character being typed - Avoid Punctuation in Shortcuts: Punctuation keys vary significantly across keyboard layouts
- Mark Events as Handled: Set
e.Handled = trueto prevent event bubbling when you’ve processed the event