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