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 draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Mon, 27 Feb 2017 18:04:30 +0900
changeset 490458 83a01842bb17073ec6ad46535b638c04dfdd8610
parent 490457 d5f662dff0bddbfdee76254ee3b236b385db18d3
child 490647 11ab0b1af8ae7fb94982b9245cd3b9be44f198ea
child 490648 260e20b4410358c95f16e2a5e3a82c1e89f27008
push id47099
push usermasayuki@d-toybox.com
push dateTue, 28 Feb 2017 13:27:03 +0000
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