The goog.ui.KeyboardShortcutHandler Class

goog.ui.KeyboardShortcutHandler
> goog.events.EventTarget
> goog.Disposable

goog.ui.KeyboardShortcutHandler(keyTarget)

Component for handling keyboard shortcuts. A shortcut is registered and bound to a specific identifier. Once the shortcut is triggered an event is fired with the identifier for the shortcut. This allows keyboard shortcuts to be customized without modifying the code that listens for them. Supports keyboard shortcuts triggered by a single key, a stroke stroke (key plus at least one modifier) and a sequence of keys or strokes.

keyTarget {goog.events.EventTarget|EventTarget}
Event target that the key event listener is attached to, typically the applications root container.

The goog.ui.KeyboardShortcutHandler.EventType Enum

Events. … more

.MAX_KEY_SEQUENCE_DELAY {number}

Maximum allowed delay, in milliseconds, allowed between the first and second key in a key sequence.

The goog.ui.KeyboardShortcutHandler.Modifiers Enum

Bit values for modifier keys. … more

.addEventListener(type, handler, opt_capture, opt_handlerScope)

Inherited from goog.events.EventTarget .

Adds an event listener to the event target. The same handler can only be added once per the type. Even if you add the same handler multiple times using the same type then it will only be called once when the event is dispatched. Supported for legacy but use goog.events.listen(src, type, handler) instead.

type {string}
The type of the event to listen for.
handler {Function|Object}
The function to handle the event. The handler can also be an object that implements the handleEvent method which takes the event object as argument.
opt_capture {boolean=}
In DOM-compliant browsers, this determines whether the listener is fired during the capture or bubble phase of the event.
opt_handlerScope {Object=}
Object in whose scope to call the listener.

.addOnDisposeCallback(callback, opt_scope)

Inherited from goog.Disposable .

Invokes a callback function when this object is disposed. Callbacks are invoked in the order in which they were added.

callback {!Function}
The callback function.
opt_scope {Object=}
An optional scope to call the callback in.

.creationStack {string}

Inherited from goog.Disposable .

If monitoring the goog.Disposable instances is enabled, stores the creation stack trace of the Disposable instance.

.dispatchEvent(e)

Inherited from goog.events.EventTarget .

Dispatches an event (or event like object) and calls all listeners listening for events of this type. The type of the event is decided by the type property on the event object. If any of the listeners returns false OR calls preventDefault then this function will return false. If one of the capture listeners calls stopPropagation, then the bubble listeners won't fire.

