Skip to content

Commit c62cf08

Browse files
committed
debugCharacterLabel
1 parent 10fba6e commit c62cf08

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

‎packages/flutter/lib/src/services/keyboard_key.dart‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,24 @@ class LogicalKeyboardKey extends KeyboardKey {
147147
/// release mode.
148148
String? get debugName => _debugNames[keyId];
149149

150+
/// Returns a character that the key represents without any modifiers.
151+
///
152+
/// For example, for KeyA it returns `a`, and for Digit1 it returns `1`. For
153+
/// non-printable keys, it returns null.
154+
///
155+
/// This method only returns an non-empty value in debug mode, and returns null
156+
/// in release mode.
157+
String? get debugCharacterLabel {
158+
String? result;
159+
assert(() {
160+
if (keyId & ~valueMask == unicodePlane) {
161+
result = String.fromCharCode(keyId);
162+
}
163+
return true;
164+
}());
165+
return result;
166+
}
167+
150168
/// Returns the [LogicalKeyboardKey] constant that matches the given ID, or
151169
/// null, if not found.
152170
static LogicalKeyboardKey? findKeyByKeyId(int keyId) => _knownLogicalKeys[keyId];
@@ -230,6 +248,7 @@ class LogicalKeyboardKey extends KeyboardKey {
230248
super.debugFillProperties(properties);
231249
properties.add(StringProperty('keyId', '0x${keyId.toRadixString(16).padLeft(8, '0')}', showName: true));
232250
properties.add(StringProperty('debugName', debugName, showName: true, defaultValue: null));
251+
properties.add(StringProperty('debugCharacterLabel', debugCharacterLabel, showName: true, defaultValue: null));
233252
}
234253

235254
static final Map<int, LogicalKeyboardKey> _additionalKeyPool = <int, LogicalKeyboardKey>{};

‎packages/flutter/test/services/keyboard_key_test.dart‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ void main() {
1717
});
1818
});
1919
group(LogicalKeyboardKey, () {
20+
test('debugCharacterLabel', () async {
21+
expect(LogicalKeyboardKey.keyA.debugCharacterLabel, 'a');
22+
expect(LogicalKeyboardKey.digit1.debugCharacterLabel, '1');
23+
expect(LogicalKeyboardKey.controlLeft.debugCharacterLabel, null);
24+
});
2025
test('Various classes of keys can be looked up by code', () async {
2126
// Check a letter key
2227
expect(LogicalKeyboardKey.findKeyByKeyId(0x0000000061), equals(LogicalKeyboardKey.keyA));

0 commit comments

Comments
 (0)