Skip to content

Commit d87faf9

Browse files
authored
[Keyboard, iOS] Generate iOS's special key mapping (#106909)
1 parent cadb264 commit d87faf9

File tree

7 files changed

+99
-29
lines changed

7 files changed

+99
-29
lines changed

dev/tools/gen_keycodes/data/ios_key_code_map_mm.tmpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@ const std::set<uint32_t> functionKeyCodes = {
4848
@@@IOS_FUNCTION_KEY_SET@@@
4949
};
5050

51+
API_AVAILABLE(ios(13.4))
52+
NSDictionary<NSString*, NSNumber*>* specialKeyMapping = [[NSDictionary alloc] initWithDictionary:@{
53+
@@@SPECIAL_KEY_MAPPING@@@
54+
}];
55+
5156
@@@SPECIAL_KEY_CONSTANTS@@@

dev/tools/gen_keycodes/data/keyboard_maps.tmpl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ const Map<int, PhysicalKeyboardKey> kIosToPhysicalKey = <int, PhysicalKeyboardKe
7878
@@@IOS_SCAN_CODE_MAP@@@
7979
};
8080

81+
/// Maps iOS specific string values of nonvisible keys to logical keys
82+
///
83+
/// Some unprintable keys on iOS has literal names on their key label, such as
84+
/// "UIKeyInputEscape". See:
85+
/// https://developer.apple.com/documentation/uikit/uikeycommand/input_strings_for_special_keys?language=objc
86+
const Map<String, LogicalKeyboardKey> kIosSpecialLogicalMap = <String, LogicalKeyboardKey>{
87+
@@@IOS_SPECIAL_MAP@@@
88+
};
89+
8190
/// A map of iOS key codes which have printable representations, but appear
8291
/// on the number pad. Used to provide different key objects for keys like
8392
/// KEY_EQUALS and NUMPAD_EQUALS.

dev/tools/gen_keycodes/lib/data.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
/// Maps iOS's special key labels to logical key names.
6+
///
7+
/// See https://developer.apple.com/documentation/uikit/uikeycommand/input_strings_for_special_keys?language=objc
8+
const Map<String, String> kIosSpecialKeyMapping = <String, String>{
9+
'UIKeyInputEscape': 'Escape',
10+
'UIKeyInputF1': 'F1',
11+
'UIKeyInputF2': 'F2',
12+
'UIKeyInputF3': 'F3',
13+
'UIKeyInputF4': 'F4',
14+
'UIKeyInputF5': 'F5',
15+
'UIKeyInputF6': 'F6',
16+
'UIKeyInputF7': 'F7',
17+
'UIKeyInputF8': 'F8',
18+
'UIKeyInputF9': 'F9',
19+
'UIKeyInputF10': 'F10',
20+
'UIKeyInputF11': 'F11',
21+
'UIKeyInputF12': 'F12',
22+
'UIKeyInputUpArrow': 'ArrowUp',
23+
'UIKeyInputDownArrow': 'ArrowDown',
24+
'UIKeyInputLeftArrow': 'ArrowLeft',
25+
'UIKeyInputRightArrow': 'ArrowRight',
26+
'UIKeyInputHome': 'Home',
27+
'UIKeyInputEnd': 'Enter',
28+
'UIKeyInputPageUp': 'PageUp',
29+
'UIKeyInputPageDown': 'PageDown',
30+
};

dev/tools/gen_keycodes/lib/ios_code_gen.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:path/path.dart' as path;
66

77
import 'base_code_gen.dart';
88
import 'constants.dart';
9+
import 'data.dart';
910
import 'logical_key_data.dart';
1011
import 'physical_key_data.dart';
1112
import 'utils.dart';
@@ -105,6 +106,15 @@ class IOSCodeGenerator extends PlatformCodeGenerator {
105106
return modifierKeyMap.toString().trimRight();
106107
}
107108

