Class Key
Provides an abstraction for common keyboard operations and state. Used for processing keyboard input and raising keyboard events.
public class Key : EventArgs, IEquatable<Key>
- Inheritance
-
Key
- Implements
- Inherited Members
Remarks
This class provides a high-level abstraction with helper methods and properties for common keyboard operations. Use this class instead of the KeyCode enumeration for keyboard input whenever possible.
The default value for Key is Null and can be tested using Empty.
Concept | Definition |
---|---|
Testing Shift State |
The Is properties (IsShift,IsCtrl, IsAlt)
test for shift state; whether the key press was modified by a shift key.
|
Adding Shift State |
The With properties (WithShift,WithCtrl,
WithAlt) return a copy of the Key with the shift modifier applied. This is useful for
specifying a key that requires a shift modifier (e.g.
var ControlAltDelete = new Key(Key.Delete).WithAlt.WithDel; .
|
Removing Shift State |
The No properties (NoShift,NoCtrl, NoAlt)
return a copy of the Key with the shift modifier removed. This is useful for specifying a key that
does not require a shift modifier (e.g. var ControlDelete = ControlAltDelete.NoCtrl; .
|
Encoding of A..Z | Lowercase alpha keys are encoded (in KeyCode) as values between 65 and 90 corresponding to the un-shifted A to Z keys on a keyboard. Properties are provided for these (e.g. A, B, etc.). Even though the encoded values are the same as the ASCII values for uppercase characters, these enum values represent *lowercase*, un-shifted characters. |
Persistence as strings |
Keys are persisted as "[Modifiers]+[Key] . For example
new Key(Key.Delete).WithAlt.WithDel is persisted as "Ctrl+Alt+Delete" . See
ToString() and TryParse(string, out Key) for more
information.
|
Constructors
- Key(string)
Constructs a new Key from a string describing the key. See TryParse(string, out Key) for information on the format of the string.
- Key(Key)
Copy constructor.
- Key(KeyCode)
Constructs a new Key from the provided Key value
Properties
- AsRune
The key value as a Rune. This is the actual value of the key pressed, and is independent of the modifiers. Useful for determining if a key represents is a printable character.
- CursorDown
The Key object for Cursor down key.
- CursorLeft
The Key object for Cursor left key.
- CursorRight
The Key object for Cursor right key.
- DeleteChar
The Key object for Delete Character key.
- Handled
Indicates if the current Key event has already been processed and the driver should stop notifying any other event subscriber. It's important to set this value to true specially when updating any View's layout from inside the subscriber method.
- InsertChar
The Key object for Insert Character key.
- IsAlt
Gets a value indicating whether the Alt key was pressed (real or synthesized)
- IsCtrl
Gets a value indicating whether the Ctrl key was pressed.
- IsKeyCodeAtoZ
Gets a value indicating whether the key represents a key in the range of A to Z, regardless of the ShiftMask. This is useful for testing if a key is based on these keys which are special cased.
- IsShift
Gets a value indicating whether the Shift key was pressed.
- IsValid
Indicates whether the Key is valid or not. Invalid keys are Empty, and keys with only shift modifiers.
- KeyCode
The encoded key value.
- NoAlt
Helper for removing a shift modifier from a Key.
var ControlAltDelete = new Key(Key.Delete).WithAlt.WithDel; var AltDelete = ControlAltDelete.NoCtrl;
- NoCtrl
Helper for removing a shift modifier from a Key.
var ControlAltDelete = new Key(Key.Delete).WithAlt.WithDel; var AltDelete = ControlAltDelete.NoCtrl;
- NoShift
Helper for removing a shift modifier from a Key.
var ControlAltDelete = new Key(Key.Delete).WithAlt.WithDel; var AltDelete = ControlAltDelete.NoCtrl;
- PrintScreen
The Key object for Print Screen key.
- Separator
Gets or sets the separator character used when parsing and printing Keys. E.g. Ctrl+A. The default is '+'.
- WithAlt
Helper for specifying a shifted Key.
var ControlAltDelete = new Key(Key.Delete).WithAlt.WithDel;
- WithCtrl
Helper for specifying a shifted Key.
var ControlAltDelete = new Key(Key.Delete).WithAlt.WithDel;
- WithShift
Helper for specifying a shifted Key.
var ControlAltDelete = new Key(Key.Delete).WithAlt.WithDel;
Methods
- Equals(object?)
Determines whether the specified object is equal to the current object.
- GetHashCode()
Serves as the default hash function.
- GetIsKeyCodeAtoZ(KeyCode)
Tests if a KeyCode represents a key in the range of A to Z, regardless of the ShiftMask. This is useful for testing if a key is based on these keys which are special cased.
- ToRune(KeyCode)
Converts a KeyCode to a Rune. Useful for determining if a key represents is a printable character.
- ToString()
Pretty prints the Key.
- ToString(KeyCode)
Formats a KeyCode as a string using the default separator of '+'
- ToString(KeyCode, Rune)
Formats a KeyCode as a string.
- TryParse(string, out Key)
Converts the provided string to a new Key instance.
Operators
- operator ==(Key, Key)
Compares two Keys for equality.
- explicit operator Rune(Key)
Explicitly cast a Key to a Rune. The conversion is lossy because properties such as Handled are not encoded in KeyCode.
- explicit operator uint(Key)
Explicitly cast Key to a uint. The conversion is lossy because properties such as Handled are not encoded in KeyCode.
- explicit operator KeyCode(Key)
Explicitly cast Key to a KeyCode. The conversion is lossy because properties such as Handled are not encoded in KeyCode.
- operator >(Key, Key)
Compares two Keys for greater-than.
- operator >=(Key, Key)
Compares two Keys for greater-than-or-equal-to.
- operator !=(Key, Key)
Compares two Keys for not equality.
- operator <(Key, Key)
Compares two Keys for less-than.
- operator <=(Key, Key)
Compares two Keys for greater-than-or-equal-to.