Skip to content

KeyCode abstract ​

abstract class KeyCode

Defines the keycode values for keys that are returned by KeyboardEvent.keyCode.

Important note: There is substantial divergence in how different browsers handle keycodes and their variants in different locales/keyboard layouts. We provide these constants to help make code processing keys more readable.

Constructors ​

KeyCode() ​

KeyCode()

Properties ​

hashCode no setter inherited ​

int get hashCode

The hash code for this object.

A hash code is a single integer which represents the state of the object that affects operator == comparisons.

All objects have hash codes. The default hash code implemented by Object represents only the identity of the object, the same way as the default operator == implementation only considers objects equal if they are identical (see identityHashCode).

If operator == is overridden to use the object state instead, the hash code must also be changed to represent that state, otherwise the object cannot be used in hash based data structures like the default Set and Map implementations.

Hash codes must be the same for objects that are equal to each other according to operator ==. The hash code of an object should only change if the object changes in a way that affects equality. There are no further requirements for the hash codes. They need not be consistent between executions of the same program and there are no distribution guarantees.

Objects that are not equal are allowed to have the same hash code. It is even technically allowed that all instances have the same hash code, but if clashes happen too often, it may reduce the efficiency of hash-based data structures like HashSet or HashMap.

If a subclass overrides hashCode, it should override the operator == operator as well to maintain consistency.

Inherited from Object.

Implementation
dart
external int get hashCode;

runtimeType no setter inherited ​

Type get runtimeType

A representation of the runtime type of the object.

Inherited from Object.

Implementation
dart
external Type get runtimeType;

Methods ​

noSuchMethod() inherited ​

dynamic noSuchMethod(Invocation invocation)

Invoked when a nonexistent method or property is accessed.

A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:

dart
dynamic object = 1;
object.add(42); // Statically allowed, run-time error

This invalid code will invoke the noSuchMethod method of the integer 1 with an Invocation representing the .add(42) call and arguments (which then throws).

Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.

A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:

dart
class MockList<T> implements List<T> {
  noSuchMethod(Invocation invocation) {
    log(invocation);
    super.noSuchMethod(invocation); // Will throw.
  }
}
void main() {
  MockList().add(42);
}

This code has no compile-time warnings or errors even though the MockList class has no concrete implementation of any of the List interface methods. Calls to List methods are forwarded to noSuchMethod, so this code will log an invocation similar to Invocation.method(#add, [42]) and then throw.

If a value is returned from noSuchMethod, it becomes the result of the original invocation. If the value is not of a type that can be returned by the original invocation, a type error occurs at the invocation.

The default behavior is to throw a NoSuchMethodError.

Inherited from Object.

Implementation
dart
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);

toString() inherited ​

String toString()

A string representation of this object.

Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.

Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.

Inherited from Object.

Implementation
dart
external String toString();

Operators ​

operator ==() inherited ​

bool operator ==(Object other)

The equality operator.

The default behavior for all Objects is to return true if and only if this object and other are the same object.

Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:

  • Total: It must return a boolean for all arguments. It should never throw.

  • Reflexive: For all objects o, o == o must be true.

  • Symmetric: For all objects o1 and o2, o1 == o2 and o2 == o1 must either both be true, or both be false.

  • Transitive: For all objects o1, o2, and o3, if o1 == o2 and o2 == o3 are true, then o1 == o3 must be true.

The method should also be consistent over time, so whether two objects are equal should only change if at least one of the objects was modified.

If a subclass overrides the equality operator, it should override the hashCode method as well to maintain consistency.

Inherited from Object.

Implementation
dart
external bool operator ==(Object other);

Static Methods ​

isCharacterKey() ​

bool isCharacterKey(int keyCode)

Returns true if the keyCode produces a (US keyboard) character. Note: This does not (yet) cover characters on non-US keyboards (Russian, Hebrew, etc.).

Implementation
dart
static bool isCharacterKey(int keyCode) {
  if ((keyCode >= ZERO && keyCode <= NINE) ||
      (keyCode >= NUM_ZERO && keyCode <= NUM_MULTIPLY) ||
      (keyCode >= A && keyCode <= Z)) {
    return true;
  }

  &#47;&#47; Safari sends zero key code for non-latin characters.
  if (Device.isWebKit && keyCode == 0) {
    return true;
  }

  return (keyCode == SPACE ||
      keyCode == QUESTION_MARK ||
      keyCode == NUM_PLUS ||
      keyCode == NUM_MINUS ||
      keyCode == NUM_PERIOD ||
      keyCode == NUM_DIVISION ||
      keyCode == SEMICOLON ||
      keyCode == FF_SEMICOLON ||
      keyCode == DASH ||
      keyCode == EQUALS ||
      keyCode == FF_EQUALS ||
      keyCode == COMMA ||
      keyCode == PERIOD ||
      keyCode == SLASH ||
      keyCode == APOSTROPHE ||
      keyCode == SINGLE_QUOTE ||
      keyCode == OPEN_SQUARE_BRACKET ||
      keyCode == BACKSLASH ||
      keyCode == CLOSE_SQUARE_BRACKET);
}