109+
String get _specialKeyMapping {
110+
final OutputLines<int> lines = OutputLines<int>('iOS special key mapping');
111+
kIosSpecialKeyMapping.forEach((String key, String logicalName) {
112+
final int value = logicalData.entryByName(logicalName).value;
113+
lines.add(value, ' @"$key" : @(${toHex(value)}),');
114+
});
115+
return lines.join().trimRight();
116+
}
117+
108118
/// This generates some keys that needs special attention.
109119
String get _specialKeyConstants {
110120
final StringBuffer specialKeyConstants = StringBuffer();
@@ -137,6 +147,7 @@ class IOSCodeGenerator extends PlatformCodeGenerator {
137147
'KEYCODE_TO_MODIFIER_FLAG_MAP': _keyToModifierFlagMap,
138148
'MODIFIER_FLAG_TO_KEYCODE_MAP': _modifierFlagToKeyMap,
139149
'SPECIAL_KEY_CONSTANTS': _specialKeyConstants,
150+
'SPECIAL_KEY_MAPPING': _specialKeyMapping,
140151
};
141152
}
142153
}

dev/tools/gen_keycodes/lib/keyboard_maps_code_gen.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:io';
77
import 'package:path/path.dart' as path;
88

99
import 'base_code_gen.dart';
10+
import 'data.dart';
1011
import 'logical_key_data.dart';
1112
import 'physical_key_data.dart';
1213
import 'utils.dart';
@@ -246,6 +247,16 @@ class KeyboardMapsCodeGenerator extends BaseCodeGenerator {
246247
return lines.sortedJoin().trimRight();
247248
}
248249

