ui.KeyboardShortcutHandler Extends goog.events.EventTarget
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.

Inheritance

Constructor

goog.ui.KeyboardShortcutHandler(keyTarget)

Parameters

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

Instance Methods

Public Protected Private
checkShortcut_(strokes) boolean
Checks if a particular keyboard shortcut is registered.
Arguments:
strokes : Array.<Object>
Strokes array.
Returns: boolean  True iff the keyboard is registred.
code »
clearKeyListener()
Removes the listener that was added by link #initializeKeyListener.
code »
disposeInternal()
No description.
code »
getAllShortcutsAreGlobal() boolean
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.
code »
getAlwaysPreventDefault() boolean
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.
code »
getAlwaysStopPropagation() boolean
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.
code »
getEventType(identifier) string
Returns event type for a specific shortcut.
Arguments:
identifier : string
Identifier for the shortcut task.
Returns: string  Theh event type.
code »
getGlobalKeys() !Array.<string>
No description.
Returns: !Array.<string>  The global keys, i.e. keys that are safe to always regard as shortcuts, even if entered in a textarea or input field.
code »
getModifierShortcutsAreGlobal() boolean
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.
code »
getShortcut_(strokesopt_indexopt_list) string | Object
Returns shortcut for a specific set of strokes.
Arguments:
strokes : Array.<number>
Strokes array.
opt_index : number=
Index in array to start with.
opt_list : Object=
List to search for shortcut in.
Returns: string | Object  The shortcut.
code »
handleGeckoKeyUp_(e)
Handler for when a keyup event is fired in Firefox (Gecko).
Arguments:
e : goog.events.BrowserEvent
The key event.
code »
handleKeyDown_(event)
Keypress handler.
Arguments:
event : goog.events.BrowserEvent
Keypress event.
code »
handleWindowsKeyPress_(e)
Handler for when a keypress event is fired on Windows.
Arguments:
e : goog.events.BrowserEvent
The key event.
code »
handleWindowsKeyUp_(e)
Handler for when a keyup event is fired on Windows.
Arguments:
e : goog.events.BrowserEvent
The key event.
code »
initializeKeyListener(keyTarget)
Adds a key event listener that triggers #handleKeyDown_ when keys are pressed.
Arguments:
keyTarget : goog.events.EventTarget | EventTarget
Event target that the event listener should be attached to.
code »
interpretStrokes_(initialIndexargs) !Array.<Object>
Parses the variable arguments for registerShortcut and unregisterShortcut.
Arguments:
initialIndex : number
The first index of "args" to treat as variable arguments.
args : Object
The "arguments" array passed to registerShortcut or unregisterShortcut. Please see the comments in registerShortcut for list of allowed forms.
Returns: !Array.<Object>  The sequence of objects containing the keyCode and modifiers of each key in sequence.
code »
isPossiblePrintableKey_(e) boolean
Returns whether this event is possibly used for typing a printable character. Windows uses ctrl+alt (a.k.a. alt-graph) keys for typing characters on European keyboards. Since only Firefox provides a method that can identify whether ctrl+alt keys are used for typing characters, we need to check whether Windows sends a keypress event to prevent firing shortcut event if this event is used for typing characters.
Arguments:
e : goog.events.BrowserEvent
The key event.
Returns: boolean  Whether this event is a possible printable-key event.
code »
isShortcutRegistered(var_args) boolean
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 #registerShortcut for syntax. In that case the method only takes one argument.
Arguments:
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.
code »
isValidShortcut_(event) boolean
Checks if a given keypress event may be treated as a shortcut.
Arguments:
event : goog.events.BrowserEvent
Keypress event.
Returns: boolean  Whether to attempt to process the event as a shortcut.
code »
registerShortcut(identifiervar_args)
Registers a keyboard shortcut.
Arguments:
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').
code »
setAllShortcutsAreGlobal(allShortcutsGlobal)
Sets whether to treat all shortcuts (including modifier shortcuts) as if the keys had been passed to the setGlobalKeys function.
Arguments:
allShortcutsGlobal : boolean
Whether to treat all shortcuts as global.
code »
setAllowSpaceKeyOnButtons(allowSpaceKeyOnButtons)
Sets whether to treat space key as a shortcut when the focused element is a checkbox, radiobutton or button.
Arguments:
allowSpaceKeyOnButtons : boolean
Whether to treat space key as a shortcut when the focused element is a checkbox, radiobutton or button.
code »
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.
Arguments:
alwaysPreventDefault : boolean
Whether to always call preventDefault.
code »
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.
Arguments:
alwaysStopPropagation : boolean
Whether to always call stopPropagation.
code »
setGlobalKeys(keys)
Sets the global keys; keys that are safe to always regarded as shortcuts, even if entered in a textarea or input field.
Arguments:
keys : Array.<number>
List of keys.
code »
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).
Arguments:
modifierShortcutsGlobal : boolean
Whether to treat shortcuts with modifiers as global.
code »
unregisterAll()
Unregisters all keyboard shortcuts.
code »
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 #registerShortcut for syntax. In that case the method only takes one argument.
Arguments:
var_args : ...(number | string | Array.<number>)
String representation, or array or list of alternating key codes and modifiers.
code »
addEventListener(typeopt_captureopt_handlerScope)
Use #listen instead, when possible. Otherwise, use goog.events.listen if you are passing Object (instead of Function) as handler. 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.
Arguments:
type : string
The type of the event to listen for.
: ?function():? | ?{handleEvent:function():?
No description.
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.
code »
assertInitialized_()
Asserts that the event target instance is initialized properly.
code »
dispatchEvent()
No description.
code »
disposeInternal()
Removes listeners from this object. Classes that extend EventTarget may need to override this method in order to remove references to DOM Elements and additional listeners.
code »
fireListeners()
No description.
code »
getListener()
No description.
code »
getListeners()
No description.
code »
getParentEventTarget() 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.
code »
hasListener()
No description.
code »
listen()
No description.
code »
listenOnce()
No description.
code »
removeAllListeners()
No description.
code »
removeEventListener(typeopt_captureopt_handlerScope)
Use #unlisten instead, when possible. Otherwise, use goog.events.unlisten if you are passing Object (instead of Function) as handler. 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.
Arguments:
type : string
The type of the event to listen for.
: ?function():? | ?{handleEvent:function():?
No description.
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.
code »
setParentEventTarget(parent)
Sets the parent of this event target to use for capture/bubble mechanism.
Arguments:
parent : goog.events.EventTarget
Parent listenable (null if none).
code »
setTargetForTesting(target)
Sets the target to be used for event.target when firing event. Mainly used for testing. For example, see goog.testing.events.mixinListenable.
Arguments:
target : !Object
The target.
code »
unlisten()
No description.
code »
unlistenByKey()
No description.
code »
addOnDisposeCallback(callbackopt_scope)
Invokes a callback function when this object is disposed. Callbacks are invoked in the order in which they were added.
Arguments:
callback : function(this:T):?
The callback function.
opt_scope : T=
An optional scope to call the callback in.
code »
dispose() void
Disposes of the object. If the object hasn't already been disposed of, calls #disposeInternal. Classes that extend goog.Disposable should override #disposeInternal in order to delete references to COM objects, DOM nodes, and other disposable objects. Reentrant.
Returns: void  Nothing.
code »
disposeInternal()
Deletes or nulls out any references to COM objects, DOM nodes, or other disposable objects. Classes that extend goog.Disposable should override this method. Not reentrant. To avoid calling it twice, it must only be called from the subclass' disposeInternal method. Everywhere else the public dispose method must be used. For example:
  mypackage.MyClass = function() {
    mypackage.MyClass.base(this, 'constructor');
    // Constructor logic specific to MyClass.
    ...
  };
  goog.inherits(mypackage.MyClass, goog.Disposable);

  mypackage.MyClass.prototype.disposeInternal = function() {
    // Dispose logic specific to MyClass.
    ...
    // Call superclass's disposeInternal at the end of the subclass's, like
    // in C++, to avoid hard-to-catch issues.
    mypackage.MyClass.base(this, 'disposeInternal');
  };
code »
getDisposed() boolean
Use #isDisposed instead. No description.
Returns: boolean  Whether the object has been disposed of.
code »
isDisposed() boolean
No description.
Returns: boolean  Whether the object has been disposed of.
code »
registerDisposable(disposable)
Associates a disposable object with this object so that they will be disposed together.
Arguments:
disposable : goog.disposable.IDisposable
that will be disposed when this object is disposed.
code »

Instance Properties

activeShortcutKeyForGecko_ :
Tracks the currently pressed shortcut key, for Firefox.
Code »
allShortcutsAreGlobal_ :
Whether to treat all shortcuts as if they had been passed to setGlobalKeys().
Code »
allowSpaceKeyOnButtons_ :
Whether to treat space key as a shortcut when the focused element is a checkbox, radiobutton or button.
Code »
alwaysPreventDefault_ :
Whether to always prevent the default action if a shortcut event is fired.
Code »
alwaysStopPropagation_ :
Whether to always stop propagation if a shortcut event is fired.
Code »
constructor :
No description.
Code »
globalKeys_ :
List of numeric key codes for keys that are safe to always regarded as shortcuts, even if entered in a textarea or input field.
Code »
isPrintableKey_ :
Whether a key event is a printable-key event. Windows uses ctrl+alt (alt-graph) keys to type characters on European keyboards. For such keys, we cannot identify whether these keys are used for typing characters when receiving keydown events. Therefore, we set this flag when we receive their respective keypress events and fire shortcut events only when we do not receive them.
Code »
Target on which to listen for key events.
Code »
lastKeys_ :
List of the last sequence of strokes. Object contains time last key was pressed and an array of strokes, represented by numeric value.
Code »
metaKeyRecentlyReleased_ :
Due to a bug in the way that Gecko on Mac handles cut/copy/paste key events using the meta key, it is necessary to fake the keyDown for the action key (C,V,X) by capturing it on keyUp. Because users will often release the meta key a slight moment before they release the action key, we need this variable that will store whether the meta key has been released recently. It will be cleared after a short delay in the key handling logic.
Code »
modifierShortcutsAreGlobal_ :
Whether to treat shortcuts with modifiers as if they had been passed to setGlobalKeys(). Ignored if allShortcutsAreGlobal_ is true. Applies only to form elements (not content-editable).
Code »
shortcuts_ :
Registered keyboard shortcuts tree. Stored as a map with the keyCode and modifier(s) as the key and either a list of further strokes or the shortcut task identifier as the value.
Code »
textInputs_ :
List of input types that should only accept ENTER as a shortcut.
Code »
actualEventTarget_ : goog.events.EventTarget
The object to use for event.target. Useful when mixing in an EventTarget to another object.
Code »
constructor :
No description.
Code »
eventTargetListeners_ : goog.events.ListenerMap
Maps of event type to an array of listeners.
Code »
parentEventTarget_ : goog.events.EventTarget
Parent event target, used during event bubbling. TODO(user): Change this to goog.events.Listenable. This currently breaks people who expect getParentEventTarget to return goog.events.EventTarget.
Code »
creationStack :
If monitoring the goog.Disposable instances is enabled, stores the creation stack trace of the Disposable instance.
Code »
disposed_ :
Whether the object has been disposed of.
Code »
onDisposeCallbacks_ :
Callbacks to invoke when this object is disposed.
Code »

Static Methods

goog.ui.KeyboardShortcutHandler.getKeyCode(name) number
Static method for getting the key code for a given key.
Arguments:
name : string
Name of key.
Returns: number  The key code.
code »
goog.ui.KeyboardShortcutHandler.makeKey_(keyCodemodifiers) number
Constructs key from key code and modifiers. The lower 8 bits are used for the key code, the following 3 for modifiers and the remaining bits are unused.
Arguments:
keyCode : number
Numeric key code.
modifiers : number
Required modifiers.
Returns: number  The key.
code »
goog.ui.KeyboardShortcutHandler.parseStringShortcut(s) !Array.<Object>
Builds stroke array from string representation of shortcut.
Arguments:
s : string
String representation of shortcut.
Returns: !Array.<Object>  The stroke array.
code »
goog.ui.KeyboardShortcutHandler.setShortcut_(parentstrokesidentifier)
Adds or removes a stroke node to/from the given parent node.
Arguments:
parent : Object
Parent node to add/remove stroke to/from.
strokes : Array.<Object>
Array of strokes for shortcut.
identifier : ?string
Identifier for the task performed by shortcut or null to clear.
code »

Static Properties

goog.ui.KeyboardShortcutHandler.DEFAULT_GLOBAL_KEYS_ :
Keys marked as global by default.
Code »
goog.ui.KeyboardShortcutHandler.DEFAULT_TEXT_INPUTS_ :
Text input types to allow only ENTER shortcuts. Web Forms 2.0 for HTML5: Section 4.10.7 from 29 May 2012.
Code »
goog.ui.KeyboardShortcutHandler.MAX_KEY_SEQUENCE_DELAY :
Maximum allowed delay, in milliseconds, allowed between the first and second key in a key sequence.
Code »
goog.ui.KeyboardShortcutHandler.nameToKeyCodeCache_ :
Cache for name to key code lookup.
Code »
goog.ui.KeyboardShortcutHandler.superClass_ :
No description.
Code »

Enumerations

goog.ui.KeyboardShortcutHandler.EventType :
Events.
Constants:
SHORTCUT_PREFIX
No description.
SHORTCUT_TRIGGERED
No description.
Code »
goog.ui.KeyboardShortcutHandler.Modifiers :
Bit values for modifier keys.
Constants:
ALT
No description.
CTRL
No description.
META
No description.
NONE
No description.
SHIFT
No description.
Code »

Package ui

Package Reference