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:
Greg Spencer
2022-04-08 12:05:41 -07:00
committed by GitHub
parent cae740b903
commit efe76a5373
7 changed files with 182 additions and 54 deletions

View File

@@ -982,6 +982,14 @@ abstract class WidgetController {
/// else. Must not be null. Some platforms (e.g. Windows, iOS) are not yet
/// supported.
///
/// Specify the `physicalKey` for the event to override what is included in
/// the simulated event. If not specified, it uses a default from the US
/// keyboard layout for the corresponding logical `key`.
///
/// Specify the `character` for the event to override what is included in the
/// simulated event. If not specified, it uses a default derived from the
/// logical `key`.
///
/// Whether the event is sent through [RawKeyEvent] or [KeyEvent] is
/// controlled by [debugKeyEventSimulatorTransitModeOverride].
///
@@ -997,11 +1005,16 @@ abstract class WidgetController {
///
/// - [sendKeyDownEvent] to simulate only a key down event.
/// - [sendKeyUpEvent] to simulate only a key up event.
Future<bool> sendKeyEvent(LogicalKeyboardKey key, { String platform = _defaultPlatform }) async {
Future<bool> sendKeyEvent(
LogicalKeyboardKey key, {
String platform = _defaultPlatform,
String? character,
PhysicalKeyboardKey? physicalKey
}) async {
assert(platform != null);
final bool handled = await simulateKeyDownEvent(key, platform: platform);
final bool handled = await simulateKeyDownEvent(key, platform: platform, character: character, physicalKey: physicalKey);
// Internally wrapped in async guard.
await simulateKeyUpEvent(key, platform: platform);
await simulateKeyUpEvent(key, platform: platform, physicalKey: physicalKey);
return handled;
}
@@ -1016,6 +1029,14 @@ abstract class WidgetController {
/// else. Must not be null. Some platforms (e.g. Windows, iOS) are not yet
/// supported.
///
/// Specify the `physicalKey` for the event to override what is included in
/// the simulated event. If not specified, it uses a default from the US
/// keyboard layout for the corresponding logical `key`.
///
/// Specify the `character` for the event to override what is included in the
/// simulated event. If not specified, it uses a default derived from the
/// logical `key`.
///
/// Whether the event is sent through [RawKeyEvent] or [KeyEvent] is
/// controlled by [debugKeyEventSimulatorTransitModeOverride].
///
@@ -1028,10 +1049,15 @@ abstract class WidgetController {
/// - [sendKeyUpEvent] and [sendKeyRepeatEvent] to simulate the corresponding
/// key up and repeat event.
/// - [sendKeyEvent] to simulate both the key up and key down in the same call.
Future<bool> sendKeyDownEvent(LogicalKeyboardKey key, { String? character, String platform = _defaultPlatform }) async {
Future<bool> sendKeyDownEvent(
LogicalKeyboardKey key, {
String platform = _defaultPlatform,
String? character,
PhysicalKeyboardKey? physicalKey
}) async {
assert(platform != null);
// Internally wrapped in async guard.
return simulateKeyDownEvent(key, character: character, platform: platform);
return simulateKeyDownEvent(key, platform: platform, character: character, physicalKey: physicalKey);
}
/// Simulates sending a physical key up event through the system channel.
@@ -1044,6 +1070,10 @@ abstract class WidgetController {
/// that type of system. Defaults to "web" on web, and "android" everywhere
/// else. May not be null.
///
/// Specify the `physicalKey` for the event to override what is included in
/// the simulated event. If not specified, it uses a default from the US
/// keyboard layout for the corresponding logical `key`.
///
/// Whether the event is sent through [RawKeyEvent] or [KeyEvent] is
/// controlled by [debugKeyEventSimulatorTransitModeOverride].
///
@@ -1054,13 +1084,17 @@ abstract class WidgetController {
/// - [sendKeyDownEvent] and [sendKeyRepeatEvent] to simulate the
/// corresponding key down and repeat event.
/// - [sendKeyEvent] to simulate both the key up and key down in the same call.
Future<bool> sendKeyUpEvent(LogicalKeyboardKey key, { String platform = _defaultPlatform }) async {
Future<bool> sendKeyUpEvent(
LogicalKeyboardKey key, {
String platform = _defaultPlatform,
PhysicalKeyboardKey? physicalKey
}) async {
assert(platform != null);
// Internally wrapped in async guard.
return simulateKeyUpEvent(key, platform: platform);
return simulateKeyUpEvent(key, platform: platform, physicalKey: physicalKey);
}
/// Simulates sending a physical key repeat event.
/// Simulates sending a key repeat event from a physical keyboard.
///
/// This only simulates key repeat events coming from a physical keyboard, not
/// from a soft keyboard.
@@ -1070,6 +1104,14 @@ abstract class WidgetController {
/// of system. Defaults to "web" on web, and "android" everywhere else. Must not be
/// null. Some platforms (e.g. Windows, iOS) are not yet supported.
///
/// Specify the `physicalKey` for the event to override what is included in
/// the simulated event. If not specified, it uses a default from the US
/// keyboard layout for the corresponding logical `key`.
///
/// Specify the `character` for the event to override what is included in the
/// simulated event. If not specified, it uses a default derived from the
/// logical `key`.
///
/// Whether the event is sent through [RawKeyEvent] or [KeyEvent] is
/// controlled by [debugKeyEventSimulatorTransitModeOverride]. If through [RawKeyEvent],
/// this method is equivalent to [sendKeyDownEvent].
@@ -1083,10 +1125,15 @@ abstract class WidgetController {
/// - [sendKeyDownEvent] and [sendKeyUpEvent] to simulate the corresponding
/// key down and up event.
/// - [sendKeyEvent] to simulate both the key up and key down in the same call.
Future<bool> sendKeyRepeatEvent(LogicalKeyboardKey key, { String? character, String platform = _defaultPlatform }) async {
Future<bool> sendKeyRepeatEvent(
LogicalKeyboardKey key, {
String platform = _defaultPlatform,
String? character,
PhysicalKeyboardKey? physicalKey
}) async {
assert(platform != null);
// Internally wrapped in async guard.
return simulateKeyRepeatEvent(key, character: character, platform: platform);
return simulateKeyRepeatEvent(key, platform: platform, character: character, physicalKey: physicalKey);
}
/// Returns the rect of the given widget. This is only valid once