Constants ​

A ​

const int A
Implementation
dart
static const int A = 65;

ALT ​

const int ALT
Implementation
dart
static const int ALT = 18;

APOSTROPHE ​

const int APOSTROPHE

CAUTION: This constant requires localization for other locales and keyboard layouts.

Implementation
dart
static const int APOSTROPHE = 192;

B ​

const int B
Implementation
dart
static const int B = 66;

BACKSLASH ​

const int BACKSLASH

CAUTION: This constant requires localization for other locales and keyboard layouts.

Implementation
dart
static const int BACKSLASH = 220;

BACKSPACE ​

const int BACKSPACE
Implementation
dart
static const int BACKSPACE = 8;

C ​

const int C
Implementation
dart
static const int C = 67;

CAPS_LOCK ​

const int CAPS_LOCK
Implementation
dart
static const int CAPS_LOCK = 20;

CLOSE_SQUARE_BRACKET ​

const int CLOSE_SQUARE_BRACKET

CAUTION: This constant requires localization for other locales and keyboard layouts.

Implementation
dart
static const int CLOSE_SQUARE_BRACKET = 221;

COMMA ​

const int COMMA

CAUTION: This constant requires localization for other locales and keyboard layouts.

Implementation
dart
static const int COMMA = 188;

CONTEXT_MENU ​

const int CONTEXT_MENU
Implementation
dart
static const int CONTEXT_MENU = 93;

CTRL ​

const int CTRL
Implementation
dart
static const int CTRL = 17;

D ​

const int D
Implementation
dart
static const int D = 68;

DASH ​

const int DASH

CAUTION: This constant requires localization for other locales and keyboard layouts.

Implementation
dart
static const int DASH = 189;

DELETE ​

const int DELETE
Implementation
dart
static const int DELETE = 46;

DOWN ​

const int DOWN
Implementation
dart
static const int DOWN = 40;

E ​

const int E
Implementation
dart
static const int E = 69;

EIGHT ​

const int EIGHT
Implementation
dart
static const int EIGHT = 56;

END ​

const int END
Implementation
dart
static const int END = 35;

ENTER ​

const int ENTER
Implementation
dart
static const int ENTER = 13;

EQUALS ​

const int EQUALS

CAUTION: This constant requires localization for other locales and keyboard layouts.

Implementation
dart
static const int EQUALS = 187;

ESC ​

const int ESC
Implementation
dart
static const int ESC = 27;

F ​

const int F
Implementation
dart
static const int F = 70;

F1 ​

const int F1
Implementation
dart
static const int F1 = 112;

F2 ​

const int F2
Implementation
dart
static const int F2 = 113;

F3 ​

const int F3
Implementation
dart
static const int F3 = 114;

F4 ​

const int F4
Implementation
dart
static const int F4 = 115;

F5 ​

const int F5
Implementation
dart
static const int F5 = 116;

F6 ​

const int F6
Implementation
dart
static const int F6 = 117;

F7 ​

const int F7
Implementation
dart
static const int F7 = 118;

F8 ​

const int F8
Implementation
dart
static const int F8 = 119;

F9 ​

const int F9
Implementation
dart
static const int F9 = 120;

F10 ​

const int F10
Implementation
dart
static const int F10 = 121;

F11 ​

const int F11
Implementation
dart
static const int F11 = 122;

F12 ​

const int F12
Implementation
dart
static const int F12 = 123;

FF_EQUALS ​

const int FF_EQUALS
Implementation
dart
static const int FF_EQUALS = 61;

FF_SEMICOLON ​

const int FF_SEMICOLON
Implementation
dart
static const int FF_SEMICOLON = 59;

FIRST_MEDIA_KEY ​

const int FIRST_MEDIA_KEY
Implementation
dart
static const int FIRST_MEDIA_KEY = 166;

FIVE ​

const int FIVE
Implementation
dart
static const int FIVE = 53;

FOUR ​

const int FOUR
Implementation
dart
static const int FOUR = 52;

