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() {