Unverified Commit 5d372062 authored by Tong Mu's avatar Tong Mu Committed by GitHub

[RawKeyboard, Web, macOS] Upper keys should generate lower logical keys (#94827)

parent f47ca955
...@@ -10,6 +10,16 @@ import 'keyboard_key.dart'; ...@@ -10,6 +10,16 @@ import 'keyboard_key.dart';
import 'keyboard_maps.dart'; import 'keyboard_maps.dart';
import 'raw_keyboard.dart'; import 'raw_keyboard.dart';
/// Convert a UTF32 rune to its lower case.
int runeToLowerCase(int rune) {
// Assume only Basic Multilingual Plane runes have lower and upper cases.
// For other characters, return them as is.
const int utf16BmpUpperBound = 0xD7FF;
if (rune > utf16BmpUpperBound)
return rune;
return String.fromCharCode(rune).toLowerCase().codeUnitAt(0);
}
/// Platform-specific key event data for macOS. /// Platform-specific key event data for macOS.
/// ///
/// This object contains information about key events obtained from macOS's /// This object contains information about key events obtained from macOS's
...@@ -99,7 +109,7 @@ class RawKeyEventDataMacOs extends RawKeyEventData { ...@@ -99,7 +109,7 @@ class RawKeyEventDataMacOs extends RawKeyEventData {
// only tests BMP, it is fine to test keyLabel instead. // only tests BMP, it is fine to test keyLabel instead.
!LogicalKeyboardKey.isControlCharacter(keyLabel) && !LogicalKeyboardKey.isControlCharacter(keyLabel) &&
!_isUnprintableKey(keyLabel)) { !_isUnprintableKey(keyLabel)) {
character = codePoints[0]; character = runeToLowerCase(codePoints[0]);
} }
} }
if (character != null) { if (character != null) {
......
...@@ -104,7 +104,7 @@ class RawKeyEventDataWeb extends RawKeyEventData { ...@@ -104,7 +104,7 @@ class RawKeyEventDataWeb extends RawKeyEventData {
final bool isPrintable = key.length == 1; final bool isPrintable = key.length == 1;
if (isPrintable) if (isPrintable)
return LogicalKeyboardKey(key.codeUnitAt(0)); return LogicalKeyboardKey(key.toLowerCase().codeUnitAt(0));
// This is a non-printable key that we don't know about, so we mint a new // This is a non-printable key that we don't know about, so we mint a new
// key from `code`. Don't mint with `key`, because the `key` will always be // key from `code`. Don't mint with `key`, because the `key` will always be
......
...@@ -1415,14 +1415,13 @@ void main() { ...@@ -1415,14 +1415,13 @@ void main() {
} }
}); });
test('Printable keyboard keys are correctly translated', () { test('Lower letter keys are correctly translated', () {
const String unmodifiedCharacter = 'a';
final RawKeyEvent keyAEvent = RawKeyEvent.fromMessage(const <String, dynamic>{ final RawKeyEvent keyAEvent = RawKeyEvent.fromMessage(const <String, dynamic>{
'type': 'keydown', 'type': 'keydown',
'keymap': 'macos', 'keymap': 'macos',
'keyCode': 0x00000000, 'keyCode': 0x00000000,
'characters': 'a', 'characters': 'a',
'charactersIgnoringModifiers': unmodifiedCharacter, 'charactersIgnoringModifiers': 'a',
'modifiers': 0x0, 'modifiers': 0x0,
}); });
final RawKeyEventDataMacOs data = keyAEvent.data as RawKeyEventDataMacOs; final RawKeyEventDataMacOs data = keyAEvent.data as RawKeyEventDataMacOs;
...@@ -1431,6 +1430,21 @@ void main() { ...@@ -1431,6 +1430,21 @@ void main() {
expect(data.keyLabel, equals('a')); expect(data.keyLabel, equals('a'));
}); });
test('Upper letter keys are correctly translated', () {
final RawKeyEvent keyAEvent = RawKeyEvent.fromMessage(const <String, dynamic>{
'type': 'keydown',
'keymap': 'macos',
'keyCode': 0x00000000,
'characters': 'A',
'charactersIgnoringModifiers': 'A',
'modifiers': 0x20002,
});
final RawKeyEventDataMacOs data = keyAEvent.data as RawKeyEventDataMacOs;
expect(data.physicalKey, equals(PhysicalKeyboardKey.keyA));
expect(data.logicalKey, equals(LogicalKeyboardKey.keyA));
expect(data.keyLabel, equals('A'));
});
test('Control keyboard keys are correctly translated', () { test('Control keyboard keys are correctly translated', () {
final RawKeyEvent escapeKeyEvent = RawKeyEvent.fromMessage(const <String, dynamic>{ final RawKeyEvent escapeKeyEvent = RawKeyEvent.fromMessage(const <String, dynamic>{
'type': 'keydown', 'type': 'keydown',
...@@ -2482,7 +2496,7 @@ void main() { ...@@ -2482,7 +2496,7 @@ void main() {
} }
}); });
test('Printable keyboard keys are correctly translated', () { test('Lower letter keys are correctly translated', () {
final RawKeyEvent keyAEvent = RawKeyEvent.fromMessage(const <String, Object?>{ final RawKeyEvent keyAEvent = RawKeyEvent.fromMessage(const <String, Object?>{
'type': 'keydown', 'type': 'keydown',
'keymap': 'web', 'keymap': 'web',
...@@ -2497,6 +2511,21 @@ void main() { ...@@ -2497,6 +2511,21 @@ void main() {
expect(data.keyLabel, equals('a')); expect(data.keyLabel, equals('a'));
}); });
test('Upper letter keys are correctly translated', () {
final RawKeyEvent keyAEvent = RawKeyEvent.fromMessage(const <String, Object?>{
'type': 'keydown',
'keymap': 'web',
'code': 'KeyA',
'key': 'A',
'location': 0,
'metaState': 0x1, // Shift
});
final RawKeyEventDataWeb data = keyAEvent.data as RawKeyEventDataWeb;
expect(data.physicalKey, equals(PhysicalKeyboardKey.keyA));
expect(data.logicalKey, equals(LogicalKeyboardKey.keyA));
expect(data.keyLabel, equals('A'));
});
test('Control keyboard keys are correctly translated', () { test('Control keyboard keys are correctly translated', () {
final RawKeyEvent escapeKeyEvent = RawKeyEvent.fromMessage(const <String, Object?>{ final RawKeyEvent escapeKeyEvent = RawKeyEvent.fromMessage(const <String, Object?>{
'type': 'keydown', 'type': 'keydown',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment