Bug 1337545 - Use keydown event for keycodes in commands r=kmag

When you create a <key> element, which is what the commands API
does under the hood to allow keyboard shortcuts, the event type
that the handler listens to defaults to keypress. For printable
characters, the keyCode property of a keypress event is 0,
causing keycode bindings for these printable characters to not
work. We could use key for these properties, but the shift key
interacts with the key property in ways that make it simpler to
simply fix the keycode usage for our purposes.

MozReview-Commit-ID: HdWdN7cMfuq

--HG--
extra : rebase_source : d75aee5472f43575adfb68f201503f9a57c2deea
This commit is contained in:
Doug Thayer 2017-05-01 13:49:18 -07:00
Родитель 6c326a4688
Коммит 7f43e4ebee
2 изменённых файлов: 39 добавлений и 1 удалений

Просмотреть файл

@ -167,11 +167,12 @@ this.commands = class extends ExtensionAPI {
// The modifiers are the remaining elements.
keyElement.setAttribute("modifiers", this.getModifiersAttribute(parts));
if (/^[A-Z0-9]$/.test(chromeKey)) {
if (/^[A-Z]$/.test(chromeKey)) {
// We use the key attribute for all single digits and characters.
keyElement.setAttribute("key", chromeKey);
} else {
keyElement.setAttribute("keycode", this.getKeycodeAttribute(chromeKey));
keyElement.setAttribute("event", "keydown");
}
return keyElement;
@ -191,6 +192,9 @@ this.commands = class extends ExtensionAPI {
* @returns {string} The constructed value for the Key's 'keycode' attribute.
*/
getKeycodeAttribute(chromeKey) {
if (/[0-9]/.test(chromeKey)) {
return `VK_${chromeKey}`;
}
return `VK${chromeKey.replace(/([A-Z])/g, "_$&").toUpperCase()}`;
}

Просмотреть файл

@ -70,6 +70,15 @@ add_task(function* test_user_defined_commands() {
shiftKey: true,
},
},
{
name: "toggle-ctrl-shift-1",
shortcut: "Ctrl+Shift+1",
key: "1",
modifiers: {
accelKey: true,
shiftKey: true,
},
},
// Alt+Shift Shortcuts
{
name: "toggle-alt-shift-1",
@ -137,6 +146,31 @@ add_task(function* test_user_defined_commands() {
shiftKey: true,
},
},
{
name: "toggle-ctrl-space",
shortcut: "Ctrl+Space",
key: "VK_SPACE",
modifiers: {
accelKey: true,
},
},
{
name: "toggle-ctrl-comma",
shortcut: "Ctrl+Comma",
key: "VK_COMMA",
modifiers: {
accelKey: true,
},
},
{
name: "toggle-ctrl-period",
shortcut: "Ctrl+Period",
key: "VK_PERIOD",
modifiers: {
accelKey: true,
},
},
];
// Create a window before the extension is loaded.