KeystrokeHandler (utils)
@ckeditor/ckeditor5-utils/src/keystrokehandler
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();
} );
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 );
}
}
That keystroke handler will listen to keydown
events fired in this view's main element.
Filtering
Properties
-
Listener used to listen to events for easier keystroke handler destruction.
Methods
-
constructor()
module:utils/keystrokehandler~KeystrokeHandler#constructor
Creates an instance of the keystroke handler.
-
destroy() → void
module:utils/keystrokehandler~KeystrokeHandler#destroy
-
listenTo( emitter ) → void
module:utils/keystrokehandler~KeystrokeHandler#listenTo
Starts listening for
keydown
events from a given emitter.Parameters
emitter : Window | Emitter | HTMLElement
Returns
void
-
press( keyEvtData ) → boolean
module:utils/keystrokehandler~KeystrokeHandler#press
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.
-
set( keystroke, callback, options ) → void
module:utils/keystrokehandler~KeystrokeHandler#set
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 : ( KeyboardEvent, () => void ) => void
A function called with the key event data object and a helper function to call both
preventDefault()
andstopPropagation()
on the underlying event.options : KeystrokeHandlerOptions
Additional options.
Defaults to
{}
Returns
void
-
stopListening( [ emitter ] ) → void
module:utils/keystrokehandler~KeystrokeHandler#stopListening
Stops listening to
keydown
events from the given emitter.Parameters
[ emitter ] : Window | Emitter | HTMLElement
Returns
void
Every day, we work hard to keep our documentation complete. Have you spotted outdated information? Is something missing? Please report it via our issue tracker.
With the release of version 42.0.0, we have rewritten much of our documentation to reflect the new import paths and features. We appreciate your feedback to help us ensure its accuracy and completeness.