Sign up (with export icon)

KeystrokeHandler

Api-class iconclass

Keystroke handler allows registering callbacks for given keystrokes.

The most frequent use of this class is through the editor.keystrokes property. It allows listening to keystrokes executed in the editing view:

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

However, this utility class can be used in various part of the UI. For instance, a certain View can use it like this:

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

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

	render() {
		super.render();

		this.keystrokes.listenTo( this.element );
	}
}
Copy code

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

Properties

Methods

  • Creates an instance of the keystroke handler.

  • Chevron-right icon

    destroy() → void

    Destroys the keystroke handler.

    Returns

    void
  • Chevron-right icon

    listenTo( emitter ) → void

    Starts listening for keydown events from a given emitter.

    Parameters

    emitter : Window | Emitter | HTMLElement

    Returns

    void
  • Chevron-right icon

    press( keyEvtData ) → boolean

    Triggers a keystroke handler for a specified key combination, if such a keystroke was defined.

    Parameters

    keyEvtData : Readonly<KeystrokeInfo>

    Key event data.

    Returns

    boolean

    Whether the keystroke was handled.

  • Chevron-right icon

    set( keystroke, callback, options ) → void

    Registers a handler for the specified keystroke.

    Parameters

    keystroke : string | readonly Array<string | number>

    Keystroke defined in a format accepted by the parseKeystroke function.

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

    A function called with the key event data object and a helper function to call both preventDefault() and stopPropagation() on the underlying event.

    options : KeystrokeHandlerOptions

    Additional options.

    Defaults to {}

    Returns

    void
  • Chevron-right icon

    stopListening( [ emitter ] ) → void

    Stops listening to keydown events from the given emitter.

    Parameters

    [ emitter ] : Window | Emitter | HTMLElement

    Returns

    void