250+
/// This generates the map of iOS key label to logical keys for special keys.
251+
String get _iOSSpecialMap {
252+
final OutputLines<int> lines = OutputLines<int>('iOS special key mapping');
253+
kIosSpecialKeyMapping.forEach((String key, String logicalName) {
254+
final LogicalKeyEntry entry = logicalData.entryByName(logicalName);
255+
lines.add(entry.value, " '$key': LogicalKeyboardKey.${entry.constantName},");
256+
});
257+
return lines.join().trimRight();
258+
}
259+
249260
/// This generates the map of iOS number pad key codes to logical keys.
250261
String get _iOSNumpadMap {
251262
final OutputLines<int> lines = OutputLines<int>('iOS numpad map');
@@ -353,6 +364,7 @@ class KeyboardMapsCodeGenerator extends BaseCodeGenerator {
353364
'MACOS_FUNCTION_KEY_MAP': _macOSFunctionKeyMap,
354365
'MACOS_KEY_CODE_MAP': _macOSKeyCodeMap,
355366
'IOS_SCAN_CODE_MAP': _iOSScanCodeMap,
367+
'IOS_SPECIAL_MAP': _iOSSpecialMap,
356368
'IOS_NUMPAD_MAP': _iOSNumpadMap,
357369
'IOS_KEY_CODE_MAP': _iOSKeyCodeMap,
358370
'GLFW_KEY_CODE_MAP': _glfwKeyCodeMap,

packages/flutter/lib/src/services/keyboard_maps.g.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,35 @@ const Map<int, PhysicalKeyboardKey> kIosToPhysicalKey = <int, PhysicalKeyboardKe
14681468
0x000000e7: PhysicalKeyboardKey.metaRight,
14691469
};
14701470

1471+
/// Maps iOS specific string values of nonvisible keys to logical keys
1472+
///
1473+
/// Some unprintable keys on iOS has literal names on their key label, such as
1474+
/// "UIKeyInputEscape". See:
1475+
/// https://developer.apple.com/documentation/uikit/uikeycommand/input_strings_for_special_keys?language=objc
1476+
const Map<String, LogicalKeyboardKey> kIosSpecialLogicalMap = <String, LogicalKeyboardKey>{
1477+
'UIKeyInputEscape': LogicalKeyboardKey.escape,
1478+
'UIKeyInputF1': LogicalKeyboardKey.f1,
1479+
'UIKeyInputF2': LogicalKeyboardKey.f2,
1480+
'UIKeyInputF3': LogicalKeyboardKey.f3,
1481+
'UIKeyInputF4': LogicalKeyboardKey.f4,
1482+
'UIKeyInputF5': LogicalKeyboardKey.f5,
1483+
'UIKeyInputF6': LogicalKeyboardKey.f6,
1484+
'UIKeyInputF7': LogicalKeyboardKey.f7,
1485+
'UIKeyInputF8': LogicalKeyboardKey.f8,
1486+
'UIKeyInputF9': LogicalKeyboardKey.f9,
1487+
'UIKeyInputF10': LogicalKeyboardKey.f10,
1488+
'UIKeyInputF11': LogicalKeyboardKey.f11,
1489+
'UIKeyInputF12': LogicalKeyboardKey.f12,
1490+
'UIKeyInputUpArrow': LogicalKeyboardKey.arrowUp,
1491+
'UIKeyInputDownArrow': LogicalKeyboardKey.arrowDown,
1492+
'UIKeyInputLeftArrow': LogicalKeyboardKey.arrowLeft,
1493+
'UIKeyInputRightArrow': LogicalKeyboardKey.arrowRight,
1494+
'UIKeyInputHome': LogicalKeyboardKey.home,
1495+
'UIKeyInputEnd': LogicalKeyboardKey.enter,
1496+
'UIKeyInputPageUp': LogicalKeyboardKey.pageUp,
1497+
'UIKeyInputPageDown': LogicalKeyboardKey.pageDown,
1498+
};
1499+
14711500
/// A map of iOS key codes which have printable representations, but appear
14721501
/// on the number pad. Used to provide different key objects for keys like
14731502
/// KEY_EQUALS and NUMPAD_EQUALS.

packages/flutter/lib/src/services/raw_keyboard_ios.dart

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,6 @@ export 'package:flutter/foundation.dart' show DiagnosticPropertiesBuilder;
1212
export 'keyboard_key.g.dart' show LogicalKeyboardKey, PhysicalKeyboardKey;
1313
export 'raw_keyboard.dart' show KeyboardSide, ModifierKey;
1414

15-
/// Maps iOS specific string values of nonvisible keys to logical keys
16-
///
17-
/// See: https://developer.apple.com/documentation/uikit/uikeycommand/input_strings_for_special_keys?language=objc
18-
const Map<String, LogicalKeyboardKey> _kIosToLogicalMap = <String, LogicalKeyboardKey>{
19-
'UIKeyInputEscape': LogicalKeyboardKey.escape,
20-
'UIKeyInputF1': LogicalKeyboardKey.f1,
21-
'UIKeyInputF2': LogicalKeyboardKey.f2,
22-
'UIKeyInputF3': LogicalKeyboardKey.f3,
23-
'UIKeyInputF4': LogicalKeyboardKey.f4,
24-
'UIKeyInputF5': LogicalKeyboardKey.f5,
25-
'UIKeyInputF6': LogicalKeyboardKey.f6,
26-
'UIKeyInputF7': LogicalKeyboardKey.f7,
27-
'UIKeyInputF8': LogicalKeyboardKey.f8,
28-
'UIKeyInputF9': LogicalKeyboardKey.f9,
29-
'UIKeyInputF10': LogicalKeyboardKey.f10,
30-
'UIKeyInputF11': LogicalKeyboardKey.f11,
31-
'UIKeyInputF12': LogicalKeyboardKey.f12,
32-
'UIKeyInputUpArrow': LogicalKeyboardKey.arrowUp,
33-
'UIKeyInputDownArrow': LogicalKeyboardKey.arrowDown,
34-
'UIKeyInputLeftArrow': LogicalKeyboardKey.arrowLeft,
35-
'UIKeyInputRightArrow': LogicalKeyboardKey.arrowRight,
36-
'UIKeyInputHome': LogicalKeyboardKey.home,
37-
'UIKeyInputEnd': LogicalKeyboardKey.enter,
38-
'UIKeyInputPageUp': LogicalKeyboardKey.pageUp,
39-
'UIKeyInputPageDown': LogicalKeyboardKey.pageDown,
40-
};
4115
/// Platform-specific key event data for iOS.
4216
///
4317
/// This object contains information about key events obtained from iOS'
@@ -107,9 +81,9 @@ class RawKeyEventDataIos extends RawKeyEventData {
10781
}
10882

10983
// Look to see if the [keyLabel] is one we know about and have a mapping for.
110-
final LogicalKeyboardKey? newKey = _kIosToLogicalMap[keyLabel];
111-
if (newKey != null) {
112-
return newKey;
84+
final LogicalKeyboardKey? specialKey = kIosSpecialLogicalMap[keyLabel];
85+
if (specialKey != null) {
86+
return specialKey;
11387
}
11488

11589
// Keys that can't be derived with characterIgnoringModifiers will be

0 commit comments

Comments
 (0)