G ​

const int G
Implementation
dart
static const int G = 71;

H ​

const int H
Implementation
dart
static const int H = 72;

HOME ​

const int HOME
Implementation
dart
static const int HOME = 36;

I ​

const int I
Implementation
dart
static const int I = 73;

INSERT ​

const int INSERT
Implementation
dart
static const int INSERT = 45;

J ​

const int J
Implementation
dart
static const int J = 74;

K ​

const int K
Implementation
dart
static const int K = 75;

L ​

const int L
Implementation
dart
static const int L = 76;

LAST_MEDIA_KEY ​

const int LAST_MEDIA_KEY
Implementation
dart
static const int LAST_MEDIA_KEY = 183;

LEFT ​

const int LEFT
Implementation
dart
static const int LEFT = 37;

M ​

const int M
Implementation
dart
static const int M = 77;

MAC_ENTER ​

const int MAC_ENTER
Implementation
dart
static const int MAC_ENTER = 3;

MAC_FF_META ​

const int MAC_FF_META
Implementation
dart
static const int MAC_FF_META = 224;

META ​

const int META
Implementation
dart
static const int META = 91;

N ​

const int N
Implementation
dart
static const int N = 78;

NINE ​

const int NINE
Implementation
dart
static const int NINE = 57;

NUM_CENTER ​

const int NUM_CENTER

NUM_CENTER is also NUMLOCK for FF and Safari on Mac.

Implementation
dart
static const int NUM_CENTER = 12;

NUM_DELETE ​

const int NUM_DELETE
Implementation
dart
static const int NUM_DELETE = 46;

NUM_DIVISION ​

const int NUM_DIVISION
Implementation
dart
static const int NUM_DIVISION = 111;

NUM_EAST ​

const int NUM_EAST
Implementation
dart
static const int NUM_EAST = 39;

NUM_EIGHT ​

const int NUM_EIGHT
Implementation
dart
static const int NUM_EIGHT = 104;

NUM_FIVE ​

const int NUM_FIVE
Implementation
dart
static const int NUM_FIVE = 101;

NUM_FOUR ​

const int NUM_FOUR
Implementation
dart
static const int NUM_FOUR = 100;

NUM_INSERT ​

const int NUM_INSERT
Implementation
dart
static const int NUM_INSERT = 45;

NUM_MINUS ​

const int NUM_MINUS
Implementation
dart
static const int NUM_MINUS = 109;

NUM_MULTIPLY ​

const int NUM_MULTIPLY
Implementation
dart
static const int NUM_MULTIPLY = 106;

NUM_NINE ​

const int NUM_NINE
Implementation
dart
static const int NUM_NINE = 105;

NUM_NORTH ​

const int NUM_NORTH
Implementation
dart
static const int NUM_NORTH = 38;

NUM_NORTH_EAST ​

const int NUM_NORTH_EAST
Implementation
dart
static const int NUM_NORTH_EAST = 33;

NUM_NORTH_WEST ​

const int NUM_NORTH_WEST
Implementation
dart
static const int NUM_NORTH_WEST = 36;

NUM_ONE ​

const int NUM_ONE
Implementation
dart
static const int NUM_ONE = 97;

NUM_PERIOD ​

const int NUM_PERIOD
Implementation
dart
static const int NUM_PERIOD = 110;

NUM_PLUS ​

const int NUM_PLUS
Implementation
dart
static const int NUM_PLUS = 107;

NUM_SEVEN ​

const int NUM_SEVEN
Implementation
dart
static const int NUM_SEVEN = 103;

NUM_SIX ​

const int NUM_SIX
Implementation
dart
static const int NUM_SIX = 102;

NUM_SOUTH ​

const int NUM_SOUTH
Implementation
dart
static const int NUM_SOUTH = 40;

NUM_SOUTH_EAST ​

const int NUM_SOUTH_EAST
Implementation
dart
static const int NUM_SOUTH_EAST = 34;

NUM_SOUTH_WEST ​

const int NUM_SOUTH_WEST
Implementation
dart
static const int NUM_SOUTH_WEST = 35;

NUM_THREE ​

const int NUM_THREE
Implementation
dart
static const int NUM_THREE = 99;

NUM_TWO ​

const int NUM_TWO
Implementation
dart
static const int NUM_TWO = 98;

NUM_WEST ​

const int NUM_WEST
Implementation
dart
static const int NUM_WEST = 37;

NUM_ZERO ​

const int NUM_ZERO
Implementation
dart
static const int NUM_ZERO = 96;

