simulateKeyDownEvent static method
- LogicalKeyboardKey key, {
- String? platform,
- PhysicalKeyboardKey? physicalKey,
- String? character,
Simulates sending a hardware key down event.
This only simulates key presses coming from a physical keyboard, not from a soft keyboard.
Specify platform
as one of the platforms allowed in
Platform.operatingSystem to make the event appear to be from that type of
system. Defaults to "web" on web, and the operating system name based on
defaultTargetPlatform everywhere else.
Keys that are down when the test completes are cleared after each test.
Returns true if the key event was handled by the framework.
See also:
- simulateKeyUpEvent to simulate the corresponding key up event.
Implementation
static Future<bool> simulateKeyDownEvent(
LogicalKeyboardKey key, {
String? platform,
PhysicalKeyboardKey? physicalKey,
String? character,
}) async {
Future<bool> simulateByRawEvent() {
return _simulateKeyEventByRawEvent(() {
platform ??= _defaultPlatform;
return getKeyData(key, platform: platform!, physicalKey: physicalKey, character: character);
});
}
switch (_transitMode) {
case KeyDataTransitMode.rawKeyData:
return simulateByRawEvent();
case KeyDataTransitMode.keyDataThenRawKeyData:
final LogicalKeyboardKey logicalKey = _getKeySynonym(key);
final bool resultByKeyEvent = ServicesBinding.instance.keyEventManager.handleKeyData(
ui.KeyData(
type: ui.KeyEventType.down,
physical: (physicalKey ?? _findPhysicalKey(logicalKey)).usbHidUsage,
logical: logicalKey.keyId,
timeStamp: Duration.zero,
character: character ?? _keyLabel(key),
synthesized: false,
),
);
return (await simulateByRawEvent()) || resultByKeyEvent;
}
}