Generate RawKeyEvents for iOS 13.4+ (#65193)

* Added RawKeyEvent support for iOS

* Removed unused remnant

* added some missing keys

* Removed trailing whitespaces

* commit for build

* Added mapping names

* Made iOS keycodes generatable and collectable

* Fixed naming and formatting issues

* fixed raw_keyboard_test
This commit is contained in:
puelo
2020-10-22 22:33:51 +02:00
committed by GitHub
parent f877266139
commit 24d3999783
15 changed files with 1546 additions and 283 deletions

View File

@@ -11,6 +11,7 @@ import 'package:path/path.dart' as path;
import 'package:gen_keycodes/android_code_gen.dart';
import 'package:gen_keycodes/base_code_gen.dart';
import 'package:gen_keycodes/ios_code_gen.dart';
import 'package:gen_keycodes/macos_code_gen.dart';
import 'package:gen_keycodes/fuchsia_code_gen.dart';
import 'package:gen_keycodes/glfw_code_gen.dart';
@@ -252,7 +253,7 @@ Future<void> main(List<String> rawArguments) async {
print('Writing ${'key maps'.padRight(15)}${mapsFile.absolute}');
await mapsFile.writeAsString(KeyboardMapsCodeGenerator(data).generate());
for (final String platform in <String>['android', 'darwin', 'glfw', 'fuchsia', 'linux', 'windows', 'web']) {
for (final String platform in <String>['android', 'macos', 'ios', 'glfw', 'fuchsia', 'linux', 'windows', 'web']) {
PlatformCodeGenerator codeGenerator;
switch (platform) {
case 'glfw':
@@ -264,9 +265,12 @@ Future<void> main(List<String> rawArguments) async {
case 'android':
codeGenerator = AndroidCodeGenerator(data);
break;
case 'darwin':
case 'macos':
codeGenerator = MacOsCodeGenerator(data);
break;
case 'ios':
codeGenerator = IosCodeGenerator(data);
break;
case 'windows':
codeGenerator = WindowsCodeGenerator(data);
break;

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,26 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <map>
// DO NOT EDIT -- DO NOT EDIT -- DO NOT EDIT
// This file is generated by flutter/flutter@dev/tools/gen_keycodes/bin/gen_keycodes.dart and
// should not be edited directly.
//
// Edit the template dev/tools/gen_keycodes/data/keyboard_map_ios_cc.tmpl instead.
// See dev/tools/gen_keycodes/README.md for more information.
// Maps macOS-specific key code values representing [PhysicalKeyboardKey].
//
// iOS doesn't provide a scan code, but a virtual keycode to represent a physical key.
const std::map<int, int> g_ios_to_physical_key = {
@@@IOS_SCAN_CODE_MAP@@@
};
// A map of iOS key codes which have printable representations, but appear
// on the number pad. Used to provide different key objects for keys like
// KEY_EQUALS and NUMPAD_EQUALS.
const std::map<int, int> g_ios_numpad_map = {
@@@IOS_NUMPAD_MAP@@@
};

View File

@@ -8,7 +8,7 @@
// This file is generated by flutter/flutter@dev/tools/gen_keycodes/bin/gen_keycodes.dart and
// should not be edited directly.
//
// Edit the template dev/tools/gen_keycodes/data/keyboard_map_darwin_cc.tmpl instead.
// Edit the template dev/tools/gen_keycodes/data/keyboard_map_macos_cc.tmpl instead.
// See dev/tools/gen_keycodes/README.md for more information.
// Maps macOS-specific key code values representing [PhysicalKeyboardKey].

View File

@@ -59,6 +59,20 @@ const Map<int, LogicalKeyboardKey> kMacOsFunctionKeyMap = <int, LogicalKeyboardK
@@@MACOS_FUNCTION_KEY_MAP@@@
};
/// Maps iOS-specific key code values representing [PhysicalKeyboardKey].
///
/// iOS doesn't provide a scan code, but a virtual keycode to represent a physical key.
const Map<int, PhysicalKeyboardKey> kIosToPhysicalKey = <int, PhysicalKeyboardKey>{
@@@IOS_SCAN_CODE_MAP@@@
};
/// A map of iOS key codes which have printable representations, but appear
/// on the number pad. Used to provide different key objects for keys like
/// KEY_EQUALS and NUMPAD_EQUALS.
const Map<int, LogicalKeyboardKey> kIosNumPadMap = <int, LogicalKeyboardKey>{
@@@IOS_NUMPAD_MAP@@@
};
/// Maps GLFW-specific key codes to the matching [LogicalKeyboardKey].
const Map<int, LogicalKeyboardKey> kGlfwToLogicalKey = <int, LogicalKeyboardKey>{
@@@GLFW_KEY_CODE_MAP@@@

View File

@@ -0,0 +1,54 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:path/path.dart' as path;
import 'base_code_gen.dart';
import 'key_data.dart';
import 'utils.dart';
/// Generates the key mapping of iOS, based on the information in the key
/// data structure given to it.
class IosCodeGenerator extends PlatformCodeGenerator {
IosCodeGenerator(KeyData keyData) : super(keyData);
/// This generates the map of iOS key codes to physical keys.
String get _iosScanCodeMap {
final StringBuffer iosScanCodeMap = StringBuffer();
for (final Key entry in keyData.data) {
if (entry.iosScanCode != null) {
iosScanCodeMap.writeln(' { ${toHex(entry.iosScanCode)}, ${toHex(entry.usbHidCode)} }, // ${entry.constantName}');
}
}
return iosScanCodeMap.toString().trimRight();
}
/// This generates the map of iOS number pad key codes to logical keys.
String get _iosNumpadMap {
final StringBuffer iosNumPadMap = StringBuffer();
for (final Key entry in numpadKeyData) {
if (entry.iosScanCode != null) {
iosNumPadMap.writeln(' { ${toHex(entry.iosScanCode)}, ${toHex(entry.flutterId, digits: 10)} }, // ${entry.constantName}');
}
}
return iosNumPadMap.toString().trimRight();
}
@override
String get templatePath => path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'keyboard_map_ios_cc.tmpl');
@override
String outputPath(String platform) => path.joinAll(<String>[flutterRoot.path, '..', 'engine', 'src', 'flutter', 'shell', 'platform', 'darwin', platform, 'keycodes', 'keyboard_map_$platform.h']);
@override
Map<String, String> mappings() {
// There is no iOS keycode map since iOS uses keycode to represent a physical key.
// The LogicalKeyboardKey is generated by raw_keyboard_macos.dart from the unmodified characters
// from NSEvent.
return <String, String>{
'IOS_SCAN_CODE_MAP': _iosScanCodeMap,
'IOS_NUMPAD_MAP': _iosNumpadMap
};
}
}

View File

@@ -325,16 +325,18 @@ class KeyData {
input = input.replaceAll(commentRegExp, '');
input.replaceAllMapped(usbMapRegExp, (Match match) {
if (match != null) {
final int usbHidCode = getHex(match.group(1));
final int macScanCode = getHex(match.group(5));
final int linuxScanCode = getHex(match.group(2));
final int xKbScanCode = getHex(match.group(3));
final int windowsScanCode = getHex(match.group(4));
final Key newEntry = Key(
usbHidCode: getHex(match.group(1)),
usbHidCode: usbHidCode,
linuxScanCode: linuxScanCode == 0 ? null : linuxScanCode,
xKbScanCode: xKbScanCode == 0 ? null : xKbScanCode,
windowsScanCode: windowsScanCode == 0 ? null : windowsScanCode,
macOsScanCode: macScanCode == 0xffff ? null : macScanCode,
iosScanCode: (usbHidCode & 0x070000) == 0x070000 ? (usbHidCode ^ 0x070000) : null,
name: match.group(6) == 'NULL' ? null : match.group(6),
// The input data has a typo...
chromiumName: shoutingToLowerCamel(match.group(7)).replaceAll('Minimium', 'Minimum'),
@@ -375,6 +377,7 @@ class Key {
this.windowsKeyNames,
this.windowsKeyCodes,
this.macOsScanCode,
this.iosScanCode,
@required this.chromiumName,
this.androidKeyNames,
this.androidScanCodes,
@@ -403,6 +406,7 @@ class Key {
windowsKeyCodes: (map['keyCodes']['windows'] as List<dynamic>)?.cast<int>(),
windowsKeyNames: (map['names']['windows'] as List<dynamic>)?.cast<String>(),
macOsScanCode: map['scanCodes']['macos'] as int,
iosScanCode: map['scanCodes']['ios'] as int,
glfwKeyNames: (map['names']['glfw'] as List<dynamic>)?.cast<String>(),
glfwKeyCodes: (map['keyCodes']['glfw'] as List<dynamic>)?.cast<int>(),
gtkKeyNames: (map['names']['gtk'] as List<dynamic>)?.cast<String>(),
@@ -428,6 +432,8 @@ class Key {
List<String> windowsKeyNames;
/// The macOS scan code of the key from Chromium's header file.
int macOsScanCode;
/// The iOS scan code of the key from UIKey's documentation (USB Hid table)
int iosScanCode;
/// The name of the key, mostly derived from the DomKey name in Chromium,
/// but where there was no DomKey representation, derived from the Chromium
/// symbol name.
@@ -483,6 +489,7 @@ class Key {
'xkb': xKbScanCode,
'windows': windowsScanCode,
'macos': macOsScanCode,
'ios': iosScanCode,
},
'keyCodes': <String, List<int>>{
'android': androidKeyCodes,
@@ -550,7 +557,8 @@ class Key {
return """'$constantName': (name: "$name", usbHidCode: ${toHex(usbHidCode)}, """
'linuxScanCode: ${toHex(linuxScanCode)}, xKbScanCode: ${toHex(xKbScanCode)}, '
'windowsKeyCode: ${toHex(windowsScanCode)}, macOsScanCode: ${toHex(macOsScanCode)}, '
'windowsScanCode: ${toHex(windowsScanCode)}, chromiumSymbolName: $chromiumName';
'windowsScanCode: ${toHex(windowsScanCode)}, chromiumSymbolName: $chromiumName '
'iOSScanCode: ${toHex(iosScanCode)})';
}
/// Returns the static map of printable representations.

View File

@@ -198,6 +198,28 @@ class KeyboardMapsCodeGenerator extends BaseCodeGenerator {
return macOsFunctionKeyMap.toString().trimRight();
}
/// This generates the map of iOS key codes to physical keys.
String get iosScanCodeMap {
final StringBuffer iosScanCodeMap = StringBuffer();
for (final Key entry in keyData.data) {
if (entry.iosScanCode != null) {
iosScanCodeMap.writeln(' ${toHex(entry.iosScanCode)}: PhysicalKeyboardKey.${entry.constantName},');
}
}
return iosScanCodeMap.toString().trimRight();
}
/// This generates the map of iOS number pad key codes to logical keys.
String get iosNumpadMap {
final StringBuffer iosNumPadMap = StringBuffer();
for (final Key entry in numpadKeyData) {
if (entry.iosScanCode != null) {
iosNumPadMap.writeln(' ${toHex(entry.iosScanCode)}: LogicalKeyboardKey.${entry.constantName},');
}
}
return iosNumPadMap.toString().trimRight();
}
/// This generates the map of Fuchsia key codes to logical keys.
String get fuchsiaKeyCodeMap {
final StringBuffer fuchsiaKeyCodeMap = StringBuffer();
@@ -267,6 +289,8 @@ class KeyboardMapsCodeGenerator extends BaseCodeGenerator {
'MACOS_SCAN_CODE_MAP': macOsScanCodeMap,
'MACOS_NUMPAD_MAP': macOsNumpadMap,
'MACOS_FUNCTION_KEY_MAP': macOsFunctionKeyMap,
'IOS_SCAN_CODE_MAP': iosScanCodeMap,
'IOS_NUMPAD_MAP': iosNumpadMap,
'GLFW_KEY_CODE_MAP': glfwKeyCodeMap,
'GLFW_NUMPAD_MAP': glfwNumpadMap,
'GTK_KEY_CODE_MAP': gtkKeyCodeMap,

View File

@@ -47,7 +47,10 @@ class MacOsCodeGenerator extends PlatformCodeGenerator {
}
@override
String get templatePath => path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'keyboard_map_darwin_cc.tmpl');
String get templatePath => path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'keyboard_map_macos_cc.tmpl');
@override
String outputPath(String platform) => path.joinAll(<String>[flutterRoot.path, '..', 'engine', 'src', 'flutter', 'shell', 'platform', 'darwin', platform, 'keycodes', 'keyboard_map_$platform.h']);
@override
Map<String, String> mappings() {

View File

@@ -27,6 +27,7 @@ export 'src/services/platform_views.dart';
export 'src/services/raw_keyboard.dart';
export 'src/services/raw_keyboard_android.dart';
export 'src/services/raw_keyboard_fuchsia.dart';
export 'src/services/raw_keyboard_ios.dart';
export 'src/services/raw_keyboard_linux.dart';
export 'src/services/raw_keyboard_macos.dart';
export 'src/services/raw_keyboard_web.dart';

View File

@@ -1178,6 +1178,196 @@ const Map<int, LogicalKeyboardKey> kMacOsFunctionKeyMap = <int, LogicalKeyboardK
0x0000005a: LogicalKeyboardKey.f20,
};
/// Maps iOS-specific key code values representing [PhysicalKeyboardKey].
///
/// iOS doesn't provide a scan code, but a virtual keycode to represent a physical key.
const Map<int, PhysicalKeyboardKey> kIosToPhysicalKey = <int, PhysicalKeyboardKey>{
0x00000000: PhysicalKeyboardKey.usbReserved,
0x00000001: PhysicalKeyboardKey.usbErrorRollOver,
0x00000002: PhysicalKeyboardKey.usbPostFail,
0x00000003: PhysicalKeyboardKey.usbErrorUndefined,
0x00000004: PhysicalKeyboardKey.keyA,
0x00000005: PhysicalKeyboardKey.keyB,
0x00000006: PhysicalKeyboardKey.keyC,
0x00000007: PhysicalKeyboardKey.keyD,
0x00000008: PhysicalKeyboardKey.keyE,
0x00000009: PhysicalKeyboardKey.keyF,
0x0000000a: PhysicalKeyboardKey.keyG,
0x0000000b: PhysicalKeyboardKey.keyH,
0x0000000c: PhysicalKeyboardKey.keyI,
0x0000000d: PhysicalKeyboardKey.keyJ,
0x0000000e: PhysicalKeyboardKey.keyK,
0x0000000f: PhysicalKeyboardKey.keyL,
0x00000010: PhysicalKeyboardKey.keyM,
0x00000011: PhysicalKeyboardKey.keyN,
0x00000012: PhysicalKeyboardKey.keyO,
0x00000013: PhysicalKeyboardKey.keyP,
0x00000014: PhysicalKeyboardKey.keyQ,
0x00000015: PhysicalKeyboardKey.keyR,
0x00000016: PhysicalKeyboardKey.keyS,
0x00000017: PhysicalKeyboardKey.keyT,
0x00000018: PhysicalKeyboardKey.keyU,
0x00000019: PhysicalKeyboardKey.keyV,
0x0000001a: PhysicalKeyboardKey.keyW,
0x0000001b: PhysicalKeyboardKey.keyX,
0x0000001c: PhysicalKeyboardKey.keyY,
0x0000001d: PhysicalKeyboardKey.keyZ,
0x0000001e: PhysicalKeyboardKey.digit1,
0x0000001f: PhysicalKeyboardKey.digit2,
0x00000020: PhysicalKeyboardKey.digit3,
0x00000021: PhysicalKeyboardKey.digit4,
0x00000022: PhysicalKeyboardKey.digit5,
0x00000023: PhysicalKeyboardKey.digit6,
0x00000024: PhysicalKeyboardKey.digit7,
0x00000025: PhysicalKeyboardKey.digit8,
0x00000026: PhysicalKeyboardKey.digit9,
0x00000027: PhysicalKeyboardKey.digit0,
0x00000028: PhysicalKeyboardKey.enter,
0x00000029: PhysicalKeyboardKey.escape,
0x0000002a: PhysicalKeyboardKey.backspace,
0x0000002b: PhysicalKeyboardKey.tab,
0x0000002c: PhysicalKeyboardKey.space,
0x0000002d: PhysicalKeyboardKey.minus,
0x0000002e: PhysicalKeyboardKey.equal,
0x0000002f: PhysicalKeyboardKey.bracketLeft,
0x00000030: PhysicalKeyboardKey.bracketRight,
0x00000031: PhysicalKeyboardKey.backslash,
0x00000033: PhysicalKeyboardKey.semicolon,
0x00000034: PhysicalKeyboardKey.quote,
0x00000035: PhysicalKeyboardKey.backquote,
0x00000036: PhysicalKeyboardKey.comma,
0x00000037: PhysicalKeyboardKey.period,
0x00000038: PhysicalKeyboardKey.slash,
0x00000039: PhysicalKeyboardKey.capsLock,
0x0000003a: PhysicalKeyboardKey.f1,
0x0000003b: PhysicalKeyboardKey.f2,
0x0000003c: PhysicalKeyboardKey.f3,
0x0000003d: PhysicalKeyboardKey.f4,
0x0000003e: PhysicalKeyboardKey.f5,
0x0000003f: PhysicalKeyboardKey.f6,
0x00000040: PhysicalKeyboardKey.f7,
0x00000041: PhysicalKeyboardKey.f8,
0x00000042: PhysicalKeyboardKey.f9,
0x00000043: PhysicalKeyboardKey.f10,
0x00000044: PhysicalKeyboardKey.f11,
0x00000045: PhysicalKeyboardKey.f12,
0x00000046: PhysicalKeyboardKey.printScreen,
0x00000047: PhysicalKeyboardKey.scrollLock,
0x00000048: PhysicalKeyboardKey.pause,
0x00000049: PhysicalKeyboardKey.insert,
0x0000004a: PhysicalKeyboardKey.home,
0x0000004b: PhysicalKeyboardKey.pageUp,
0x0000004c: PhysicalKeyboardKey.delete,
0x0000004d: PhysicalKeyboardKey.end,
0x0000004e: PhysicalKeyboardKey.pageDown,
0x0000004f: PhysicalKeyboardKey.arrowRight,
0x00000050: PhysicalKeyboardKey.arrowLeft,
0x00000051: PhysicalKeyboardKey.arrowDown,
0x00000052: PhysicalKeyboardKey.arrowUp,
0x00000053: PhysicalKeyboardKey.numLock,
0x00000054: PhysicalKeyboardKey.numpadDivide,
0x00000055: PhysicalKeyboardKey.numpadMultiply,
0x00000056: PhysicalKeyboardKey.numpadSubtract,
0x00000057: PhysicalKeyboardKey.numpadAdd,
0x00000058: PhysicalKeyboardKey.numpadEnter,
0x00000059: PhysicalKeyboardKey.numpad1,
0x0000005a: PhysicalKeyboardKey.numpad2,
0x0000005b: PhysicalKeyboardKey.numpad3,
0x0000005c: PhysicalKeyboardKey.numpad4,
0x0000005d: PhysicalKeyboardKey.numpad5,
0x0000005e: PhysicalKeyboardKey.numpad6,
0x0000005f: PhysicalKeyboardKey.numpad7,
0x00000060: PhysicalKeyboardKey.numpad8,
0x00000061: PhysicalKeyboardKey.numpad9,
0x00000062: PhysicalKeyboardKey.numpad0,
0x00000063: PhysicalKeyboardKey.numpadDecimal,
0x00000064: PhysicalKeyboardKey.intlBackslash,
0x00000065: PhysicalKeyboardKey.contextMenu,
0x00000066: PhysicalKeyboardKey.power,
0x00000067: PhysicalKeyboardKey.numpadEqual,
0x00000068: PhysicalKeyboardKey.f13,
0x00000069: PhysicalKeyboardKey.f14,
0x0000006a: PhysicalKeyboardKey.f15,
0x0000006b: PhysicalKeyboardKey.f16,
0x0000006c: PhysicalKeyboardKey.f17,
0x0000006d: PhysicalKeyboardKey.f18,
0x0000006e: PhysicalKeyboardKey.f19,
0x0000006f: PhysicalKeyboardKey.f20,
0x00000070: PhysicalKeyboardKey.f21,
0x00000071: PhysicalKeyboardKey.f22,
0x00000072: PhysicalKeyboardKey.f23,
0x00000073: PhysicalKeyboardKey.f24,
0x00000074: PhysicalKeyboardKey.open,
0x00000075: PhysicalKeyboardKey.help,
0x00000077: PhysicalKeyboardKey.select,
0x00000079: PhysicalKeyboardKey.again,
0x0000007a: PhysicalKeyboardKey.undo,
0x0000007b: PhysicalKeyboardKey.cut,
0x0000007c: PhysicalKeyboardKey.copy,
0x0000007d: PhysicalKeyboardKey.paste,
0x0000007e: PhysicalKeyboardKey.find,
0x0000007f: PhysicalKeyboardKey.audioVolumeMute,
0x00000080: PhysicalKeyboardKey.audioVolumeUp,
0x00000081: PhysicalKeyboardKey.audioVolumeDown,
0x00000085: PhysicalKeyboardKey.numpadComma,
0x00000087: PhysicalKeyboardKey.intlRo,
0x00000088: PhysicalKeyboardKey.kanaMode,
0x00000089: PhysicalKeyboardKey.intlYen,
0x0000008a: PhysicalKeyboardKey.convert,
0x0000008b: PhysicalKeyboardKey.nonConvert,
0x00000090: PhysicalKeyboardKey.lang1,
0x00000091: PhysicalKeyboardKey.lang2,
0x00000092: PhysicalKeyboardKey.lang3,
0x00000093: PhysicalKeyboardKey.lang4,
0x00000094: PhysicalKeyboardKey.lang5,
0x0000009b: PhysicalKeyboardKey.abort,
0x000000a3: PhysicalKeyboardKey.props,
0x000000b6: PhysicalKeyboardKey.numpadParenLeft,
0x000000b7: PhysicalKeyboardKey.numpadParenRight,
0x000000bb: PhysicalKeyboardKey.numpadBackspace,
0x000000d0: PhysicalKeyboardKey.numpadMemoryStore,
0x000000d1: PhysicalKeyboardKey.numpadMemoryRecall,
0x000000d2: PhysicalKeyboardKey.numpadMemoryClear,
0x000000d3: PhysicalKeyboardKey.numpadMemoryAdd,
0x000000d4: PhysicalKeyboardKey.numpadMemorySubtract,
0x000000d7: PhysicalKeyboardKey.numpadSignChange,
0x000000d8: PhysicalKeyboardKey.numpadClear,
0x000000d9: PhysicalKeyboardKey.numpadClearEntry,
0x000000e0: PhysicalKeyboardKey.controlLeft,
0x000000e1: PhysicalKeyboardKey.shiftLeft,
0x000000e2: PhysicalKeyboardKey.altLeft,
0x000000e3: PhysicalKeyboardKey.metaLeft,
0x000000e4: PhysicalKeyboardKey.controlRight,
0x000000e5: PhysicalKeyboardKey.shiftRight,
0x000000e6: PhysicalKeyboardKey.altRight,
0x000000e7: PhysicalKeyboardKey.metaRight,
};
/// A map of iOS key codes which have printable representations, but appear
/// on the number pad. Used to provide different key objects for keys like
/// KEY_EQUALS and NUMPAD_EQUALS.
const Map<int, LogicalKeyboardKey> kIosNumPadMap = <int, LogicalKeyboardKey>{
0x00000054: LogicalKeyboardKey.numpadDivide,
0x00000055: LogicalKeyboardKey.numpadMultiply,
0x00000056: LogicalKeyboardKey.numpadSubtract,
0x00000057: LogicalKeyboardKey.numpadAdd,
0x00000059: LogicalKeyboardKey.numpad1,
0x0000005a: LogicalKeyboardKey.numpad2,
0x0000005b: LogicalKeyboardKey.numpad3,
0x0000005c: LogicalKeyboardKey.numpad4,
0x0000005d: LogicalKeyboardKey.numpad5,
0x0000005e: LogicalKeyboardKey.numpad6,
0x0000005f: LogicalKeyboardKey.numpad7,
0x00000060: LogicalKeyboardKey.numpad8,
0x00000061: LogicalKeyboardKey.numpad9,
0x00000062: LogicalKeyboardKey.numpad0,
0x00000063: LogicalKeyboardKey.numpadDecimal,
0x00000067: LogicalKeyboardKey.numpadEqual,
0x00000085: LogicalKeyboardKey.numpadComma,
0x000000b6: LogicalKeyboardKey.numpadParenLeft,
0x000000b7: LogicalKeyboardKey.numpadParenRight,
};
/// Maps GLFW-specific key codes to the matching [LogicalKeyboardKey].
const Map<int, LogicalKeyboardKey> kGlfwToLogicalKey = <int, LogicalKeyboardKey>{
65: LogicalKeyboardKey.keyA,

View File

@@ -10,6 +10,7 @@ import 'package:flutter/foundation.dart';
import 'keyboard_key.dart';
import 'raw_keyboard_android.dart';
import 'raw_keyboard_fuchsia.dart';
import 'raw_keyboard_ios.dart';
import 'raw_keyboard_linux.dart';
import 'raw_keyboard_macos.dart';
import 'raw_keyboard_web.dart';
@@ -306,6 +307,13 @@ abstract class RawKeyEvent with Diagnosticable {
modifiers: message['modifiers'] as int? ?? 0);
character = message['characters'] as String?;
break;
case 'ios':
data = RawKeyEventDataIos(
characters: message['characters'] as String? ?? '',
charactersIgnoringModifiers: message['charactersIgnoringModifiers'] as String? ?? '',
keyCode: message['keyCode'] as int? ?? 0,
modifiers: message['modifiers'] as int? ?? 0);
break;
case 'linux':
final int unicodeScalarValues = message['unicodeScalarValues'] as int? ?? 0;
data = RawKeyEventDataLinux(
@@ -340,9 +348,10 @@ abstract class RawKeyEvent with Diagnosticable {
}
break;
default:
// Raw key events are not yet implemented on iOS or other platforms,
// but this exception isn't hit, because the engine never sends these
// messages.
/// This exception would only be hit on platforms that haven't yet
/// implemented raw key events, but will only be triggered if the
/// engine for those platforms sends raw key event messages in the
/// first place.
throw FlutterError('Unknown keymap for key events: $keymap');
}

View File

@@ -0,0 +1,370 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/foundation.dart';
import 'keyboard_key.dart';
import 'keyboard_maps.dart';
import 'raw_keyboard.dart';
/// Maps iOS specific string values of nonvisible keys to logical keys
///
/// See: https://developer.apple.com/documentation/uikit/uikeycommand/input_strings_for_special_keys?language=objc
const Map<String, LogicalKeyboardKey> _kIosToLogicalMap = <String, LogicalKeyboardKey>{
'UIKeyInputEscape': LogicalKeyboardKey.escape,
'UIKeyInputF1': LogicalKeyboardKey.f1,
'UIKeyInputF2': LogicalKeyboardKey.f2,
'UIKeyInputF3': LogicalKeyboardKey.f3,
'UIKeyInputF4': LogicalKeyboardKey.f4,
'UIKeyInputF5': LogicalKeyboardKey.f5,
'UIKeyInputF6': LogicalKeyboardKey.f6,
'UIKeyInputF7': LogicalKeyboardKey.f7,
'UIKeyInputF8': LogicalKeyboardKey.f8,
'UIKeyInputF9': LogicalKeyboardKey.f9,
'UIKeyInputF10': LogicalKeyboardKey.f10,
'UIKeyInputF11': LogicalKeyboardKey.f11,
'UIKeyInputF12': LogicalKeyboardKey.f12,
'UIKeyInputUpArrow': LogicalKeyboardKey.arrowUp,
'UIKeyInputDownArrow': LogicalKeyboardKey.arrowDown,
'UIKeyInputLeftArrow': LogicalKeyboardKey.arrowLeft,
'UIKeyInputRightArrow': LogicalKeyboardKey.arrowRight,
'UIKeyInputHome': LogicalKeyboardKey.home,
'UIKeyInputEnd': LogicalKeyboardKey.enter,
'UIKeyInputPageUp': LogicalKeyboardKey.pageUp,
'UIKeyInputPageDown': LogicalKeyboardKey.pageDown
};
/// Platform-specific key event data for iOS.
///
/// This object contains information about key events obtained from iOS'
/// `UIKey` interface.
///
/// See also:
///
/// * [RawKeyboard], which uses this interface to expose key data.
class RawKeyEventDataIos extends RawKeyEventData {
/// Creates a key event data structure specific for iOS.
///
/// The [characters], [charactersIgnoringModifiers], and [modifiers], arguments
/// must not be null.
const RawKeyEventDataIos({
this.characters = '',
this.charactersIgnoringModifiers = '',
this.keyCode = 0,
this.modifiers = 0,
}) : assert(characters != null),
assert(charactersIgnoringModifiers != null),
assert(keyCode != null),
assert(modifiers != null);
/// The Unicode characters associated with a key-up or key-down event.
///
/// See also:
///
/// * [Apple's UIKey documentation](https://developer.apple.com/documentation/uikit/uikey/3526130-characters?language=objc)
final String characters;
/// The characters generated by a key event as if no modifier key (except for
/// Shift) applies.
///
/// See also:
///
/// * [Apple's UIKey documentation](https://developer.apple.com/documentation/uikit/uikey/3526131-charactersignoringmodifiers?language=objc)
final String charactersIgnoringModifiers;
/// The virtual key code for the keyboard key associated with a key event.
///
/// See also:
///
/// * [Apple's UIKey documentation](https://developer.apple.com/documentation/uikit/uikey/3526132-keycode?language=objc)
final int keyCode;
/// A mask of the current modifiers using the values in Modifier Flags.
///
/// See also:
///
/// * [Apple's UIKey documentation](https://developer.apple.com/documentation/uikit/uikey/3526133-modifierflags?language=objc)
final int modifiers;
@override
String get keyLabel => charactersIgnoringModifiers;
@override
PhysicalKeyboardKey get physicalKey => kIosToPhysicalKey[keyCode] ?? PhysicalKeyboardKey.none;
@override
LogicalKeyboardKey get logicalKey {
// Look to see if the keyCode is a printable number pad key, so that a
// difference between regular keys (e.g. "=") and the number pad version
// (e.g. the "=" on the number pad) can be determined.
final LogicalKeyboardKey? numPadKey = kIosNumPadMap[keyCode];
if (numPadKey != null) {
return numPadKey;
}
// Look to see if the [keyLabel] is one we know about and have a mapping for.
final LogicalKeyboardKey? newKey = _kIosToLogicalMap[keyLabel];
if (newKey != null) {
return newKey;
}
// If this key is printable, generate the LogicalKeyboardKey from its
// Unicode value. Control keys such as ESC, CRTL, and SHIFT are not
// printable. HOME, DEL, arrow keys, and function keys are considered
// modifier function keys, which generate invalid Unicode scalar values.
if (keyLabel.isNotEmpty &&
!LogicalKeyboardKey.isControlCharacter(keyLabel)) {
// Given that charactersIgnoringModifiers can contain a String of
// arbitrary length, limit to a maximum of two Unicode scalar values. It
// is unlikely that a keyboard would produce a code point bigger than 32
// bits, but it is still worth defending against this case.
assert(charactersIgnoringModifiers.length <= 2);
int codeUnit = charactersIgnoringModifiers.codeUnitAt(0);
if (charactersIgnoringModifiers.length == 2) {
final int secondCode = charactersIgnoringModifiers.codeUnitAt(1);
codeUnit = (codeUnit << 16) | secondCode;
}
final int keyId = LogicalKeyboardKey.unicodePlane | (codeUnit & LogicalKeyboardKey.valueMask);
return LogicalKeyboardKey.findKeyByKeyId(keyId) ?? LogicalKeyboardKey(
keyId,
keyLabel: keyLabel,
debugName: kReleaseMode ? null : 'Key ${keyLabel.toUpperCase()}',
);
}
// Control keys like "backspace" and movement keys like arrow keys don't
// have a printable representation, but are present on the physical
// keyboard. Since there is no logical keycode map for iOS (iOS uses the
// keycode to reference physical keys), a LogicalKeyboardKey is created with
// the physical key's HID usage and debugName. This avoids duplicating the
// physical key map.
if (physicalKey != PhysicalKeyboardKey.none) {
final int keyId = physicalKey.usbHidUsage | LogicalKeyboardKey.hidPlane;
return LogicalKeyboardKey.findKeyByKeyId(keyId) ?? LogicalKeyboardKey(
keyId,
keyLabel: physicalKey.debugName ?? '',
debugName: physicalKey.debugName,
);
}
// This is a non-printable key that is unrecognized, so a new code is minted
// with the autogenerated bit set.
const int iosKeyIdPlane = 0x00400000000;
return LogicalKeyboardKey(
iosKeyIdPlane | keyCode | LogicalKeyboardKey.autogeneratedMask,
debugName: kReleaseMode ? null : 'Unknown iOS key code $keyCode',
);
}
bool _isLeftRightModifierPressed(KeyboardSide side, int anyMask, int leftMask, int rightMask) {
if (modifiers & anyMask == 0) {
return false;
}
// If only the "anyMask" bit is set, then we respond true for requests of
// whether either left or right is pressed. Handles the case where iOS
// supplies just the "either" modifier flag, but not the left/right flag.
// (e.g. modifierShift but not modifierLeftShift).
final bool anyOnly = modifiers & (leftMask | rightMask | anyMask) == anyMask;
switch (side) {
case KeyboardSide.any:
return true;
case KeyboardSide.all:
return modifiers & leftMask != 0 && modifiers & rightMask != 0 || anyOnly;
case KeyboardSide.left:
return modifiers & leftMask != 0 || anyOnly;
case KeyboardSide.right:
return modifiers & rightMask != 0 || anyOnly;
}
}
@override
bool isModifierPressed(ModifierKey key, {KeyboardSide side = KeyboardSide.any}) {
final int independentModifier = modifiers & deviceIndependentMask;
bool result;
switch (key) {
case ModifierKey.controlModifier:
result = _isLeftRightModifierPressed(side, independentModifier & modifierControl, modifierLeftControl, modifierRightControl);
break;
case ModifierKey.shiftModifier:
result = _isLeftRightModifierPressed(side, independentModifier & modifierShift, modifierLeftShift, modifierRightShift);
break;
case ModifierKey.altModifier:
result = _isLeftRightModifierPressed(side, independentModifier & modifierOption, modifierLeftOption, modifierRightOption);
break;
case ModifierKey.metaModifier:
result = _isLeftRightModifierPressed(side, independentModifier & modifierCommand, modifierLeftCommand, modifierRightCommand);
break;
case ModifierKey.capsLockModifier:
result = independentModifier & modifierCapsLock != 0;
break;
// On iOS, the function modifier bit is set for any function key, like F1,
// F2, etc., but the meaning of ModifierKey.modifierFunction in Flutter is
// that of the Fn modifier key, so there's no good way to emulate that on
// iOS.
case ModifierKey.functionModifier:
case ModifierKey.numLockModifier:
case ModifierKey.symbolModifier:
case ModifierKey.scrollLockModifier:
// These modifier masks are not used in iOS keyboards.
result = false;
break;
}
assert(!result || getModifierSide(key) != null, "$runtimeType thinks that a modifier is pressed, but can't figure out what side it's on.");
return result;
}
@override
KeyboardSide? getModifierSide(ModifierKey key) {
KeyboardSide? findSide(int anyMask, int leftMask, int rightMask) {
final int combinedMask = leftMask | rightMask;
final int combined = modifiers & combinedMask;
if (combined == leftMask) {
return KeyboardSide.left;
} else if (combined == rightMask) {
return KeyboardSide.right;
} else if (combined == combinedMask || modifiers & (combinedMask | anyMask) == anyMask) {
// Handles the case where iOS supplies just the "either" modifier
// flag, but not the left/right flag. (e.g. modifierShift but not
// modifierLeftShift), or if left and right flags are provided, but not
// the "either" modifier flag.
return KeyboardSide.all;
}
return null;
}
switch (key) {
case ModifierKey.controlModifier:
return findSide(modifierControl, modifierLeftControl, modifierRightControl);
case ModifierKey.shiftModifier:
return findSide(modifierShift, modifierLeftShift, modifierRightShift);
case ModifierKey.altModifier:
return findSide(modifierOption, modifierLeftOption, modifierRightOption);
case ModifierKey.metaModifier:
return findSide(modifierCommand, modifierLeftCommand, modifierRightCommand);
case ModifierKey.capsLockModifier:
case ModifierKey.numLockModifier:
case ModifierKey.scrollLockModifier:
case ModifierKey.functionModifier:
case ModifierKey.symbolModifier:
return KeyboardSide.all;
}
}
// Modifier key masks. See Apple's UIKey documentation
// https://developer.apple.com/documentation/uikit/uikeymodifierflags?language=objc
// https://opensource.apple.com/source/IOHIDFamily/IOHIDFamily-86/IOHIDSystem/IOKit/hidsystem/IOLLEvent.h.auto.html
/// This mask is used to check the [modifiers] field to test whether the CAPS
/// LOCK modifier key is on.
///
/// {@template flutter.services.rawKeyEventDataIOS.modifiers}
/// Use this value if you need to decode the [modifiers] field yourself, but
/// it's much easier to use [isModifierPressed] if you just want to know if
/// a modifier is pressed.
/// {@endtemplate}
static const int modifierCapsLock = 0x10000;
/// This mask is used to check the [modifiers] field to test whether one of the
/// SHIFT modifier keys is pressed.
///
/// {@macro flutter.services.rawKeyEventDataIOS.modifiers}
static const int modifierShift = 0x20000;
/// This mask is used to check the [modifiers] field to test whether the left
/// SHIFT modifier key is pressed.
///
/// {@macro flutter.services.rawKeyEventDataIOS.modifiers}
static const int modifierLeftShift = 0x02;
/// This mask is used to check the [modifiers] field to test whether the right
/// SHIFT modifier key is pressed.
///
/// {@macro flutter.services.rawKeyEventDataIOS.modifiers}
static const int modifierRightShift = 0x04;
/// This mask is used to check the [modifiers] field to test whether one of the
/// CTRL modifier keys is pressed.
///
/// {@macro flutter.services.rawKeyEventDataIOS.modifiers}
static const int modifierControl = 0x40000;
/// This mask is used to check the [modifiers] field to test whether the left
/// CTRL modifier key is pressed.
///
/// {@macro flutter.services.rawKeyEventDataIOS.modifiers}
static const int modifierLeftControl = 0x01;
/// This mask is used to check the [modifiers] field to test whether the right
/// CTRL modifier key is pressed.
///
/// {@macro flutter.services.rawKeyEventDataIOS.modifiers}
static const int modifierRightControl = 0x2000;
/// This mask is used to check the [modifiers] field to test whether one of the
/// ALT modifier keys is pressed.
///
/// {@macro flutter.services.rawKeyEventDataIOS.modifiers}
static const int modifierOption = 0x80000;
/// This mask is used to check the [modifiers] field to test whether the left
/// ALT modifier key is pressed.
///
/// {@macro flutter.services.rawKeyEventDataIOS.modifiers}
static const int modifierLeftOption = 0x20;
/// This mask is used to check the [modifiers] field to test whether the right
/// ALT modifier key is pressed.
///
/// {@macro flutter.services.rawKeyEventDataIOS.modifiers}
static const int modifierRightOption = 0x40;
/// This mask is used to check the [modifiers] field to test whether one of the
/// CMD modifier keys is pressed.
///
/// {@macro flutter.services.rawKeyEventDataIOS.modifiers}
static const int modifierCommand = 0x100000;
/// This mask is used to check the [modifiers] field to test whether the left
/// CMD modifier keys is pressed.
///
/// {@macro flutter.services.rawKeyEventDataIOS.modifiers}
static const int modifierLeftCommand = 0x08;
/// This mask is used to check the [modifiers] field to test whether the right
/// CMD modifier keys is pressed.
///
/// {@macro flutter.services.rawKeyEventDataIOS.modifiers}
static const int modifierRightCommand = 0x10;
/// This mask is used to check the [modifiers] field to test whether any key in
/// the numeric keypad is pressed.
///
/// {@macro flutter.services.rawKeyEventDataIOS.modifiers}
static const int modifierNumericPad = 0x200000;
/// This mask is used to check the [modifiers] field to test whether the
/// HELP modifier key is pressed.
///
/// {@macro flutter.services.rawKeyEventDataIOS.modifiers}
static const int modifierHelp = 0x400000;
/// This mask is used to check the [modifiers] field to test whether one of the
/// FUNCTION modifier keys is pressed.
///
/// {@macro flutter.services.rawKeyEventDataIOS.modifiers}
static const int modifierFunction = 0x800000;
/// Used to retrieve only the device-independent modifier flags, allowing
/// applications to mask off the device-dependent modifier flags, including
/// event coalescing information.
static const int deviceIndependentMask = 0xffff0000;
@override
String toString() {
return '${objectRuntimeType(this, 'RawKeyEventDataIos')}(keyLabel: $keyLabel, keyCode: $keyCode, characters: $characters,'
' unmodifiedCharacters: $charactersIgnoringModifiers, modifiers: $modifiers, '
'modifiers down: $modifiersPressed)';
}
}

View File

@@ -41,7 +41,7 @@ void main() {
}
});
testWidgets('keysPressed is maintained', (WidgetTester tester) async {
for (final String platform in <String>['linux', 'android', 'macos', 'fuchsia', 'windows']) {
for (final String platform in <String>['linux', 'android', 'macos', 'fuchsia', 'windows', 'ios']) {
RawKeyboard.instance.clearKeysPressed();
expect(RawKeyboard.instance.keysPressed, isEmpty, reason: 'on $platform');
await simulateKeyDownEvent(LogicalKeyboardKey.shiftLeft, platform: platform);
@@ -110,7 +110,7 @@ void main() {
await simulateKeyUpEvent(LogicalKeyboardKey.shiftLeft, platform: platform);
expect(RawKeyboard.instance.keysPressed, isEmpty, reason: 'on $platform');
// The Fn key isn't mapped on linux or Windows.
if (platform != 'linux' && platform != 'windows') {
if (platform != 'linux' && platform != 'windows' && platform != 'ios') {
await simulateKeyDownEvent(LogicalKeyboardKey.fn, platform: platform);
expect(
RawKeyboard.instance.keysPressed,
@@ -146,7 +146,7 @@ void main() {
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/61021
testWidgets('keysPressed is correct when modifier is released before key', (WidgetTester tester) async {
for (final String platform in <String>['linux', 'android', 'macos', 'fuchsia', 'windows']) {
for (final String platform in <String>['linux', 'android', 'macos', 'fuchsia', 'windows', 'ios']) {
RawKeyboard.instance.clearKeysPressed();
expect(RawKeyboard.instance.keysPressed, isEmpty, reason: 'on $platform');
await simulateKeyDownEvent(LogicalKeyboardKey.shiftLeft, platform: platform, physicalKey: PhysicalKeyboardKey.shiftLeft);
@@ -221,6 +221,31 @@ void main() {
);
});
testWidgets('keysPressed modifiers are synchronized with key events on iOS', (WidgetTester tester) async {
expect(RawKeyboard.instance.keysPressed, isEmpty);
// Generate the data for a regular key down event.
final Map<String, dynamic> data = KeyEventSimulator.getKeyData(
LogicalKeyboardKey.keyA,
platform: 'ios',
isDown: true,
);
// Change the modifiers so that they show the shift key as already down
// when this event is received, but it's not in keysPressed yet.
data['modifiers'] |= RawKeyEventDataMacOs.modifierLeftShift | RawKeyEventDataMacOs.modifierShift;
// dispatch the modified data.
await ServicesBinding.instance!.defaultBinaryMessenger.handlePlatformMessage(
SystemChannels.keyEvent.name,
SystemChannels.keyEvent.codec.encodeMessage(data),
(ByteData? data) {},
);
expect(
RawKeyboard.instance.keysPressed,
equals(
<LogicalKeyboardKey>{LogicalKeyboardKey.shiftLeft, LogicalKeyboardKey.keyA},
),
);
});
testWidgets('keysPressed modifiers are synchronized with key events on Windows', (WidgetTester tester) async {
expect(RawKeyboard.instance.keysPressed, isEmpty);
// Generate the data for a regular key down event.
@@ -403,6 +428,44 @@ void main() {
);
});
testWidgets('sided modifiers without a side set return all sides on iOS', (WidgetTester tester) async {
expect(RawKeyboard.instance.keysPressed, isEmpty);
// Generate the data for a regular key down event.
final Map<String, dynamic> data = KeyEventSimulator.getKeyData(
LogicalKeyboardKey.keyA,
platform: 'ios',
isDown: true,
);
// Set only the generic "shift down" modifier, without setting a side.
data['modifiers'] |=
RawKeyEventDataIos.modifierShift |
RawKeyEventDataIos.modifierOption |
RawKeyEventDataIos.modifierCommand |
RawKeyEventDataIos.modifierControl;
// dispatch the modified data.
await ServicesBinding.instance?.defaultBinaryMessenger.handlePlatformMessage(
SystemChannels.keyEvent.name,
SystemChannels.keyEvent.codec.encodeMessage(data),
(ByteData? data) {},
);
expect(
RawKeyboard.instance.keysPressed,
equals(
<LogicalKeyboardKey>{
LogicalKeyboardKey.shiftLeft,
LogicalKeyboardKey.shiftRight,
LogicalKeyboardKey.altLeft,
LogicalKeyboardKey.altRight,
LogicalKeyboardKey.controlLeft,
LogicalKeyboardKey.controlRight,
LogicalKeyboardKey.metaLeft,
LogicalKeyboardKey.metaRight,
LogicalKeyboardKey.keyA,
},
),
);
});
testWidgets('sided modifiers without a side set return all sides on Windows', (WidgetTester tester) async {
expect(RawKeyboard.instance.keysPressed, isEmpty);
// Generate the data for a regular key down event.
@@ -946,7 +1009,6 @@ void main() {
'type': 'keydown',
'keymap': 'macos',
'keyCode': 0x04,
'plainCodePoint': 0x64,
'characters': 'a',
'charactersIgnoringModifiers': 'a',
'modifiers': modifier | RawKeyEventDataMacOs.modifierCapsLock,
@@ -1037,6 +1099,152 @@ void main() {
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/35347
});
group('RawKeyEventDataIos', () {
const Map<int, _ModifierCheck> modifierTests = <int, _ModifierCheck>{
RawKeyEventDataIos.modifierOption | RawKeyEventDataIos.modifierLeftOption: _ModifierCheck(ModifierKey.altModifier, KeyboardSide.left),
RawKeyEventDataIos.modifierOption | RawKeyEventDataIos.modifierRightOption: _ModifierCheck(ModifierKey.altModifier, KeyboardSide.right),
RawKeyEventDataIos.modifierShift | RawKeyEventDataIos.modifierLeftShift: _ModifierCheck(ModifierKey.shiftModifier, KeyboardSide.left),
RawKeyEventDataIos.modifierShift | RawKeyEventDataIos.modifierRightShift: _ModifierCheck(ModifierKey.shiftModifier, KeyboardSide.right),
RawKeyEventDataIos.modifierControl | RawKeyEventDataIos.modifierLeftControl: _ModifierCheck(ModifierKey.controlModifier, KeyboardSide.left),
RawKeyEventDataIos.modifierControl | RawKeyEventDataIos.modifierRightControl: _ModifierCheck(ModifierKey.controlModifier, KeyboardSide.right),
RawKeyEventDataIos.modifierCommand | RawKeyEventDataIos.modifierLeftCommand: _ModifierCheck(ModifierKey.metaModifier, KeyboardSide.left),
RawKeyEventDataIos.modifierCommand | RawKeyEventDataIos.modifierRightCommand: _ModifierCheck(ModifierKey.metaModifier, KeyboardSide.right),
RawKeyEventDataIos.modifierOption: _ModifierCheck(ModifierKey.altModifier, KeyboardSide.all),
RawKeyEventDataIos.modifierShift: _ModifierCheck(ModifierKey.shiftModifier, KeyboardSide.all),
RawKeyEventDataIos.modifierControl: _ModifierCheck(ModifierKey.controlModifier, KeyboardSide.all),
RawKeyEventDataIos.modifierCommand: _ModifierCheck(ModifierKey.metaModifier, KeyboardSide.all),
RawKeyEventDataIos.modifierCapsLock: _ModifierCheck(ModifierKey.capsLockModifier, KeyboardSide.all),
};
test('modifier keys are recognized individually', () {
for (final int modifier in modifierTests.keys) {
final RawKeyEvent event = RawKeyEvent.fromMessage(<String, dynamic>{
'type': 'keydown',
'keymap': 'ios',
'keyCode': 0x04,
'characters': 'a',
'charactersIgnoringModifiers': 'a',
'modifiers': modifier,
});
final RawKeyEventDataIos data = event.data as RawKeyEventDataIos;
for (final ModifierKey key in ModifierKey.values) {
if (modifierTests[modifier]!.key == key) {
expect(
data.isModifierPressed(key, side: modifierTests[modifier]!.side),
isTrue,
reason: "$key should be pressed with metaState $modifier, but isn't.",
);
expect(data.getModifierSide(key), equals(modifierTests[modifier]!.side));
} else {
expect(
data.isModifierPressed(key, side: modifierTests[modifier]!.side),
isFalse,
reason: '$key should not be pressed with metaState $modifier.',
);
}
}
}
});
test('modifier keys are recognized when combined', () {
for (final int modifier in modifierTests.keys) {
if (modifier == RawKeyEventDataIos.modifierCapsLock) {
// No need to combine caps lock key with itself.
continue;
}
final RawKeyEvent event = RawKeyEvent.fromMessage(<String, dynamic>{
'type': 'keydown',
'keymap': 'ios',
'keyCode': 0x04,
'characters': 'a',
'charactersIgnoringModifiers': 'a',
'modifiers': modifier | RawKeyEventDataIos.modifierCapsLock,
});
final RawKeyEventDataIos data = event.data as RawKeyEventDataIos;
for (final ModifierKey key in ModifierKey.values) {
if (modifierTests[modifier]!.key == key || key == ModifierKey.capsLockModifier) {
expect(
data.isModifierPressed(key, side: modifierTests[modifier]!.side),
isTrue,
reason: '$key should be pressed with metaState $modifier '
"and additional key ${RawKeyEventDataIos.modifierCapsLock}, but isn't.",
);
if (key != ModifierKey.capsLockModifier) {
expect(data.getModifierSide(key), equals(modifierTests[modifier]!.side));
} else {
expect(data.getModifierSide(key), equals(KeyboardSide.all));
}
} else {
expect(
data.isModifierPressed(key, side: modifierTests[modifier]!.side),
isFalse,
reason: '$key should not be pressed with metaState $modifier '
'and additional key ${RawKeyEventDataIos.modifierCapsLock}.',
);
}
}
}
});
test('Printable keyboard keys are correctly translated', () {
const String unmodifiedCharacter = 'a';
final RawKeyEvent keyAEvent = RawKeyEvent.fromMessage(const <String, dynamic>{
'type': 'keydown',
'keymap': 'ios',
'keyCode': 0x00000004,
'characters': 'a',
'charactersIgnoringModifiers': unmodifiedCharacter,
'modifiers': 0x0,
});
final RawKeyEventDataIos data = keyAEvent.data as RawKeyEventDataIos;
expect(data.physicalKey, equals(PhysicalKeyboardKey.keyA));
expect(data.logicalKey, equals(LogicalKeyboardKey.keyA));
expect(data.keyLabel, equals('a'));
});
test('Control keyboard keys are correctly translated', () {
final RawKeyEvent escapeKeyEvent = RawKeyEvent.fromMessage(const <String, dynamic>{
'type': 'keydown',
'keymap': 'ios',
'keyCode': 0x00000029,
'characters': '',
'charactersIgnoringModifiers': '',
'modifiers': 0x0,
});
final RawKeyEventDataIos data = escapeKeyEvent.data as RawKeyEventDataIos;
expect(data.physicalKey, equals(PhysicalKeyboardKey.escape));
expect(data.logicalKey, equals(LogicalKeyboardKey.escape));
expect(data.keyLabel, isEmpty);
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/35347
test('Modifier keyboard keys are correctly translated', () {
final RawKeyEvent shiftLeftKeyEvent = RawKeyEvent.fromMessage(const <String, dynamic>{
'type': 'keydown',
'keymap': 'ios',
'keyCode': 0x000000e1,
'characters': '',
'charactersIgnoringModifiers': '',
'modifiers': RawKeyEventDataIos.modifierLeftShift,
});
final RawKeyEventDataIos data = shiftLeftKeyEvent.data as RawKeyEventDataIos;
expect(data.physicalKey, equals(PhysicalKeyboardKey.shiftLeft));
expect(data.logicalKey, equals(LogicalKeyboardKey.shiftLeft));
expect(data.keyLabel, isEmpty);
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/35347
test('Unprintable keyboard keys are correctly translated', () {
final RawKeyEvent leftArrowKey = RawKeyEvent.fromMessage(const <String, dynamic>{
'type': 'keydown',
'keymap': 'ios',
'keyCode': 0x00000050,
'characters': '',
'charactersIgnoringModifiers': 'UIKeyInputLeftArrow',
'modifiers': RawKeyEventDataIos.modifierFunction,
});
final RawKeyEventDataIos data = leftArrowKey.data as RawKeyEventDataIos;
expect(data.physicalKey, equals(PhysicalKeyboardKey.arrowLeft));
expect(data.logicalKey, equals(LogicalKeyboardKey.arrowLeft));
expect(data.logicalKey.keyLabel, isEmpty);
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/35347
});
group('RawKeyEventDataWindows', () {
const Map<int, _ModifierCheck> modifierTests = <int, _ModifierCheck>{
RawKeyEventDataWindows.modifierLeftAlt: _ModifierCheck(ModifierKey.altModifier, KeyboardSide.left),

View File

@@ -39,6 +39,7 @@ class KeyEventSimulator {
case 'macos':
case 'linux':
case 'web':
case 'ios':
case 'windows':
return true;
}
@@ -58,6 +59,9 @@ class KeyEventSimulator {
case 'macos':
map = kMacOsToPhysicalKey;
break;
case 'ios':
map = kIosToPhysicalKey;
break;
case 'linux':
map = kLinuxToPhysicalKey;
break;
@@ -90,7 +94,10 @@ class KeyEventSimulator {
map = kFuchsiaToLogicalKey;
break;
case 'macos':
// macOS doesn't do key codes, just scan codes.
// macOS doesn't do key codes, just scan codes.
return -1;
case 'ios':
// iOS doesn't do key codes, just scan codes.
return -1;
case 'web':
// web doesn't have int type code
@@ -137,6 +144,9 @@ class KeyEventSimulator {
case 'macos':
map = kMacOsToPhysicalKey;
break;
case 'ios':
map = kIosToPhysicalKey;
break;
case 'linux':
map = kLinuxToPhysicalKey;
break;
@@ -213,6 +223,12 @@ class KeyEventSimulator {
}
result['modifiers'] = _getMacOsModifierFlags(key, isDown);
break;
case 'ios':
result['keyCode'] = scanCode;
result['characters'] = key.keyLabel;
result['charactersIgnoringModifiers'] = key.keyLabel;
result['modifiers'] = _getIOSModifierFlags(key, isDown);
break;
case 'web':
result['code'] = _getWebKeyCode(key);
result['key'] = key.keyLabel;
@@ -504,6 +520,73 @@ class KeyEventSimulator {
return result;
}
static int _getIOSModifierFlags(LogicalKeyboardKey newKey, bool isDown) {
int result = 0;
final Set<LogicalKeyboardKey> pressed = RawKeyboard.instance.keysPressed;
if (isDown) {
pressed.add(newKey);
} else {
pressed.remove(newKey);
}
if (pressed.contains(LogicalKeyboardKey.shiftLeft)) {
result |= RawKeyEventDataIos.modifierLeftShift | RawKeyEventDataIos.modifierShift;
}
if (pressed.contains(LogicalKeyboardKey.shiftRight)) {
result |= RawKeyEventDataIos.modifierRightShift | RawKeyEventDataIos.modifierShift;
}
if (pressed.contains(LogicalKeyboardKey.metaLeft)) {
result |= RawKeyEventDataIos.modifierLeftCommand | RawKeyEventDataIos.modifierCommand;
}
if (pressed.contains(LogicalKeyboardKey.metaRight)) {
result |= RawKeyEventDataIos.modifierRightCommand | RawKeyEventDataIos.modifierCommand;
}
if (pressed.contains(LogicalKeyboardKey.controlLeft)) {
result |= RawKeyEventDataIos.modifierLeftControl | RawKeyEventDataIos.modifierControl;
}
if (pressed.contains(LogicalKeyboardKey.controlRight)) {
result |= RawKeyEventDataIos.modifierRightControl | RawKeyEventDataIos.modifierControl;
}
if (pressed.contains(LogicalKeyboardKey.altLeft)) {
result |= RawKeyEventDataIos.modifierLeftOption | RawKeyEventDataIos.modifierOption;
}
if (pressed.contains(LogicalKeyboardKey.altRight)) {
result |= RawKeyEventDataIos.modifierRightOption | RawKeyEventDataIos.modifierOption;
}
final Set<LogicalKeyboardKey> functionKeys = <LogicalKeyboardKey>{
LogicalKeyboardKey.f1,
LogicalKeyboardKey.f2,
LogicalKeyboardKey.f3,
LogicalKeyboardKey.f4,
LogicalKeyboardKey.f5,
LogicalKeyboardKey.f6,
LogicalKeyboardKey.f7,
LogicalKeyboardKey.f8,
LogicalKeyboardKey.f9,
LogicalKeyboardKey.f10,
LogicalKeyboardKey.f11,
LogicalKeyboardKey.f12,
LogicalKeyboardKey.f13,
LogicalKeyboardKey.f14,
LogicalKeyboardKey.f15,
LogicalKeyboardKey.f16,
LogicalKeyboardKey.f17,
LogicalKeyboardKey.f18,
LogicalKeyboardKey.f19,
LogicalKeyboardKey.f20,
LogicalKeyboardKey.f21,
};
if (pressed.intersection(functionKeys).isNotEmpty) {
result |= RawKeyEventDataIos.modifierFunction;
}
if (pressed.intersection(kMacOsNumPadMap.values.toSet()).isNotEmpty) {
result |= RawKeyEventDataIos.modifierNumericPad;
}
if (pressed.contains(LogicalKeyboardKey.capsLock)) {
result |= RawKeyEventDataIos.modifierCapsLock;
}
return result;
}
/// Simulates sending a hardware key down event through the system channel.
///
/// This only simulates key presses coming from a physical keyboard, not from a