Fork me on GitHub

editor/keyboard

This modules handles low-level keyboard events and normalize them across browsers.

Methods

delegateKeyboardEvents(textarea: Element, handlers: [string]:any)privatestatic

Setup to capture the keyboard events from a TextArea and redispatch them to handlers.

In general, commands (arrows, delete, etc..) should be handled in the keystroke() handler while text input should be handled in typedtext().

textarea : Element

A TextArea element that will capture the keyboard events. While this element will usually be a TextArea, it could be any element that is focusable and can receive keyboard events.

handlers : [string]:any
handlers.container : Element
handlers.keystroke : function

invoked on a key down event, including for special keys such as ESC, arrow keys, tab, etc... and their variants with modifiers.

handlers.typedtext : function

invoked on a keypress or other events when a key corresponding to a character has been pressed. This include a-z, 0-9, {}, ^_(), etc... This does not include arrow keys, tab, etc... but does include 'space' When a 'character' key is pressed, both keystroke() and typedtext() will be invoked. When a control/function key is pressed, only keystroke() will be invoked. In some cases, for example when using input methods or entering emoji, only typedtext() will be invoked.

handlers.paste(text) : function

Invoked in response to a paste command. Not all browsers support this (Chrome doesn't), so typedtext() will be invoked instead.

handlers.cut : function
handlers.copy : function

keyboardEventToString(evt: Event)static

Create a normalized string representation of the key combo, i.e., key code and modifier keys. For example:

  • Ctrl-Shift-Alt-KeyF
  • Alt-Space
  • Shift-Digit6
evt : Event
To Do:
  • See https://github.com/madrobby/keymaster/blob/master/keymaster.js - Doesn't work very well for command- - Returns "Alt-Alt" when only the Alt key is pressed