e {string|Object|goog.events.Event}
Event object.
returns {boolean}
If anyone called preventDefault on the event object (or if any of the handlers returns false this will also return false.

.dispose()

Inherited from goog.Disposable .

Disposes of the object. If the object hasn't already been disposed of, calls {@link #disposeInternal}. Classes that extend {@code goog.Disposable} should override {@link #disposeInternal} in order to delete references to COM objects, DOM nodes, and other disposable objects. Reentrant.

returns {void}
Nothing.

.getAllShortcutsAreGlobal()

Returns whether all shortcuts (including modifier shortcuts) are treated as if the keys had been passed to the setGlobalKeys function.

returns {boolean}
Whether all shortcuts are treated as globals.
@see
#setAllShortcutsAreGlobal

.getAlwaysPreventDefault()

Returns whether the default action will always be prevented when a shortcut event is fired. The default value is true.

returns {boolean}
Whether preventDefault will always be called.
@see
#setAlwaysPreventDefault

.getAlwaysStopPropagation()

Returns whether the event will always be stopped from propagating beyond its target when a shortcut event is fired. The default value is false.

returns {boolean}
Whether stopPropagation will always be called.
@see
#setAlwaysStopPropagation

.getEventType(identifier)

Returns event type for a specific shortcut.

identifier {string}
Identifier for the shortcut task.
returns {string}
Theh event type.

.getGlobalKeys()

returns {Array.<number>}
The global keys, i.e. keys that are safe to always regard as shortcuts, even if entered in a textarea or input field.

.getKeyCode(name)

Static method for getting the key code for a given key.

name {string}
Name of key.
returns {number}
The key code.

.getModifierShortcutsAreGlobal()

Returns whether shortcuts with modifiers are treated as if the keys had been passed to the setGlobalKeys function. Ignored if you have called setAllShortcutsAreGlobal(true). Applies only to form elements (not content-editable).

returns {boolean}
Whether shortcuts with modifiers are treated as globals.
@see
#setModifierShortcutsAreGlobal

.getParentEventTarget()

Inherited from goog.events.EventTarget .

Returns the parent of this event target to use for bubbling.

returns {goog.events.EventTarget}
The parent EventTarget or null if there is no parent.

.isDisposed()

Inherited from goog.Disposable .

returns {boolean}
Whether the object has been disposed of.

.isShortcutRegistered(var_args)

Verifies if a particular keyboard shortcut is registered already. It has the same interface as the unregistering of shortcuts. param {number} keyCode Numeric code for key param {number=} opt_modifiers Bitmap indicating required modifier keys. goog.ui.KeyboardShortcutHandler.Modifiers.SHIFT, CONTROL, ALT, or META. The two parameters can be repeated any number of times to create a shortcut using a sequence of strokes. A string representation of the shortcut can be supplied instead see {@link #registerShortcut} for syntax. In that case the method only takes one argument.

var_args {...(number|string|Array.<number>)}
String representation, or array or list of alternating key codes and modifiers.
returns {boolean}
Whether the specified keyboard shortcut is registered.

.parseStringShortcut(s)

Builds stroke array from string representation of shortcut.

s {string}
String representation of shortcut.
returns {Array.<Object>}
The stroke array.

.registerDisposable(disposable)

Inherited from goog.Disposable .

Associates a disposable object with this object so that they will be disposed together.

disposable {goog.disposable.IDisposable}
that will be disposed when this object is disposed.

.registerShortcut(identifier, var_args)

Registers a keyboard shortcut.

identifier {string}
Identifier for the task performed by the keyboard combination. Multiple shortcuts can be provided for the same task by specifying the same identifier.
var_args {...(number|string|Array.<number>)}
See below. param {number} keyCode Numeric code for key param {number=} opt_modifiers Bitmap indicating required modifier keys. goog.ui.KeyboardShortcutHandler.Modifiers.SHIFT, CONTROL, ALT, or META. The last two parameters can be repeated any number of times to create a shortcut using a sequence of strokes. Instead of varagrs the second parameter could also be an array where each element would be ragarded as a parameter. A string representation of the shortcut can be supplied instead of the last two parameters. In that case the method only takes two arguments, the identifier and the string. Examples: g registerShortcut(str, G_KEYCODE) Ctrl+g registerShortcut(str, G_KEYCODE, CTRL) Ctrl+Shift+g registerShortcut(str, G_KEYCODE, CTRL | SHIFT) Ctrl+g a registerShortcut(str, G_KEYCODE, CTRL, A_KEYCODE) Ctrl+g Shift+a registerShortcut(str, G_KEYCODE, CTRL, A_KEYCODE, SHIFT) g a registerShortcut(str, G_KEYCODE, NONE, A_KEYCODE) Examples using string representation for shortcuts: g registerShortcut(str, 'g') Ctrl+g registerShortcut(str, 'ctrl+g') Ctrl+Shift+g registerShortcut(str, 'ctrl+shift+g') Ctrl+g a registerShortcut(str, 'ctrl+g a') Ctrl+g Shift+a registerShortcut(str, 'ctrl+g shift+a') g a registerShortcut(str, 'g a').

.removeEventListener(type, handler, opt_capture, opt_handlerScope)

Inherited from goog.events.EventTarget .

Removes an event listener from the event target. The handler must be the same object as the one added. If the handler has not been added then nothing is done.

type {string}
The type of the event to listen for.
handler {Function|Object}
The function to handle the event. The handler can also be an object that implements the handleEvent method which takes the event object as argument.
opt_capture {boolean=}
In DOM-compliant browsers, this determines whether the listener is fired during the capture or bubble phase of the event.
opt_handlerScope {Object=}
Object in whose scope to call the listener.

.setAllShortcutsAreGlobal(allShortcutsGlobal)

Sets whether to treat all shortcuts (including modifier shortcuts) as if the keys had been passed to the setGlobalKeys function.

allShortcutsGlobal {boolean}
Whether to treat all shortcuts as global.

.setAlwaysPreventDefault(alwaysPreventDefault)

Sets whether to always prevent the default action when a shortcut event is fired. If false, the default action is prevented only if preventDefault is called on either of the corresponding SHORTCUT_TRIGGERED or SHORTCUT_PREFIX events. If true, the default action is prevented whenever a shortcut event is fired. The default value is true.

alwaysPreventDefault {boolean}
Whether to always call preventDefault.

.setAlwaysStopPropagation(alwaysStopPropagation)

Sets whether to always stop propagation for the event when fired. If false, the propagation is stopped only if stopPropagation is called on either of the corresponding SHORT_CUT_TRIGGERED or SHORTCUT_PREFIX events. If true, the event is prevented from propagating beyond its target whenever it is fired. The default value is false.

alwaysStopPropagation {boolean}
Whether to always call stopPropagation.

.setGlobalKeys(keys)

Sets the global keys; keys that are safe to always regarded as shortcuts, even if entered in a textarea or input field.

keys {Array.<number>}
List of keys.

.setModifierShortcutsAreGlobal(modifierShortcutsGlobal)

Sets whether to treat shortcuts with modifiers as if the keys had been passed to the setGlobalKeys function. Ignored if you have called setAllShortcutsAreGlobal(true). Applies only to form elements (not content-editable).

modifierShortcutsGlobal {boolean}
Whether to treat shortcuts with modifiers as global.

.setParentEventTarget(parent)

Inherited from goog.events.EventTarget .

Sets the parent of this event target to use for bubbling.

parent {goog.events.EventTarget?}
Parent EventTarget (null if none).

.unregisterAll()

Unregisters all keyboard shortcuts.

.unregisterShortcut(var_args)

Unregisters a keyboard shortcut by keyCode and modifiers or string representation of sequence. param {number} keyCode Numeric code for key param {number=} opt_modifiers Bitmap indicating required modifier keys. goog.ui.KeyboardShortcutHandler.Modifiers.SHIFT, CONTROL, ALT, or META. The two parameters can be repeated any number of times to create a shortcut using a sequence of strokes. A string representation of the shortcut can be supplied instead see {@link #registerShortcut} for syntax. In that case the method only takes one argument.

var_args {...(number|string|Array.<number>)}
String representation, or array or list of alternating key codes and modifiers.