Bug 1263302 Swap kVK_ISO_Section and kVK_ANSI_Grave key code values of ISO keyboard at computing KeyboardEvent.code value because macOS sends them as swapped r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Mon, 27 Feb 2017 18:04:30 +0900
changeset 374232 51b368d536a3b1ae2985dcd0bc8414cd2d2cec66
parent 374231 a79c630604eb88f8c98087cbead0d02008035be7
child 374233 c6a4bbb143946b614aa02da0a88bf1c80ea9918e
push id10863
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 23:02:23 +0000
treeherdermozilla-aurora@0931190cd725 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1263302
milestone54.0a1
Bug 1263302 Swap kVK_ISO_Section and kVK_ANSI_Grave key code values of ISO keyboard at computing KeyboardEvent.code value because macOS sends them as swapped r=m_kato macOS oddly sends kVK_ISO_Section instead of kVK_ANSI_Grave when user types left key of Key1 only when the connected keyboard is ISO keyboard. On the other hand, macOS sends kVK_ANSI_Grave instead of kVK_ISO_Section when user types left key of KeyZ only when the connected keyboard is ISO keyboard. So, macOS swapps their key code values only when ISO keyboard is connected. So, we should treat them as swapped when we compute KeyboardEvent.code value since Chromium treates them as swapped only when computing KeyboardEvent.code value too. MozReview-Commit-ID: BYeFedydyR5
widget/cocoa/TextInputHandler.h
widget/cocoa/TextInputHandler.mm
--- a/widget/cocoa/TextInputHandler.h
+++ b/widget/cocoa/TextInputHandler.h
@@ -272,18 +272,21 @@ public:
    * @param aNativeKeyCode        A native keycode.
    */
   static KeyNameIndex ComputeGeckoKeyNameIndex(UInt32 aNativeKeyCode);
 
   /**
    * ComputeGeckoCodeNameIndex() returns Gecko code name index for the key.
    *
    * @param aNativeKeyCode        A native keycode.
+   * @param aKbType               A native Keyboard Type value.  Typically,
+   *                              this is a result of ::LMGetKbdType().
    */
-  static CodeNameIndex ComputeGeckoCodeNameIndex(UInt32 aNativeKeyCode);
+  static CodeNameIndex ComputeGeckoCodeNameIndex(UInt32 aNativeKeyCode,
+                                                 UInt32 aKbType);
 
 protected:
   /**
    * TranslateToString() computes the inputted text from the native keyCode,
    * modifier flags and keyboard type.
    *
    * @param aKeyCode              A native keyCode.
    * @param aModifiers            Combination of native modifier flags.
--- a/widget/cocoa/TextInputHandler.mm
+++ b/widget/cocoa/TextInputHandler.mm
@@ -1025,17 +1025,17 @@ TISInputSourceWrapper::InitKeyEvent(NSEv
         aKeyEvent.mKeyValue.Truncate();
       }
     }
   } else {
     // Compute the key for non-printable keys and some special printable keys.
     aKeyEvent.mKeyNameIndex = ComputeGeckoKeyNameIndex(nativeKeyCode);
   }
 
-  aKeyEvent.mCodeNameIndex = ComputeGeckoCodeNameIndex(nativeKeyCode);
+  aKeyEvent.mCodeNameIndex = ComputeGeckoCodeNameIndex(nativeKeyCode, kbType);
   MOZ_ASSERT(aKeyEvent.mCodeNameIndex != CODE_NAME_INDEX_USE_STRING);
 
   NS_OBJC_END_TRY_ABORT_BLOCK
 }
 
 void
 TISInputSourceWrapper::WillDispatchKeyboardEvent(
                          NSEvent* aNativeKeyEvent,
@@ -1412,18 +1412,31 @@ TISInputSourceWrapper::ComputeGeckoKeyNa
 
     default:
       return KEY_NAME_INDEX_Unidentified;
   }
 }
 
 // static
 CodeNameIndex
-TISInputSourceWrapper::ComputeGeckoCodeNameIndex(UInt32 aNativeKeyCode)
+TISInputSourceWrapper::ComputeGeckoCodeNameIndex(UInt32 aNativeKeyCode,
+                                                 UInt32 aKbType)
 {
+  // macOS swaps native key code between Backquote key and IntlBackslash key
+  // only when the keyboard type is ISO.  Let's treat the key code after
+  // swapping them here because Chromium does so only when computing .code
+  // value.
+  if (::KBGetLayoutType(aKbType) == kKeyboardISO) {
+    if (aNativeKeyCode == kVK_ISO_Section) {
+      aNativeKeyCode = kVK_ANSI_Grave;
+    } else if (aNativeKeyCode == kVK_ANSI_Grave) {
+      aNativeKeyCode = kVK_ISO_Section;
+    }
+  }
+
   switch (aNativeKeyCode) {
 
 #define NS_NATIVE_KEY_TO_DOM_CODE_NAME_INDEX(aNativeKey, aCodeNameIndex) \
     case aNativeKey: return aCodeNameIndex;
 
 #include "NativeKeyToDOMCodeName.h"
 
 #undef NS_NATIVE_KEY_TO_DOM_CODE_NAME_INDEX