Adding support for logical and physical key events (#27627)

This adds support for logical and physical key information inside of RawKeyEvent. This allows developers to differentiate keys in a platform-agnostic way. They are able to tell the physical location of a key (PhysicalKeyboardKey) and a logical meaning of the key (LogicalKeyboardKey), as well as get notified of the character generated by the keypress. All of which is useful for handling keyboard shortcuts.

This PR builds on the previous PR (#27620) which generated the key code mappings and definitions.
This commit is contained in:
Greg Spencer
2019-02-08 12:42:34 -08:00
committed by GitHub
parent b7807ce84c
commit 431cfdafd9
15 changed files with 4771 additions and 85 deletions

View File

@@ -15,7 +15,7 @@ class CodeGenerator {
/// Given an [input] string, wraps the text at 80 characters and prepends each
/// line with the [prefix] string. Use for generated comments.
String wrapString(String input, String prefix) {
String wrapString(String input, {String prefix = ' /// '}) {
final int wrapWidth = 80 - prefix.length;
final StringBuffer result = StringBuffer();
final List<String> words = input.split(RegExp(r'\s+'));
@@ -38,12 +38,14 @@ class CodeGenerator {
String get physicalDefinitions {
final StringBuffer definitions = StringBuffer();
for (Key entry in keyData.data) {
final String comment = wrapString('Represents the location of a '
'"${entry.commentName}" key on a generalized keyboard. See the function '
'[RawKeyEvent.physicalKey] for more information.', ' /// ');
final String firstComment = wrapString('Represents the location of the '
'"${entry.commentName}" key on a generalized keyboard.');
final String otherComments = wrapString('See the function '
'[RawKeyEvent.physicalKey] for more information.');
definitions.write('''
$comment static const PhysicalKeyboardKey ${entry.constantName} = PhysicalKeyboardKey(${toHex(entry.usbHidCode, digits: 8)}, debugName: kReleaseMode ? null : '${entry.commentName}');
$firstComment ///
$otherComments static const PhysicalKeyboardKey ${entry.constantName} = PhysicalKeyboardKey(${toHex(entry.usbHidCode, digits: 8)}, debugName: kReleaseMode ? null : '${entry.commentName}');
''');
}
return definitions.toString();
@@ -54,17 +56,19 @@ $comment static const PhysicalKeyboardKey ${entry.constantName} = PhysicalKeybo
String escapeLabel(String label) => label.contains("'") ? 'r"$label"' : "r'$label'";
final StringBuffer definitions = StringBuffer();
for (Key entry in keyData.data) {
final String comment = wrapString('Represents a logical "${entry.commentName}" key on the '
'keyboard. See the function [RawKeyEvent.logicalKey] for more information.', ' /// ');
final String firstComment = wrapString('Represents the logical "${entry.commentName}" key on the keyboard.');
final String otherComments = wrapString('See the function [RawKeyEvent.logicalKey] for more information.');
if (entry.keyLabel == null) {
definitions.write('''
$comment static const LogicalKeyboardKey ${entry.constantName} = LogicalKeyboardKey(${toHex(entry.flutterId, digits: 11)}, debugName: kReleaseMode ? null : '${entry.commentName}');
$firstComment ///
$otherComments static const LogicalKeyboardKey ${entry.constantName} = LogicalKeyboardKey(${toHex(entry.flutterId, digits: 11)}, debugName: kReleaseMode ? null : '${entry.commentName}');
''');
} else {
definitions.write('''
$comment static const LogicalKeyboardKey ${entry.constantName} = LogicalKeyboardKey(${toHex(entry.flutterId, digits: 11)}, keyLabel: ${escapeLabel(entry.keyLabel)}, debugName: kReleaseMode ? null : '${entry.commentName}');
$firstComment ///
$otherComments static const LogicalKeyboardKey ${entry.constantName} = LogicalKeyboardKey(${toHex(entry.flutterId, digits: 11)}, keyLabel: ${escapeLabel(entry.keyLabel)}, debugName: kReleaseMode ? null : '${entry.commentName}');
''');
}
}

View File

@@ -193,7 +193,7 @@ class KeyData {
/// A single entry in the key data structure.
///
/// Can be read from JSON with the [Key..fromJsonMapEntry] constructor, or
/// Can be read from JSON with the [Key.fromJsonMapEntry] constructor, or
/// written with the [toJson] method.
class Key {
/// Creates a single key entry from available data.