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> { ...@@ -91,13 +91,14 @@ class _FocusDemoState extends State<FocusDemo> {
super.dispose(); super.dispose();
} }
KeyEventResult _handleKeyPress(FocusNode node, RawKeyEvent event) { KeyEventResult _handleKeyPress(FocusNode node, KeyEvent event) {
if (event is RawKeyDownEvent) { if (event is KeyDownEvent) {
print('Scope got key event: ${event.logicalKey}, $node'); 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) { if (event.logicalKey == LogicalKeyboardKey.tab) {
debugDumpFocusTree(); debugDumpFocusTree();
if (event.isShiftPressed) { if (HardwareKeyboard.instance.logicalKeysPressed.contains(LogicalKeyboardKey.shiftLeft)
|| HardwareKeyboard.instance.logicalKeysPressed.contains(LogicalKeyboardKey.shiftRight)) {
print('Moving to previous.'); print('Moving to previous.');
node.previousFocus(); node.previousFocus();
return KeyEventResult.handled; return KeyEventResult.handled;
...@@ -135,7 +136,7 @@ class _FocusDemoState extends State<FocusDemo> { ...@@ -135,7 +136,7 @@ class _FocusDemoState extends State<FocusDemo> {
policy: ReadingOrderTraversalPolicy(), policy: ReadingOrderTraversalPolicy(),
child: FocusScope( child: FocusScope(
debugLabel: 'Scope', debugLabel: 'Scope',
onKey: _handleKeyPress, onKeyEvent: _handleKeyPress,
autofocus: true, autofocus: true,
child: DefaultTextStyle( child: DefaultTextStyle(
style: textTheme.headlineMedium!, style: textTheme.headlineMedium!,
......
...@@ -13,22 +13,22 @@ void main() { ...@@ -13,22 +13,22 @@ void main() {
title: const Text('Hardware Key Demo'), title: const Text('Hardware Key Demo'),
), ),
body: const Center( body: const Center(
child: RawKeyboardDemo(), child: HardwareKeyboardDemo(),
), ),
), ),
)); ));
} }
class RawKeyboardDemo extends StatefulWidget { class HardwareKeyboardDemo extends StatefulWidget {
const RawKeyboardDemo({super.key}); const HardwareKeyboardDemo({super.key});
@override @override
State<RawKeyboardDemo> createState() => _HardwareKeyDemoState(); State<HardwareKeyboardDemo> createState() => _HardwareKeyboardDemoState();
} }
class _HardwareKeyDemoState extends State<RawKeyboardDemo> { class _HardwareKeyboardDemoState extends State<HardwareKeyboardDemo> {
final FocusNode _focusNode = FocusNode(); final FocusNode _focusNode = FocusNode();
RawKeyEvent? _event; KeyEvent? _event;
@override @override
void dispose() { void dispose() {
...@@ -36,27 +36,19 @@ class _HardwareKeyDemoState extends State<RawKeyboardDemo> { ...@@ -36,27 +36,19 @@ class _HardwareKeyDemoState extends State<RawKeyboardDemo> {
super.dispose(); super.dispose();
} }
KeyEventResult _handleKeyEvent(FocusNode node, RawKeyEvent event) { KeyEventResult _handleKeyEvent(FocusNode node, KeyEvent event) {
setState(() { setState(() {
_event = event; _event = event;
}); });
return KeyEventResult.ignored; 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final TextTheme textTheme = Theme.of(context).textTheme; final TextTheme textTheme = Theme.of(context).textTheme;
return Focus( return Focus(
focusNode: _focusNode, focusNode: _focusNode,
onKey: _handleKeyEvent, onKeyEvent: _handleKeyEvent,
autofocus: true, autofocus: true,
child: AnimatedBuilder( child: AnimatedBuilder(
animation: _focusNode, animation: _focusNode,
...@@ -74,67 +66,17 @@ class _HardwareKeyDemoState extends State<RawKeyboardDemo> { ...@@ -74,67 +66,17 @@ class _HardwareKeyDemoState extends State<RawKeyboardDemo> {
return Text('Press a key', style: textTheme.headlineMedium); 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>[ final List<Widget> dataText = <Widget>[
Text('${_event.runtimeType}'), Text('${_event.runtimeType}'),
if (_event?.character?.isNotEmpty ?? false) Text('character produced: "${_event?.character}"'), 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('logical: ${_event?.logicalKey}'));
dataText.add(Text('physical: ${_event?.physicalKey}')); dataText.add(Text('physical: ${_event?.physicalKey}'));
if (_event?.character != null) { if (_event?.character != null) {
dataText.add(Text('character: ${_event?.character}')); 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:']; 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!); pressed.add(key.debugName!);
} }
dataText.add(Text(pressed.join(' '))); dataText.add(Text(pressed.join(' ')));
......
...@@ -46,7 +46,7 @@ class _MyKeyExampleState extends State<MyKeyExample> { ...@@ -46,7 +46,7 @@ class _MyKeyExampleState extends State<MyKeyExample> {
// Handles the key events from the Focus widget and updates the // Handles the key events from the Focus widget and updates the
// _message. // _message.
KeyEventResult _handleKeyEvent(FocusNode node, RawKeyEvent event) { KeyEventResult _handleKeyEvent(FocusNode node, KeyEvent event) {
setState(() { setState(() {
if (event.logicalKey == LogicalKeyboardKey.keyQ) { if (event.logicalKey == LogicalKeyboardKey.keyQ) {
_message = 'Pressed the "Q" key!'; _message = 'Pressed the "Q" key!';
...@@ -73,7 +73,7 @@ class _MyKeyExampleState extends State<MyKeyExample> { ...@@ -73,7 +73,7 @@ class _MyKeyExampleState extends State<MyKeyExample> {
style: textTheme.headlineMedium!, style: textTheme.headlineMedium!,
child: Focus( child: Focus(
focusNode: _focusNode, focusNode: _focusNode,
onKey: _handleKeyEvent, onKeyEvent: _handleKeyEvent,
child: ListenableBuilder( child: ListenableBuilder(
listenable: _focusNode, listenable: _focusNode,
builder: (BuildContext context, Widget? child) { builder: (BuildContext context, Widget? child) {
......
...@@ -46,7 +46,7 @@ class _MyPhysicalKeyExampleState extends State<MyPhysicalKeyExample> { ...@@ -46,7 +46,7 @@ class _MyPhysicalKeyExampleState extends State<MyPhysicalKeyExample> {
// Handles the key events from the RawKeyboardListener and update the // Handles the key events from the RawKeyboardListener and update the
// _message. // _message.
KeyEventResult _handleKeyEvent(FocusNode node, RawKeyEvent event) { KeyEventResult _handleKeyEvent(FocusNode node, KeyEvent event) {
setState(() { setState(() {
if (event.physicalKey == PhysicalKeyboardKey.keyA) { if (event.physicalKey == PhysicalKeyboardKey.keyA) {
_message = 'Pressed the key next to CAPS LOCK!'; _message = 'Pressed the key next to CAPS LOCK!';
...@@ -73,7 +73,7 @@ class _MyPhysicalKeyExampleState extends State<MyPhysicalKeyExample> { ...@@ -73,7 +73,7 @@ class _MyPhysicalKeyExampleState extends State<MyPhysicalKeyExample> {
style: textTheme.headlineMedium!, style: textTheme.headlineMedium!,
child: Focus( child: Focus(
focusNode: _focusNode, focusNode: _focusNode,
onKey: _handleKeyEvent, onKeyEvent: _handleKeyEvent,
child: ListenableBuilder( child: ListenableBuilder(
listenable: _focusNode, listenable: _focusNode,
builder: (BuildContext context, Widget? child) { builder: (BuildContext context, Widget? child) {
......
...@@ -41,7 +41,7 @@ class _ColorfulButtonState extends State<ColorfulButton> { ...@@ -41,7 +41,7 @@ class _ColorfulButtonState extends State<ColorfulButton> {
super.initState(); super.initState();
_node = FocusNode(debugLabel: 'Button'); _node = FocusNode(debugLabel: 'Button');
_node.addListener(_handleFocusChange); _node.addListener(_handleFocusChange);
_nodeAttachment = _node.attach(context, onKey: _handleKeyPress); _nodeAttachment = _node.attach(context, onKeyEvent: _handleKeyPress);
} }
void _handleFocusChange() { void _handleFocusChange() {
...@@ -52,8 +52,8 @@ class _ColorfulButtonState extends State<ColorfulButton> { ...@@ -52,8 +52,8 @@ class _ColorfulButtonState extends State<ColorfulButton> {
} }
} }
KeyEventResult _handleKeyPress(FocusNode node, RawKeyEvent event) { KeyEventResult _handleKeyPress(FocusNode node, KeyEvent event) {
if (event is RawKeyDownEvent) { if (event is KeyDownEvent) {
debugPrint('Focus node ${node.debugLabel} got key event: ${event.logicalKey}'); debugPrint('Focus node ${node.debugLabel} got key event: ${event.logicalKey}');
if (event.logicalKey == LogicalKeyboardKey.keyR) { if (event.logicalKey == LogicalKeyboardKey.keyR) {
debugPrint('Changing color to red.'); debugPrint('Changing color to red.');
......
...@@ -33,8 +33,8 @@ class FocusExample extends StatefulWidget { ...@@ -33,8 +33,8 @@ class FocusExample extends StatefulWidget {
class _FocusExampleState extends State<FocusExample> { class _FocusExampleState extends State<FocusExample> {
Color _color = Colors.white; Color _color = Colors.white;
KeyEventResult _handleKeyPress(FocusNode node, RawKeyEvent event) { KeyEventResult _handleKeyPress(FocusNode node, KeyEvent event) {
if (event is RawKeyDownEvent) { if (event is KeyDownEvent) {
debugPrint('Focus node ${node.debugLabel} got key event: ${event.logicalKey}'); debugPrint('Focus node ${node.debugLabel} got key event: ${event.logicalKey}');
if (event.logicalKey == LogicalKeyboardKey.keyR) { if (event.logicalKey == LogicalKeyboardKey.keyR) {
debugPrint('Changing color to red.'); debugPrint('Changing color to red.');
...@@ -68,7 +68,7 @@ class _FocusExampleState extends State<FocusExample> { ...@@ -68,7 +68,7 @@ class _FocusExampleState extends State<FocusExample> {
child: DefaultTextStyle( child: DefaultTextStyle(
style: textTheme.headlineMedium!, style: textTheme.headlineMedium!,
child: Focus( child: Focus(
onKey: _handleKeyPress, onKeyEvent: _handleKeyPress,
debugLabel: 'Button', debugLabel: 'Button',
child: Builder( child: Builder(
builder: (BuildContext context) { 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