Skip to content

Commit

Permalink
handle events without deprecated keyCode property (#5543)
Browse files Browse the repository at this point in the history
  • Loading branch information
nightwing committed May 8, 2024
1 parent 8e51966 commit 7591120
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 143 deletions.
10 changes: 10 additions & 0 deletions src/editor_commands_test.js
Expand Up @@ -564,6 +564,16 @@ module.exports = {

url = editor.findLinkAt(1, 5);
assert.equal(url, "https://www.google.com/");
},
"test handle events without deprecated keyCode property": function() {
var e = new CustomEvent("keydown");
e.code = "KeyA";
e.ctrlKey = true;
editor = new Editor(new MockRenderer());
editor.session.setValue("123");
assert.equal(editor.getSelectedText(), "");
editor.textInput.getElement().dispatchEvent(e);
assert.equal(editor.getSelectedText(), "123");
}
};

Expand Down
4 changes: 4 additions & 0 deletions src/lib/event.js
Expand Up @@ -203,6 +203,10 @@ exports.getModifierString = function(e) {
function normalizeCommandKeys(callback, e, keyCode) {
var hashId = getModifierHash(e);

if (!keyCode && e.code) {
keyCode = keys.$codeToKeyCode[e.code] || keyCode;
}

if (!useragent.isMac && pressedKeys) {
if (e.getModifierState && (e.getModifierState("OS") || e.getModifierState("Win")))
hashId |= 8;
Expand Down
285 changes: 142 additions & 143 deletions src/lib/keys.js
@@ -1,154 +1,153 @@
/*! @license
==========================================================================
SproutCore -- JavaScript Application Framework
copyright 2006-2009, Sprout Systems Inc., Apple Inc. and contributors.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
SproutCore and the SproutCore logo are trademarks of Sprout Systems, Inc.
For more information about SproutCore, visit http://www.sproutcore.com
==========================================================================
@license */

// Most of the following code is taken from SproutCore with a few changes.

"use strict";

var oop = require("./oop");

/*
* Helper functions and hashes for key handling.
*/
var Keys = (function() {
var ret = {
MODIFIER_KEYS: {
16: 'Shift', 17: 'Ctrl', 18: 'Alt', 224: 'Meta',
91: 'MetaLeft', 92: 'MetaRight', 93: 'ContextMenu'
},

KEY_MODS: {
"ctrl": 1, "alt": 2, "option" : 2, "shift": 4,
"super": 8, "meta": 8, "command": 8, "cmd": 8,
"control": 1
},

FUNCTION_KEYS : {
8 : "Backspace",
9 : "Tab",
13 : "Return",
19 : "Pause",
27 : "Esc",
32 : "Space",
33 : "PageUp",
34 : "PageDown",
35 : "End",
36 : "Home",
37 : "Left",
38 : "Up",
39 : "Right",
40 : "Down",
44 : "Print",
45 : "Insert",
46 : "Delete",
96 : "Numpad0",
97 : "Numpad1",
98 : "Numpad2",
99 : "Numpad3",
100: "Numpad4",
101: "Numpad5",
102: "Numpad6",
103: "Numpad7",
104: "Numpad8",
105: "Numpad9",
'-13': "NumpadEnter",
112: "F1",
113: "F2",
114: "F3",
115: "F4",
116: "F5",
117: "F6",
118: "F7",
119: "F8",
120: "F9",
121: "F10",
122: "F11",
123: "F12",
144: "Numlock",
145: "Scrolllock"
},

PRINTABLE_KEYS: {
32: ' ', 48: '0', 49: '1', 50: '2', 51: '3', 52: '4', 53: '5',
54: '6', 55: '7', 56: '8', 57: '9', 59: ';', 61: '=', 65: 'a',
66: 'b', 67: 'c', 68: 'd', 69: 'e', 70: 'f', 71: 'g', 72: 'h',
73: 'i', 74: 'j', 75: 'k', 76: 'l', 77: 'm', 78: 'n', 79: 'o',
80: 'p', 81: 'q', 82: 'r', 83: 's', 84: 't', 85: 'u', 86: 'v',
87: 'w', 88: 'x', 89: 'y', 90: 'z', 107: '+', 109: '-', 110: '.',
186: ';', 187: '=', 188: ',', 189: '-', 190: '.', 191: '/', 192: '`',
219: '[', 220: '\\',221: ']', 222: "'", 111: '/', 106: '*'
}
};

// workaround for firefox bug
ret.PRINTABLE_KEYS[173] = '-';

// A reverse map of FUNCTION_KEYS
var name, i;
for (i in ret.FUNCTION_KEYS) {
name = ret.FUNCTION_KEYS[i].toLowerCase();
ret[name] = parseInt(i, 10);
}

// A reverse map of PRINTABLE_KEYS
for (i in ret.PRINTABLE_KEYS) {
name = ret.PRINTABLE_KEYS[i].toLowerCase();
ret[name] = parseInt(i, 10);
var Keys = {
MODIFIER_KEYS: {
16: 'Shift', 17: 'Ctrl', 18: 'Alt', 224: 'Meta',
91: 'MetaLeft', 92: 'MetaRight', 93: 'ContextMenu'
},

KEY_MODS: {
"ctrl": 1, "alt": 2, "option" : 2, "shift": 4,
"super": 8, "meta": 8, "command": 8, "cmd": 8,
"control": 1
},

FUNCTION_KEYS : {
8 : "Backspace",
9 : "Tab",
13 : "Return",
19 : "Pause",
27 : "Esc",
32 : "Space",
33 : "PageUp",
34 : "PageDown",
35 : "End",
36 : "Home",
37 : "Left",
38 : "Up",
39 : "Right",
40 : "Down",
44 : "Print",
45 : "Insert",
46 : "Delete",
'-13': "NumpadEnter",
144: "Numlock",
145: "Scrolllock"
},

PRINTABLE_KEYS: {
32: ' ', 59: ';', 61: '=', 107: '+', 109: '-', 110: '.',
186: ';', 187: '=', 188: ',', 189: '-', 190: '.', 191: '/', 192: '`',
219: '[', 220: '\\',221: ']', 222: "'", 111: '/', 106: '*'
}
};

// Add the MODIFIER_KEYS, FUNCTION_KEYS and PRINTABLE_KEYS to the KEY
// variables as well.
oop.mixin(ret, ret.MODIFIER_KEYS);
oop.mixin(ret, ret.PRINTABLE_KEYS);
oop.mixin(ret, ret.FUNCTION_KEYS);

// aliases
ret.enter = ret["return"];
ret.escape = ret.esc;
ret.del = ret["delete"];

(function() {
var mods = ["cmd", "ctrl", "alt", "shift"];
for (var i = Math.pow(2, mods.length); i--;) {
ret.KEY_MODS[i] = mods.filter(function(x) {
return i & ret.KEY_MODS[x];
}).join("-") + "-";
}
})();

ret.KEY_MODS[0] = "";
ret.KEY_MODS[-1] = "input-";

return ret;
var codeToKeyCode = {
Command: 224,
Backspace: 8,
Tab: 9,
Return: 13,
Enter: 13,
Pause: 19,
Escape: 27,
PageUp: 33,
PageDown: 34,
End: 35,
Home: 36,
Insert: 45,
Delete: 46,
ArrowLeft: 37,
ArrowUp: 38,
ArrowRight: 39,
ArrowDown: 40,
// special keys
Backquote: 192,
Minus: 189,
Equal: 187,
BracketLeft: 219,
Backslash: 220,
BracketRight: 221,
Semicolon: 186,
Quote: 222,
Comma: 188,
Period: 190,
Slash: 191,
Space: 32,
NumpadAdd: 107,
NumpadDecimal: 110,
NumpadSubtract: 109,
NumpadDivide: 111,
NumpadMultiply: 106
};
for (var i = 0; i < 10; i++) {
codeToKeyCode["Digit" + i] = 48 + i;
codeToKeyCode["Numpad" + i] = 96 + i;
Keys.PRINTABLE_KEYS[48 + i] = "" + i;
Keys.FUNCTION_KEYS[96 + i] = "Numpad" + i;
}
for (var i = 65; i < 91; i++) {
var chr = String.fromCharCode(i + 32);
codeToKeyCode["Key" + chr.toUpperCase()] = i;
Keys.PRINTABLE_KEYS[i] = chr;
}
for (var i = 1; i < 13; i++) {
codeToKeyCode["F" + i] = 111 + i;
Keys.FUNCTION_KEYS[111 + i] = "F" + i;
}
var modifiers = {
Shift: 16,
Control: 17,
Alt: 18,
Meta: 224
};
for (var mod in modifiers) {
codeToKeyCode[mod] = codeToKeyCode[mod + "Left"]
= codeToKeyCode[mod + "Right"] = modifiers[mod];
}
exports.$codeToKeyCode = codeToKeyCode;

// workaround for firefox bug
Keys.PRINTABLE_KEYS[173] = '-';

// A reverse map of FUNCTION_KEYS
for (var j in Keys.FUNCTION_KEYS) {
var name = Keys.FUNCTION_KEYS[j].toLowerCase();
Keys[name] = parseInt(j, 10);
}

// A reverse map of PRINTABLE_KEYS
for (var j in Keys.PRINTABLE_KEYS) {
var name = Keys.PRINTABLE_KEYS[j].toLowerCase();
Keys[name] = parseInt(j, 10);
}

// Add the MODIFIER_KEYS, FUNCTION_KEYS and PRINTABLE_KEYS to the KEY
// variables as well.
oop.mixin(Keys, Keys.MODIFIER_KEYS);
oop.mixin(Keys, Keys.PRINTABLE_KEYS);
oop.mixin(Keys, Keys.FUNCTION_KEYS);

// aliases
Keys.enter = Keys["return"];
Keys.escape = Keys.esc;
Keys.del = Keys["delete"];

(function() {
var mods = ["cmd", "ctrl", "alt", "shift"];
for (var i = Math.pow(2, mods.length); i--;) {
Keys.KEY_MODS[i] = mods.filter(function(x) {
return i & Keys.KEY_MODS[x];
}).join("-") + "-";
}
})();

Keys.KEY_MODS[0] = "";
Keys.KEY_MODS[-1] = "input-";


oop.mixin(exports, Keys);

exports.default = exports;
Expand Down

0 comments on commit 7591120

Please sign in to comment.