utils/keyboard
Constants
keyCodes : objectmodule:utils/keyboard~keyCodesAn object with
keyName => keyCodepairs 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.
Interfaces
module:utils/keyboard~KeystrokeInfo
Type Definitions
module:utils/keyboard~ArrowKeyCodeDirection
Functions
getCode( key ) → numbermodule:utils/keyboard~getCodeConverts a key name or keystroke info into a key code.
Note: Key names are matched with
keyCodesin a case-insensitive way.Parameters
key : string | Readonly<KeystrokeInfo>A key name (see
keyCodes) or a keystroke data object.
Returns
numberKey or keystroke code.
getEnvKeystrokeText( keystroke, [ forcedEnv ] ) → stringmodule:utils/keyboard~getEnvKeystrokeTextTranslates any keystroke string text like
"Ctrl+A"to an environment–specific keystroke, i.e."⌘A"on macOS.Parameters
keystroke : stringThe keystroke text.
[ forcedEnv ] : 'PC' | 'Mac'The environment to force the key translation to. If not provided, the current environment is used.
Returns
stringThe keystroke text specific for the environment.
getLocalizedArrowKeyCodeDirection( keyCode, contentLanguageDirection ) → undefined | ArrowKeyCodeDirectionmodule:utils/keyboard~getLocalizedArrowKeyCodeDirectionReturns the direction in which the selection 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).
Parameters
keyCode : numberA key code as in
keyCode.contentLanguageDirection : LanguageDirectionThe content language direction, corresponding to
contentLanguageDirection.
Returns
undefined | ArrowKeyCodeDirectionLocalized arrow direction or
undefinedfor non-arrow key codes.
isArrowKeyCode( keyCode ) → booleanmodule:utils/keyboard~isArrowKeyCodeReturns
trueif the provided key code represents one of the arrow keys.Parameters
keyCode : numberA key code as in
keyCode.
Returns
boolean
isForwardArrowKeyCode( keyCode, contentLanguageDirection ) → booleanmodule:utils/keyboard~isForwardArrowKeyCodeDetermines if the provided key code moves the selection 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.
Parameters
keyCode : numberA key code as in
keyCode.contentLanguageDirection : LanguageDirectionThe content language direction, corresponding to
contentLanguageDirection.
Returns
boolean
parseKeystroke( keystroke ) → numbermodule:utils/keyboard~parseKeystrokeParses the keystroke and returns a keystroke code that will match the code returned by
getCodefor the corresponding keystroke info.The keystroke can be passed in two formats:
- as a single string – e.g.
ctrl + A, - as an array of known key names and key codes – e.g.:
[ 'ctrl', 32 ](ctrl + space),[ 'ctrl', 'a' ](ctrl + A).
Note: Key names are matched with
keyCodesin a case-insensitive way.Note: Only keystrokes with a single non-modifier key are supported (e.g.
ctrl+Ais OK, butctrl+A+Bis not).Note: On macOS, keystroke handling is translating the
Ctrlkey to theCmdkey and handling only that keystroke. For example, a registered keystrokeCtrl+Awill be translated toCmd+Aon macOS. To disable the translation of some keystroke, use the forced modifier:Ctrl!+A(note the exclamation mark).Parameters
keystroke : string | readonly Array<string | number>The keystroke definition.
Returns
numberKeystroke code.
- as a single string – e.g.