NUMLOCK ​

const int NUMLOCK
Implementation
dart
static const int NUMLOCK = 144;

O ​

const int O
Implementation
dart
static const int O = 79;

ONE ​

const int ONE
Implementation
dart
static const int ONE = 49;

OPEN_SQUARE_BRACKET ​

const int OPEN_SQUARE_BRACKET

CAUTION: This constant requires localization for other locales and keyboard layouts.

Implementation
dart
static const int OPEN_SQUARE_BRACKET = 219;

P ​

const int P
Implementation
dart
static const int P = 80;

PAGE_DOWN ​

const int PAGE_DOWN
Implementation
dart
static const int PAGE_DOWN = 34;

PAGE_UP ​

const int PAGE_UP
Implementation
dart
static const int PAGE_UP = 33;

PAUSE ​

const int PAUSE
Implementation
dart
static const int PAUSE = 19;

PERIOD ​

const int PERIOD

CAUTION: This constant requires localization for other locales and keyboard layouts.

Implementation
dart
static const int PERIOD = 190;

PRINT_SCREEN ​

const int PRINT_SCREEN
Implementation
dart
static const int PRINT_SCREEN = 44;

Q ​

const int Q
Implementation
dart
static const int Q = 81;

QUESTION_MARK ​

const int QUESTION_MARK

CAUTION: The question mark is for US-keyboard layouts. It varies for other locales and keyboard layouts.

Implementation
dart
static const int QUESTION_MARK = 63;

R ​

const int R
Implementation
dart
static const int R = 82;

RIGHT ​

const int RIGHT
Implementation
dart
static const int RIGHT = 39;

S ​

const int S
Implementation
dart
static const int S = 83;

SCROLL_LOCK ​

const int SCROLL_LOCK
Implementation
dart
static const int SCROLL_LOCK = 145;

SEMICOLON ​

const int SEMICOLON

CAUTION: This constant requires localization for other locales and keyboard layouts.

Implementation
dart
static const int SEMICOLON = 186;

SEVEN ​

const int SEVEN
Implementation
dart
static const int SEVEN = 55;

SHIFT ​

const int SHIFT
Implementation
dart
static const int SHIFT = 16;

SINGLE_QUOTE ​

const int SINGLE_QUOTE

CAUTION: This constant requires localization for other locales and keyboard layouts.

Implementation
dart
static const int SINGLE_QUOTE = 222;

SIX ​

const int SIX
Implementation
dart
static const int SIX = 54;

SLASH ​

const int SLASH

CAUTION: This constant requires localization for other locales and keyboard layouts.

Implementation
dart
static const int SLASH = 191;

SPACE ​

const int SPACE
Implementation
dart
static const int SPACE = 32;

T ​

const int T
Implementation
dart
static const int T = 84;

TAB ​

const int TAB
Implementation
dart
static const int TAB = 9;

THREE ​

const int THREE
Implementation
dart
static const int THREE = 51;

TILDE ​

const int TILDE

CAUTION: This constant requires localization for other locales and keyboard layouts.

Implementation
dart
static const int TILDE = 192;

TWO ​

const int TWO
Implementation
dart
static const int TWO = 50;

U ​

const int U
Implementation
dart
static const int U = 85;

UNKNOWN ​

const int UNKNOWN

A sentinel value if the keycode could not be determined.

Implementation
dart
static const int UNKNOWN = -1;

UP ​

const int UP
Implementation
dart
static const int UP = 38;

V ​

const int V
Implementation
dart
static const int V = 86;

W ​

const int W
Implementation
dart
static const int W = 87;

WIN_IME ​

const int WIN_IME
Implementation
dart
static const int WIN_IME = 229;

WIN_KEY ​

const int WIN_KEY
Implementation
dart
static const int WIN_KEY = 224;

WIN_KEY_FF_LINUX ​

const int WIN_KEY_FF_LINUX
Implementation
dart
static const int WIN_KEY_FF_LINUX = 0;

WIN_KEY_LEFT ​

const int WIN_KEY_LEFT
Implementation
dart
static const int WIN_KEY_LEFT = 91;

WIN_KEY_RIGHT ​

const int WIN_KEY_RIGHT
Implementation
dart
static const int WIN_KEY_RIGHT = 92;

X ​

const int X
Implementation
dart
static const int X = 88;

Y ​

const int Y
Implementation
dart
static const int Y = 89;

Z ​

const int Z
Implementation
dart
static const int Z = 90;

ZERO ​

const int ZERO
Implementation
dart
static const int ZERO = 48;