Bug 1268450 - Accept loose digits for azerty keyboards. r=jryans
☠☠ backed out by c62705a833ff ☠ ☠
authorAlexandre Poirot <poirot.alex@gmail.com>
Thu, 19 May 2016 16:13:00 -0400
changeset 369149 9d79d99a3e18ce2f28ce2eae1b310c59871e0ed3
parent 369148 9090f1b3c2e45034380e46169b5e6152cf138ee3
child 369150 466d5c96f43964d4122959e5ab19131237bdc6fa
push id18759
push userbmo:zer0@mozilla.com
push dateFri, 20 May 2016 14:22:18 +0000
reviewersjryans
bugs1268450
milestone49.0a1
Bug 1268450 - Accept loose digits for azerty keyboards. r=jryans
devtools/client/shared/key-shortcuts.js
devtools/client/shared/test/browser_key_shortcuts.js
--- a/devtools/client/shared/key-shortcuts.js
+++ b/devtools/client/shared/key-shortcuts.js
@@ -182,17 +182,22 @@ KeyShortcuts.prototype = {
     // expected key is a special character accessible via shift.
     if (shortcut.shift != event.shiftKey && event.key &&
         event.key.match(/[a-zA-Z]/)) {
       return false;
     }
     if (shortcut.keyCode) {
       return event.keyCode == shortcut.keyCode;
     }
-    return event.key.toLowerCase() == shortcut.key;
+    // For character keys, we match if the final character is the expected one.
+    // But for digits we also accept indirect match to please azerty keyboard,
+    // which requires Shift to be pressed to get digits.
+    return event.key.toLowerCase() == shortcut.key ||
+      ( shortcut.key.match(/[0-9]/) &&
+        event.keyCode == shortcut.key.charCodeAt(0) );
   },
 
   handleEvent(event) {
     for (let [key, shortcut] of this.keys) {
       if (this.doesEventMatchShortcut(event, shortcut)) {
         this.eventEmitter.emit(key, event);
       }
     }
--- a/devtools/client/shared/test/browser_key_shortcuts.js
+++ b/devtools/client/shared/test/browser_key_shortcuts.js
@@ -5,16 +5,17 @@ var isOSX = Services.appinfo.OS === "Dar
 
 add_task(function* () {
   let shortcuts = new KeyShortcuts({
     window
   });
   yield testSimple(shortcuts);
   yield testNonLetterCharacter(shortcuts);
   yield testMixup(shortcuts);
+  yield testLooseDigits(shortcuts);
   yield testExactModifiers(shortcuts);
   yield testLooseShiftModifier(shortcuts);
   yield testStrictLetterShiftModifier(shortcuts);
   yield testAltModifier(shortcuts);
   yield testCommandOrControlModifier(shortcuts);
   yield testCtrlModifier(shortcuts);
   shortcuts.destroy();
 });
@@ -90,16 +91,50 @@ function testMixup(shortcuts) {
   ok(!hitSecond, "No mixup, second shortcut is still not notified (2/2)");
 
   // Finally dispatch the second shortcut
   EventUtils.synthesizeKey("a", { altKey: true }, window);
   yield onSecondKey;
   ok(hitSecond, "Got the second shortcut notified once it is actually fired");
 }
 
+// On azerty keyboard, digits are only available by pressing Shift/Capslock,
+// but we accept them even if we omit doing that.
+function testLooseDigits(shortcuts) {
+  info("Test Loose digits");
+  let onKey = once(shortcuts, "0", (key, event) => {
+    is(event.key, "à");
+    ok(!event.altKey);
+    ok(!event.ctrlKey);
+    ok(!event.metaKey);
+    ok(!event.shiftKey);
+  });
+  // Simulate a press on the "0" key, without shift pressed on a french
+  // keyboard
+  EventUtils.synthesizeKey(
+    "à",
+    { keyCode: 48 },
+    window);
+  yield onKey;
+
+  onKey = once(shortcuts, "0", (key, event) => {
+    is(event.key, "0");
+    ok(!event.altKey);
+    ok(!event.ctrlKey);
+    ok(!event.metaKey);
+    ok(event.shiftKey);
+  });
+  // Simulate the same press with shift pressed
+  EventUtils.synthesizeKey(
+    "0",
+    { keyCode: 48, shiftKey: true },
+    window);
+  yield onKey;
+}
+
 // Test that shortcuts is notified only when the modifiers match exactly
 function testExactModifiers(shortcuts) {
   info("Test exact modifiers match");
 
   let hit = false;
   let onKey = once(shortcuts, "Alt+A", (key, event) => {
     is(event.key, "a");
     ok(event.altKey);