keys.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*! @license
  2. ==========================================================================
  3. SproutCore -- JavaScript Application Framework
  4. copyright 2006-2009, Sprout Systems Inc., Apple Inc. and contributors.
  5. Permission is hereby granted, free of charge, to any person obtaining a
  6. copy of this software and associated documentation files (the "Software"),
  7. to deal in the Software without restriction, including without limitation
  8. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. and/or sell copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. DEALINGS IN THE SOFTWARE.
  20. SproutCore and the SproutCore logo are trademarks of Sprout Systems, Inc.
  21. For more information about SproutCore, visit http://www.sproutcore.com
  22. ==========================================================================
  23. @license */
  24. // Most of the following code is taken from SproutCore with a few changes.
  25. define(function(require, exports, module) {
  26. "use strict";
  27. require("./fixoldbrowsers");
  28. var oop = require("./oop");
  29. /*
  30. * Helper functions and hashes for key handling.
  31. */
  32. var Keys = (function() {
  33. var ret = {
  34. MODIFIER_KEYS: {
  35. 16: 'Shift', 17: 'Ctrl', 18: 'Alt', 224: 'Meta'
  36. },
  37. KEY_MODS: {
  38. "ctrl": 1, "alt": 2, "option" : 2, "shift": 4,
  39. "super": 8, "meta": 8, "command": 8, "cmd": 8
  40. },
  41. FUNCTION_KEYS : {
  42. 8 : "Backspace",
  43. 9 : "Tab",
  44. 13 : "Return",
  45. 19 : "Pause",
  46. 27 : "Esc",
  47. 32 : "Space",
  48. 33 : "PageUp",
  49. 34 : "PageDown",
  50. 35 : "End",
  51. 36 : "Home",
  52. 37 : "Left",
  53. 38 : "Up",
  54. 39 : "Right",
  55. 40 : "Down",
  56. 44 : "Print",
  57. 45 : "Insert",
  58. 46 : "Delete",
  59. 96 : "Numpad0",
  60. 97 : "Numpad1",
  61. 98 : "Numpad2",
  62. 99 : "Numpad3",
  63. 100: "Numpad4",
  64. 101: "Numpad5",
  65. 102: "Numpad6",
  66. 103: "Numpad7",
  67. 104: "Numpad8",
  68. 105: "Numpad9",
  69. '-13': "NumpadEnter",
  70. 112: "F1",
  71. 113: "F2",
  72. 114: "F3",
  73. 115: "F4",
  74. 116: "F5",
  75. 117: "F6",
  76. 118: "F7",
  77. 119: "F8",
  78. 120: "F9",
  79. 121: "F10",
  80. 122: "F11",
  81. 123: "F12",
  82. 144: "Numlock",
  83. 145: "Scrolllock"
  84. },
  85. PRINTABLE_KEYS: {
  86. 32: ' ', 48: '0', 49: '1', 50: '2', 51: '3', 52: '4', 53: '5',
  87. 54: '6', 55: '7', 56: '8', 57: '9', 59: ';', 61: '=', 65: 'a',
  88. 66: 'b', 67: 'c', 68: 'd', 69: 'e', 70: 'f', 71: 'g', 72: 'h',
  89. 73: 'i', 74: 'j', 75: 'k', 76: 'l', 77: 'm', 78: 'n', 79: 'o',
  90. 80: 'p', 81: 'q', 82: 'r', 83: 's', 84: 't', 85: 'u', 86: 'v',
  91. 87: 'w', 88: 'x', 89: 'y', 90: 'z', 107: '+', 109: '-', 110: '.',
  92. 186: ';', 187: '=', 188: ',', 189: '-', 190: '.', 191: '/', 192: '`',
  93. 219: '[', 220: '\\',221: ']', 222: "'", 111: '/', 106: '*'
  94. }
  95. };
  96. // A reverse map of FUNCTION_KEYS
  97. var name, i;
  98. for (i in ret.FUNCTION_KEYS) {
  99. name = ret.FUNCTION_KEYS[i].toLowerCase();
  100. ret[name] = parseInt(i, 10);
  101. }
  102. // A reverse map of PRINTABLE_KEYS
  103. for (i in ret.PRINTABLE_KEYS) {
  104. name = ret.PRINTABLE_KEYS[i].toLowerCase();
  105. ret[name] = parseInt(i, 10);
  106. }
  107. // Add the MODIFIER_KEYS, FUNCTION_KEYS and PRINTABLE_KEYS to the KEY
  108. // variables as well.
  109. oop.mixin(ret, ret.MODIFIER_KEYS);
  110. oop.mixin(ret, ret.PRINTABLE_KEYS);
  111. oop.mixin(ret, ret.FUNCTION_KEYS);
  112. // aliases
  113. ret.enter = ret["return"];
  114. ret.escape = ret.esc;
  115. ret.del = ret["delete"];
  116. // workaround for firefox bug
  117. ret[173] = '-';
  118. (function() {
  119. var mods = ["cmd", "ctrl", "alt", "shift"];
  120. for (var i = Math.pow(2, mods.length); i--;) {
  121. ret.KEY_MODS[i] = mods.filter(function(x) {
  122. return i & ret.KEY_MODS[x];
  123. }).join("-") + "-";
  124. }
  125. })();
  126. ret.KEY_MODS[0] = "";
  127. ret.KEY_MODS[-1] = "input-";
  128. return ret;
  129. })();
  130. oop.mixin(exports, Keys);
  131. exports.keyCodeToString = function(keyCode) {
  132. // Language-switching keystroke in Chrome/Linux emits keyCode 0.
  133. var keyString = Keys[keyCode];
  134. if (typeof keyString != "string")
  135. keyString = String.fromCharCode(keyCode);
  136. return keyString.toLowerCase();
  137. };
  138. });