1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * Namespace for keyboard utility functions.
11 * Swallows left and right keypress and keyup events.
12 * @param {KeyboardEvent} event Raised event.
15 keyboard.onKeyIgnore_ = function(event) {
16 if (event.ctrlKey || event.shiftKey || event.altKey || event.metaKey)
19 if (event.keyIdentifier == 'Left' || event.keyIdentifier == 'Right') {
20 event.stopPropagation();
21 event.preventDefault();
26 * Converts right/left into tab/shift-tab key events.
27 * @param {KeyboardEvent} event Raised event.
30 keyboard.onKeyDown_ = function(event) {
31 if (event.ctrlKey || event.shiftKey || event.altKey || event.metaKey)
34 if (event.keyIdentifier == 'Left') {
35 keyboard.raiseKeyFocusPrevious(document.activeElement);
36 event.stopPropagation();
37 event.preventDefault();
38 } else if (event.keyIdentifier == 'Right') {
39 keyboard.raiseKeyFocusNext(document.activeElement);
40 event.stopPropagation();
41 event.preventDefault();
46 * Raises tab/shift-tab keyboard events.
47 * @param {HTMLElement} element Element that should receive the event.
48 * @param {string} eventType Keyboard event type.
49 * @param {boolean} shift True if shift should be on.
52 keyboard.raiseTabKeyEvent_ = function(element, eventType, shift) {
53 var event = document.createEvent('KeyboardEvent');
54 event.initKeyboardEvent(
65 element.dispatchEvent(event);
69 * Raises shift+tab keyboard events to focus previous element.
70 * @param {HTMLElement} element Element that should receive the event.
72 keyboard.raiseKeyFocusPrevious = function(element) {
73 keyboard.raiseTabKeyEvent_(element, 'keydown', true);
74 keyboard.raiseTabKeyEvent_(element, 'keypress', true);
75 keyboard.raiseTabKeyEvent_(element, 'keyup', true);
79 * Raises tab keyboard events to focus next element.
80 * @param {HTMLElement} element Element that should receive the event.
82 keyboard.raiseKeyFocusNext = function(element) {
83 keyboard.raiseTabKeyEvent_(element, 'keydown', false);
84 keyboard.raiseTabKeyEvent_(element, 'keypress', false);
85 keyboard.raiseTabKeyEvent_(element, 'keyup', false);
89 * Initializes event handling for arrow keys driven focus flow.
91 keyboard.initializeKeyboardFlow = function() {
92 document.addEventListener('keydown',
93 keyboard.onKeyDown_, true);
94 document.addEventListener('keypress',
95 keyboard.onKeyIgnore_, true);
96 document.addEventListener('keyup',
97 keyboard.onKeyIgnore_, true);