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
--- 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