# utils/keyboard

module

<a id="constants">

## Constants

<a id="constant-keyCodes">

### `keyCodes: Record<string, number>`

An object with `keyName => keyCode` pairs for a set of known keys.

Contains:

* `a-z`,
* `0-9`,
* `f1-f12`,
* `` ` ``, `-`, `=`, `[`, `]`, `;`, `'`, `,`, `.`, `/`, `\`,
* `arrow(left|up|right|bottom)`,
* `backspace`, `delete`, `end`, `enter`, `esc`, `home`, `tab`,
* `ctrl`, `cmd`, `shift`, `alt`.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/keyboard.ts#L52)

<a id="interfaces">

## Interfaces

<a id="interface-KeystrokeInfo">

### `KeystrokeInfo`

<a id="type-definitions">

## Type Definitions

<a id="typedef-ArrowKeyCodeDirection">

### `ArrowKeyCodeDirection`

<a id="functions">

## Functions

<a id="function-getCode">

### `getCode( key ) → number`

Converts a key name or [keystroke info](module_utils_keyboard-KeystrokeInfo.md) into a key code.

Note: Key names are matched with [`keyCodes`](#constant-keyCodes) in a case-insensitive way.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/keyboard.ts#L76)

#### Parameters

* `key: string | Readonly<KeystrokeInfo>`

  A key name (see [`keyCodes`](#constant-keyCodes)) or a keystroke data object.

#### Returns

* `number`

  Key or keystroke code.

<a id="function-getEnvKeystrokeText">

### `getEnvKeystrokeText( keystroke, forcedEnv? ) → string`

Translates any keystroke string text like `"Ctrl+A"` to an environment–specific keystroke, i.e. `"⌘A"` on macOS.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/keyboard.ts#L142)

#### Parameters

* `keystroke: string`

  The keystroke text.

* `forcedEnv?: 'PC' | 'Mac'`

  The environment to force the key translation to. If not provided, the current environment is used.

#### Returns

* `string`

  The keystroke text specific for the environment.

<a id="function-getLocalizedArrowKeyCodeDirection">

### `getLocalizedArrowKeyCodeDirection( keyCode, contentLanguageDirection ) → ArrowKeyCodeDirection | undefined`

Returns the direction in which the [selection](module_engine_model_documentselection-ModelDocumentSelection.md) will move when the provided arrow key code is pressed considering the language direction of the editor content.

For instance, in right–to–left (RTL) content languages, pressing the left arrow means moving the selection right (forward) in the model structure. Similarly, pressing the right arrow moves the selection left (backward).

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/keyboard.ts#L191)

#### Parameters

* `keyCode: number`

  A key code as in [`keyCode`](module_utils_keyboard-KeystrokeInfo.md#member-keyCode).

* `contentLanguageDirection: LanguageDirection`

  The content language direction, corresponding to [`contentLanguageDirection`](module_utils_locale-Locale.md#member-contentLanguageDirection).

#### Returns

* `ArrowKeyCodeDirection | undefined`

  Localized arrow direction or `undefined` for non-arrow key codes.

<a id="function-isArrowKeyCode">

### `isArrowKeyCode( keyCode ) → boolean`

Returns `true` if the provided key code represents one of the arrow keys.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/keyboard.ts#L167)

#### Parameters

* `keyCode: number`

  A key code as in [`keyCode`](module_utils_keyboard-KeystrokeInfo.md#member-keyCode).

#### Returns

* `boolean`

<a id="function-isForwardArrowKeyCode">

### `isForwardArrowKeyCode( keyCode, contentLanguageDirection ) → boolean`

Determines if the provided key code moves the [selection](module_engine_model_documentselection-ModelDocumentSelection.md) forward or backward considering the language direction of the editor content.

For instance, in right–to–left (RTL) languages, pressing the left arrow means moving forward in the model structure. Similarly, pressing the right arrow moves the selection backward.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/keyboard.ts#L242)

#### Parameters

* `keyCode: number`

  A key code as in [`keyCode`](module_utils_keyboard-KeystrokeInfo.md#member-keyCode).

* `contentLanguageDirection: LanguageDirection`

  The content language direction, corresponding to [`contentLanguageDirection`](module_utils_locale-Locale.md#member-contentLanguageDirection).

#### Returns

* `boolean`

<a id="function-parseKeystroke">

### `parseKeystroke( keystroke ) → number`

Parses the keystroke and returns a keystroke code that will match the code returned by [`getCode`](#function-getCode) for the corresponding [keystroke info](module_utils_keyboard-KeystrokeInfo.md).

The keystroke can be passed in two formats:

* as a single string – e.g. `ctrl + A`,

* as an array of [known key names](#constant-keyCodes) and key codes – e.g.:

  * `[ 'ctrl', 32 ]` (ctrl + space),
  * `[ 'ctrl', 'a' ]` (ctrl + A).

Note: Key names are matched with [`keyCodes`](#constant-keyCodes) in a case-insensitive way.

Note: Only keystrokes with a single non-modifier key are supported (e.g. `ctrl+A` is OK, but `ctrl+A+B` is not).

Note: On macOS, keystroke handling is translating the `Ctrl` key to the `Cmd` key and handling only that keystroke. For example, a registered keystroke `Ctrl+A` will be translated to `Cmd+A` on macOS. To disable the translation of some keystroke, use the forced modifier: `Ctrl!+A` (note the exclamation mark).

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/keyboard.ts#L124)

#### Parameters

* `keystroke: string | readonly Array<string | number>`

  The keystroke definition.

#### Returns

* `number`

  Keystroke code.

---

Full index of the CKEditor 5 API reference: [llms.txt](llms.txt)
