Update key examples to use Focus widgets instead of RawKeyboardListener (#101537)
This updates the examples for PhysicalKeyboardKey and LogicalKeyboardKey to use Focus widgets that handle the keys instead of using RawKeyboardListener, since that usually leads people down the wrong path. Updated the See Also and added tests as well. Also exposed the `physicalKey` attribute for `tester.sendKeyEvent`.
This commit is contained in:
@@ -151,6 +151,29 @@ void main() {
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, isEmpty);
|
||||
events.clear();
|
||||
|
||||
// Key press keyA with physical keyQ
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.keyA, physicalKey: PhysicalKeyboardKey.keyQ);
|
||||
expect(events.length, 1);
|
||||
_verifyKeyEvent<KeyDownEvent>(events[0], PhysicalKeyboardKey.keyQ, LogicalKeyboardKey.keyA, 'a');
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, equals(<PhysicalKeyboardKey>{PhysicalKeyboardKey.keyQ}));
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, equals(<LogicalKeyboardKey>{LogicalKeyboardKey.keyA}));
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, isEmpty);
|
||||
events.clear();
|
||||
|
||||
await tester.sendKeyRepeatEvent(LogicalKeyboardKey.keyA, physicalKey: PhysicalKeyboardKey.keyQ);
|
||||
_verifyKeyEvent<KeyRepeatEvent>(events[0], PhysicalKeyboardKey.keyQ, LogicalKeyboardKey.keyA, 'a');
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, equals(<PhysicalKeyboardKey>{PhysicalKeyboardKey.keyQ}));
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, equals(<LogicalKeyboardKey>{LogicalKeyboardKey.keyA}));
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, isEmpty);
|
||||
events.clear();
|
||||
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.keyA, physicalKey: PhysicalKeyboardKey.keyQ);
|
||||
_verifyKeyEvent<KeyUpEvent>(events[0], PhysicalKeyboardKey.keyQ, LogicalKeyboardKey.keyA, null);
|
||||
expect(HardwareKeyboard.instance.physicalKeysPressed, isEmpty);
|
||||
expect(HardwareKeyboard.instance.logicalKeysPressed, isEmpty);
|
||||
expect(HardwareKeyboard.instance.lockModesEnabled, isEmpty);
|
||||
events.clear();
|
||||
|
||||
// Key press numpad1
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.numpad1);
|
||||
_verifyKeyEvent<KeyDownEvent>(events[0], PhysicalKeyboardKey.numpad1, LogicalKeyboardKey.numpad1, null);
|
||||
|
||||
Reference in New Issue
Block a user