Table of Contents

Class Key

Namespace
Terminal.Gui
Assembly
Terminal.Gui.dll

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.

ConceptDefinition
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()

Constructs a new Key

Key(char)

Constructs a new Key from a char.

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

A

The Key object for the A key (un-shifted). Use Key.A.WithShift for uppercase 'A'.

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.

B

The Key object for the B key (un-shifted). Use Key.B.WithShift for uppercase 'B'.

Backspace

The Key object for the Backspace key.

C

The Key object for the C key (un-shifted). Use Key.C.WithShift for uppercase 'C'.

Clear

The Key object for the clear key.

CursorDown

The Key object for Cursor down key.

CursorLeft

The Key object for Cursor left key.

CursorRight

The Key object for Cursor right key.

CursorUp

The Key object for the Cursor up key.

D

The Key object for the D key (un-shifted). Use Key.D.WithShift for uppercase 'D'.

D0

The Key object for 0 key.

D1

The Key object for 1 key.

D2

The Key object for 2 key.

D3

The Key object for 3 key.

D4

The Key object for 4 key.

D5

The Key object for 5 key.

D6

The Key object for 6 key.

D7

The Key object for 7 key.

D8

The Key object for 8 key.

D9

The Key object for 9 key.

Delete

The Key object for the Delete key.

DeleteChar

The Key object for Delete Character key.

E

The Key object for the E key (un-shifted). Use Key.E.WithShift for uppercase 'E'.

Empty

An uninitialized The Key object.

End

The Key object for End key.

Enter

The Key object for the return key.

Esc

The Key object for the Escape key.

F

The Key object for the F key (un-shifted). Use Key.F.WithShift for uppercase 'F'.

F1

The Key object for F1 key.

F10

The Key object for F10 key.

F11

The Key object for F11 key.

F12

The Key object for F12 key.

F13

The Key object for F13 key.

F14

The Key object for F14 key.

F15

The Key object for F15 key.

F16

The Key object for F16 key.

F17

The Key object for F17 key.

F18

The Key object for F18 key.

F19

The Key object for F19 key.

F2

The Key object for F2 key.

F20

The Key object for F20 key.

F21

The Key object for F21 key.

F22

The Key object for F22 key.

F23

The Key object for F23 key.

F24

The Key object for F24 key.

F3

The Key object for F3 key.

F4

The Key object for F4 key.

F5

The Key object for F5 key.

F6

The Key object for F6 key.

F7

The Key object for F7 key.

F8

The Key object for F8 key.

F9

The Key object for F9 key.

G

The Key object for the G key (un-shifted). Use Key.G.WithShift for uppercase 'G'.

H

The Key object for the H key (un-shifted). Use Key.H.WithShift for uppercase 'H'.

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.

Home

The Key object for Home key.

I

The Key object for the I key (un-shifted). Use Key.I.WithShift for uppercase 'I'.

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.

J

The Key object for the J key (un-shifted). Use Key.J.WithShift for uppercase 'J'.

K

The Key object for the K key (un-shifted). Use Key.K.WithShift for uppercase 'K'.

KeyCode

The encoded key value.

L

The Key object for the L key (un-shifted). Use Key.L.WithShift for uppercase 'L'.

M

The Key object for the M key (un-shifted). Use Key.M.WithShift for uppercase 'M'.

N

The Key object for the N key (un-shifted). Use Key.N.WithShift for uppercase 'N'.

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;
O

The Key object for the O key (un-shifted). Use Key.O.WithShift for uppercase 'O'.

P

The Key object for the P key (un-shifted). Use Key.P.WithShift for uppercase 'P'.

PageDown

The Key object for Page Down key.

PageUp

The Key object for Page Up key.

PrintScreen

The Key object for Print Screen key.

Q

The Key object for the Q key (un-shifted). Use Key.Q.WithShift for uppercase 'Q'.

R

The Key object for the R key (un-shifted). Use Key.R.WithShift for uppercase 'R'.

S

The Key object for the S key (un-shifted). Use Key.S.WithShift for uppercase 'S'.

Separator

Gets or sets the separator character used when parsing and printing Keys. E.g. Ctrl+A. The default is '+'.

Space

The Key object for the Space bar key.

T

The Key object for the T key (un-shifted). Use Key.T.WithShift for uppercase 'T'.

Tab

The Key object for the tab key (forwards tab key).

U

The Key object for the U key (un-shifted). Use Key.U.WithShift for uppercase 'U'.

V

The Key object for the V key (un-shifted). Use Key.V.WithShift for uppercase 'V'.

W

The Key object for the W key (un-shifted). Use Key.W.WithShift for uppercase 'W'.

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;
X

The Key object for the X key (un-shifted). Use Key.X.WithShift for uppercase 'X'.

Y

The Key object for the Y key (un-shifted). Use Key.Y.WithShift for uppercase 'Y'.

Z

The Key object for the Z key (un-shifted). Use Key.Z.WithShift for uppercase 'Z'.

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.

implicit operator Key(char)

Cast char to a Key.

implicit operator Key(string)

Cast string to a Key.

implicit operator string(Key)

Cast a Key to a string.

implicit operator Key(KeyCode)

Cast KeyCode to a Key.

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.