Fixed: CPTextView keyboard issue in recent Firefox versions (#2874)

Wrong characters were sometimes emitted in keyboard events on Firefox
This commit is contained in:
daboe01
2020-01-07 12:34:39 +01:00
committed by Martin Carlberg
parent 109003ae12
commit 8daa53dd3e
2 changed files with 15 additions and 3 deletions
+8 -2
View File
@@ -738,8 +738,14 @@ _CPPlatformWindowWillCloseNotification = @"_CPPlatformWindowWillCloseNotificatio
if (aDOMEvent.which === 0 || aDOMEvent.charCode === 0 || (aDOMEvent.which === undefined && aDOMEvent.charCode === undefined))
characters = KeyCodesToUnicodeMap[_keyCode];
// The problem with keyCode is that this property refers to keys on the keyboard and not to characters
// This is why String.fromCharCode does not always work in more recent versions of Firefox
// E.g. pressing a '#' on a German keyboard gives you a charCode of 163, which refers to '£' and not '#'
// The property key works fine, though. From there we can get the actual character more robustly.
// Therefore we prefer key over keyCode whenever possible
if (!characters)
characters = String.fromCharCode(_keyCode).toLowerCase();
characters = (aDOMEvent.key && aDOMEvent.key.length == 1) ? aDOMEvent.key.toLowerCase() : String.fromCharCode(_keyCode).toLowerCase();
overrideCharacters = (modifierFlags & CPShiftKeyMask || _capsLockActive) ? characters.toUpperCase() : characters;
@@ -767,7 +773,7 @@ _CPPlatformWindowWillCloseNotification = @"_CPPlatformWindowWillCloseNotificatio
//this lets us be consistent in all browsers and send on the keydown
//which means we can cancel the event early enough, but only if sendEvent needs to
}
else if (CPKeyCodes.firesKeyPressEvent(_keyCode, _lastKey, aDOMEvent.shiftKey, aDOMEvent.ctrlKey, aDOMEvent.altKey))
else if (CPKeyCodes.firesKeyPressEvent(_keyCode, aDOMEvent.key, _lastKey, aDOMEvent.shiftKey, aDOMEvent.ctrlKey, aDOMEvent.altKey))
{
// this branch is taken by events which fire keydown, keypress, and keyup.
// this is the only time we'll ALLOW character keys to propagate (needed for text fields)
@@ -157,8 +157,14 @@ CPKeyCodes = {
* @param opt_altKey Whether the alt key is held down.
* @return Returns YES if it's a key that fires a keypress event.
*/
CPKeyCodes.firesKeyPressEvent = function(keyCode, opt_heldKeyCode, opt_shiftKey, opt_ctrlKey, opt_altKey)
CPKeyCodes.firesKeyPressEvent = function(keyCode, key, opt_heldKeyCode, opt_shiftKey, opt_ctrlKey, opt_altKey)
{
// The property key from event is one character wide in case of 'regular' keys (as opposed e.g. to arrow keys)
// Regular keys all fire the keypress event
if (key && key.length == 1)
return true;
if (!CPFeatureIsCompatible(CPJavaScriptRemedialKeySupport))
return true;