OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 /** |
| 6 * @fileoverview A collection of JavaScript utilities used to simplify working |
| 7 * with keyboard events. |
| 8 */ |
| 9 |
| 10 |
| 11 goog.provide('cvox.KeyUtil'); |
| 12 |
| 13 |
| 14 /** |
| 15 * Create the namespace |
| 16 * @constructor |
| 17 */ |
| 18 cvox.KeyUtil = function() { |
| 19 }; |
| 20 |
| 21 /** |
| 22 * Convert a key event into an unambiguous string representation that's |
| 23 * unique but also human-readable enough for debugging. |
| 24 * |
| 25 * A key event with a keyCode of 76 ('L') and the control and alt keys down |
| 26 * would return the string 'Ctrl+Alt+L', for example. A key code that doesn't |
| 27 * correspond to a letter or number will return a string with a pound and |
| 28 * then its keyCode, like 'Ctrl+Alt+#39' for Right Arrow. |
| 29 * |
| 30 * The modifiers always come in this order: |
| 31 * |
| 32 * Ctrl |
| 33 * Alt |
| 34 * Shift |
| 35 * Meta |
| 36 * |
| 37 * @param {Object} keyEvent The keyEvent to convert. |
| 38 * @return {string} A string representation of the key event. |
| 39 */ |
| 40 cvox.KeyUtil.keyEventToString = function(keyEvent) { |
| 41 var str = ''; |
| 42 |
| 43 if (keyEvent.ctrlKey) { |
| 44 if (str) { |
| 45 str += '+'; |
| 46 } |
| 47 str += 'Ctrl'; |
| 48 } |
| 49 if (keyEvent.altKey) { |
| 50 if (str) { |
| 51 str += '+'; |
| 52 } |
| 53 str += 'Alt'; |
| 54 } |
| 55 if (keyEvent.shiftKey) { |
| 56 if (str) { |
| 57 str += '+'; |
| 58 } |
| 59 str += 'Shift'; |
| 60 } |
| 61 if (keyEvent.metaKey) { |
| 62 if (str) { |
| 63 str += '+'; |
| 64 } |
| 65 str += 'Meta'; |
| 66 } |
| 67 |
| 68 if (str) { |
| 69 str += '+'; |
| 70 } |
| 71 |
| 72 if (keyEvent.keyCode >= 65 && keyEvent.keyCode <= 90) { |
| 73 // A - Z |
| 74 str += String.fromCharCode(keyEvent.keyCode); |
| 75 } else if (keyEvent.keyCode >= 48 && keyEvent.keyCode <= 57) { |
| 76 // 0 - 9 |
| 77 str += String.fromCharCode(keyEvent.keyCode); |
| 78 } else { |
| 79 // Anything else |
| 80 str += '#' + keyEvent.keyCode; |
| 81 } |
| 82 |
| 83 return str; |
| 84 }; |
OLD | NEW |