# KeystrokeHandler

class

Keystroke handler allows registering callbacks for given keystrokes.

The most frequent use of this class is through the [`editor.keystrokes`](module_core_editor_editor-Editor.md#member-keystrokes) property. It allows listening to keystrokes executed in the editing view:

```typescript
editor.keystrokes.set( 'Ctrl+A', ( keyEvtData, cancel ) => {
	console.log( 'Ctrl+A has been pressed' );
	cancel();
} );
```

However, this utility class can be used in various part of the UI. For instance, a certain [`View`](module_ui_view-View.md) can use it like this:

```typescript
class MyView extends View {
	constructor() {
		this.keystrokes = new KeystrokeHandler();

		this.keystrokes.set( 'tab', handleTabKey );
	}

	render() {
		super.render();

		this.keystrokes.listenTo( this.element );
	}
}
```

That keystroke handler will listen to `keydown` events fired in this view's main element.

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

<a id="properties">

## Properties

<a id="member-_listener">

### `_listener: DomEmitter` _(private)_

Listener used to listen to events for easier keystroke handler destruction.

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

<a id="methods">

## Methods

<a id="function-constructor">

### `constructor()`

Creates an instance of the keystroke handler.

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

<a id="function-destroy">

### `destroy() → void`

Destroys the keystroke handler.

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

#### Returns

* `void`

<a id="function-listenTo">

### `listenTo( emitter ) → void`

Starts listening for `keydown` events from a given emitter.

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

#### Parameters

* `emitter: Window | Emitter | HTMLElement`

#### Returns

* `void`

<a id="function-press">

### `press( keyEvtData ) → boolean`

Triggers a keystroke handler for a specified key combination, if such a keystroke was [defined](#function-set).

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

#### Parameters

* `keyEvtData: Readonly<KeystrokeInfo>`

  Key event data.

#### Returns

* `boolean`

  Whether the keystroke was handled.

<a id="function-set">

### `set( keystroke, callback, options ) → void`

Registers a handler for the specified keystroke.

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

#### Parameters

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

  Keystroke defined in a format accepted by the [`parseKeystroke`](module_utils_keyboard.md#function-parseKeystroke) function.

* `callback: ( ev: KeyboardEvent, cancel: () => void ) => void`

  A function called with the [key event data](module_engine_view_observer_keyobserver-ViewDocumentKeyEventData.md) object and a helper function to call both `preventDefault()` and `stopPropagation()` on the underlying event.

* `options: KeystrokeHandlerOptions`

  Additional options.

  Defaults to `{}`

#### Returns

* `void`

<a id="function-stopListening">

### `stopListening( emitter? ) → void`

Stops listening to `keydown` events from the given emitter.

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

#### Parameters

* `emitter?: Window | Emitter | HTMLElement`

#### Returns

* `void`

---

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