Unverified Commit 3ef32f04 authored by Greg Spencer's avatar Greg Spencer Committed by GitHub

Change some usage of RawKeyEvent to KeyEvent in preparation for deprecation (#136420)

## Description

This converts some usages of `RawKeyEvent` to `KeyEvent` to prepare the repo for deprecation of `RawKeyEvent`, and swaps out the `raw_keyboard.dart` manual test for `hardware_keyboard.dart`.

## Related Issues
 - https://github.com/flutter/flutter/issues/136419

## Tests
 - Just refactoring code, no semantic changes.
parent 33db5d7a
......@@ -91,13 +91,14 @@ class _FocusDemoState extends State<FocusDemo> {
super.dispose();
}
KeyEventResult _handleKeyPress(FocusNode node, RawKeyEvent event) {
if (event is RawKeyDownEvent) {
KeyEventResult _handleKeyPress(FocusNode node, KeyEvent event) {
if (event is KeyDownEvent) {
print('Scope got key event: ${event.logicalKey}, $node');
print('Keys down: ${RawKeyboard.instance.keysPressed}');
print('Keys down: ${HardwareKeyboard.instance.logicalKeysPressed}');
if (event.logicalKey == LogicalKeyboardKey.tab) {
debugDumpFocusTree();
if (event.isShiftPressed) {
if (HardwareKeyboard.instance.logicalKeysPressed.contains(LogicalKeyboardKey.shiftLeft)
|| HardwareKeyboard.instance.logicalKeysPressed.contains(LogicalKeyboardKey.shiftRight)) {
print('Moving to previous.');
node.previousFocus();
return KeyEventResult.handled;
......@@ -135,7 +136,7 @@ class _FocusDemoState extends State<FocusDemo> {
policy: ReadingOrderTraversalPolicy(),
child: FocusScope(
debugLabel: 'Scope',
onKey: _handleKeyPress,
onKeyEvent: _handleKeyPress,
autofocus: true,
child: DefaultTextStyle(
style: textTheme.headlineMedium!,
......
......@@ -13,22 +13,22 @@ void main() {
title: const Text('Hardware Key Demo'),
),
body: const Center(
child: RawKeyboardDemo(),
child: HardwareKeyboardDemo(),
),
),
));
}
class RawKeyboardDemo extends StatefulWidget {
const RawKeyboardDemo({super.key});
class HardwareKeyboardDemo extends StatefulWidget {
const HardwareKeyboardDemo({super.key});
@override
State<RawKeyboardDemo> createState() => _HardwareKeyDemoState();
State<HardwareKeyboardDemo> createState() => _HardwareKeyboardDemoState();
}
class _HardwareKeyDemoState extends State<RawKeyboardDemo> {
class _HardwareKeyboardDemoState extends State<HardwareKeyboardDemo> {
final FocusNode _focusNode = FocusNode();
RawKeyEvent? _event;
KeyEvent? _event;
@override
void dispose() {
......@@ -36,27 +36,19 @@ class _HardwareKeyDemoState extends State<RawKeyboardDemo> {
super.dispose();
}
KeyEventResult _handleKeyEvent(FocusNode node, RawKeyEvent event) {
KeyEventResult _handleKeyEvent(FocusNode node, KeyEvent event) {
setState(() {
_event = event;
});
return KeyEventResult.ignored;
}
String _asHex(int value) => '0x${value.toRadixString(16)}';
String _getEnumName(dynamic enumItem) {
final String name = '$enumItem';
final int index = name.indexOf('.');
return index == -1 ? name : name.substring(index + 1);
}
@override
Widget build(BuildContext context) {
final TextTheme textTheme = Theme.of(context).textTheme;
return Focus(
focusNode: _focusNode,
onKey: _handleKeyEvent,
onKeyEvent: _handleKeyEvent,
autofocus: true,
child: AnimatedBuilder(
animation: _focusNode,
......@@ -74,67 +66,17 @@ class _HardwareKeyDemoState extends State<RawKeyboardDemo> {
return Text('Press a key', style: textTheme.headlineMedium);
}
final RawKeyEventData? data = _event?.data;
final String? modifierList = data?.modifiersPressed.keys.map<String>(_getEnumName).join(', ').replaceAll('Modifier', '');
final List<Widget> dataText = <Widget>[
Text('${_event.runtimeType}'),
if (_event?.character?.isNotEmpty ?? false) Text('character produced: "${_event?.character}"'),
Text('modifiers set: $modifierList'),
];
if (data is RawKeyEventDataAndroid) {
const int combiningCharacterMask = 0x7fffffff;
final String codePointChar = String.fromCharCode(combiningCharacterMask & data.codePoint);
dataText.add(Text('codePoint: ${data.codePoint} (${_asHex(data.codePoint)}: $codePointChar)'));
final String plainCodePointChar = String.fromCharCode(combiningCharacterMask & data.plainCodePoint);
dataText.add(Text('plainCodePoint: ${data.plainCodePoint} (${_asHex(data.plainCodePoint)}: $plainCodePointChar)'));
dataText.add(Text('keyCode: ${data.keyCode} (${_asHex(data.keyCode)})'));
dataText.add(Text('scanCode: ${data.scanCode} (${_asHex(data.scanCode)})'));
dataText.add(Text('metaState: ${data.metaState} (${_asHex(data.metaState)})'));
dataText.add(Text('source: ${data.eventSource} (${_asHex(data.eventSource)})'));
dataText.add(Text('vendorId: ${data.vendorId} (${_asHex(data.vendorId)})'));
dataText.add(Text('productId: ${data.productId} (${_asHex(data.productId)})'));
dataText.add(Text('flags: ${data.flags} (${_asHex(data.flags)})'));
dataText.add(Text('repeatCount: ${data.repeatCount} (${_asHex(data.repeatCount)})'));
} else if (data is RawKeyEventDataFuchsia) {
dataText.add(Text('codePoint: ${data.codePoint} (${_asHex(data.codePoint)})'));
dataText.add(Text('hidUsage: ${data.hidUsage} (${_asHex(data.hidUsage)})'));
dataText.add(Text('modifiers: ${data.modifiers} (${_asHex(data.modifiers)})'));
} else if (data is RawKeyEventDataMacOs) {
dataText.add(Text('keyCode: ${data.keyCode} (${_asHex(data.keyCode)})'));
dataText.add(Text('characters: ${data.characters}'));
dataText.add(Text('charactersIgnoringModifiers: ${data.charactersIgnoringModifiers}'));
dataText.add(Text('modifiers: ${data.modifiers} (${_asHex(data.modifiers)})'));
} else if (data is RawKeyEventDataLinux) {
dataText.add(Text('keyCode: ${data.keyCode} (${_asHex(data.keyCode)})'));
dataText.add(Text('scanCode: ${data.scanCode}'));
dataText.add(Text('unicodeScalarValues: ${data.unicodeScalarValues}'));
dataText.add(Text('modifiers: ${data.modifiers} (${_asHex(data.modifiers)})'));
} else if (data is RawKeyEventDataWindows) {
dataText.add(Text('keyCode: ${data.keyCode} (${_asHex(data.keyCode)})'));
dataText.add(Text('scanCode: ${data.scanCode}'));
dataText.add(Text('characterCodePoint: ${data.characterCodePoint}'));
dataText.add(Text('modifiers: ${data.modifiers} (${_asHex(data.modifiers)})'));
} else if (data is RawKeyEventDataWeb) {
dataText.add(Text('key: ${data.key}'));
dataText.add(Text('code: ${data.code}'));
dataText.add(Text('metaState: ${data.metaState} (${_asHex(data.metaState)})'));
}
dataText.add(Text('logical: ${_event?.logicalKey}'));
dataText.add(Text('physical: ${_event?.physicalKey}'));
if (_event?.character != null) {
dataText.add(Text('character: ${_event?.character}'));
}
for (final ModifierKey modifier in data!.modifiersPressed.keys) {
for (final KeyboardSide side in KeyboardSide.values) {
if (data.isModifierPressed(modifier, side: side)) {
dataText.add(
Text('${_getEnumName(side)} ${_getEnumName(modifier).replaceAll('Modifier', '')} pressed'),
);
}
}
}
final List<String> pressed = <String>['Pressed:'];
for (final LogicalKeyboardKey key in RawKeyboard.instance.keysPressed) {
for (final LogicalKeyboardKey key in HardwareKeyboard.instance.logicalKeysPressed) {
pressed.add(key.debugName!);
}
dataText.add(Text(pressed.join(' ')));
......
......@@ -46,7 +46,7 @@ class _MyKeyExampleState extends State<MyKeyExample> {
// Handles the key events from the Focus widget and updates the
// _message.
KeyEventResult _handleKeyEvent(FocusNode node, RawKeyEvent event) {
KeyEventResult _handleKeyEvent(FocusNode node, KeyEvent event) {
setState(() {
if (event.logicalKey == LogicalKeyboardKey.keyQ) {
_message = 'Pressed the "Q" key!';
......@@ -73,7 +73,7 @@ class _MyKeyExampleState extends State<MyKeyExample> {
style: textTheme.headlineMedium!,
child: Focus(
focusNode: _focusNode,
onKey: _handleKeyEvent,
onKeyEvent: _handleKeyEvent,
child: ListenableBuilder(
listenable: _focusNode,
builder: (BuildContext context, Widget? child) {
......
......@@ -46,7 +46,7 @@ class _MyPhysicalKeyExampleState extends State<MyPhysicalKeyExample> {
// Handles the key events from the RawKeyboardListener and update the
// _message.
KeyEventResult _handleKeyEvent(FocusNode node, RawKeyEvent event) {
KeyEventResult _handleKeyEvent(FocusNode node, KeyEvent event) {
setState(() {
if (event.physicalKey == PhysicalKeyboardKey.keyA) {
_message = 'Pressed the key next to CAPS LOCK!';
......@@ -73,7 +73,7 @@ class _MyPhysicalKeyExampleState extends State<MyPhysicalKeyExample> {
style: textTheme.headlineMedium!,
child: Focus(
focusNode: _focusNode,
onKey: _handleKeyEvent,
onKeyEvent: _handleKeyEvent,
child: ListenableBuilder(
listenable: _focusNode,
builder: (BuildContext context, Widget? child) {
......
......@@ -41,7 +41,7 @@ class _ColorfulButtonState extends State<ColorfulButton> {
super.initState();
_node = FocusNode(debugLabel: 'Button');
_node.addListener(_handleFocusChange);
_nodeAttachment = _node.attach(context, onKey: _handleKeyPress);
_nodeAttachment = _node.attach(context, onKeyEvent: _handleKeyPress);
}
void _handleFocusChange() {
......@@ -52,8 +52,8 @@ class _ColorfulButtonState extends State<ColorfulButton> {
}
}
KeyEventResult _handleKeyPress(FocusNode node, RawKeyEvent event) {
if (event is RawKeyDownEvent) {
KeyEventResult _handleKeyPress(FocusNode node, KeyEvent event) {
if (event is KeyDownEvent) {
debugPrint('Focus node ${node.debugLabel} got key event: ${event.logicalKey}');
if (event.logicalKey == LogicalKeyboardKey.keyR) {
debugPrint('Changing color to red.');
......
......@@ -33,8 +33,8 @@ class FocusExample extends StatefulWidget {
class _FocusExampleState extends State<FocusExample> {
Color _color = Colors.white;
KeyEventResult _handleKeyPress(FocusNode node, RawKeyEvent event) {
if (event is RawKeyDownEvent) {
KeyEventResult _handleKeyPress(FocusNode node, KeyEvent event) {
if (event is KeyDownEvent) {
debugPrint('Focus node ${node.debugLabel} got key event: ${event.logicalKey}');
if (event.logicalKey == LogicalKeyboardKey.keyR) {
debugPrint('Changing color to red.');
......@@ -68,7 +68,7 @@ class _FocusExampleState extends State<FocusExample> {
child: DefaultTextStyle(
style: textTheme.headlineMedium!,
child: Focus(
onKey: _handleKeyPress,
onKeyEvent: _handleKeyPress,
debugLabel: 'Button',
child: Builder(
builder: (BuildContext context) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment