Character activator (#81807)
This commit is contained in:
@@ -1021,10 +1021,10 @@ abstract class WidgetController {
|
||||
///
|
||||
/// - [sendKeyUpEvent] to simulate the corresponding key up event.
|
||||
/// - [sendKeyEvent] to simulate both the key up and key down in the same call.
|
||||
Future<bool> sendKeyDownEvent(LogicalKeyboardKey key, { String platform = _defaultPlatform }) async {
|
||||
Future<bool> sendKeyDownEvent(LogicalKeyboardKey key, { String? character, String platform = _defaultPlatform }) async {
|
||||
assert(platform != null);
|
||||
// Internally wrapped in async guard.
|
||||
return simulateKeyDownEvent(key, platform: platform);
|
||||
return simulateKeyDownEvent(key, character: character, platform: platform);
|
||||
}
|
||||
|
||||
/// Simulates sending a physical key up event through the system channel.
|
||||
|
||||
@@ -194,6 +194,7 @@ class KeyEventSimulator {
|
||||
required String platform,
|
||||
bool isDown = true,
|
||||
PhysicalKeyboardKey? physicalKey,
|
||||
String? character,
|
||||
}) {
|
||||
assert(_osIsSupported(platform), 'Platform $platform not supported for key simulation');
|
||||
|
||||
@@ -211,27 +212,31 @@ class KeyEventSimulator {
|
||||
'keymap': platform,
|
||||
};
|
||||
|
||||
if (kIsWeb) {
|
||||
final String resultCharacter = character ?? _keyLabel(key);
|
||||
void assignWeb() {
|
||||
result['code'] = _getWebKeyCode(key);
|
||||
result['key'] = _keyLabel(key);
|
||||
result['key'] = resultCharacter;
|
||||
result['metaState'] = _getWebModifierFlags(key, isDown);
|
||||
}
|
||||
if (kIsWeb) {
|
||||
assignWeb();
|
||||
return result;
|
||||
}
|
||||
|
||||
switch (platform) {
|
||||
case 'android':
|
||||
result['keyCode'] = keyCode;
|
||||
if (_keyLabel(key).isNotEmpty) {
|
||||
result['codePoint'] = _keyLabel(key).codeUnitAt(0);
|
||||
result['character'] = _keyLabel(key);
|
||||
if (resultCharacter.isNotEmpty) {
|
||||
result['codePoint'] = resultCharacter.codeUnitAt(0);
|
||||
result['character'] = resultCharacter;
|
||||
}
|
||||
result['scanCode'] = scanCode;
|
||||
result['metaState'] = _getAndroidModifierFlags(key, isDown);
|
||||
break;
|
||||
case 'fuchsia':
|
||||
result['hidUsage'] = physicalKey.usbHidUsage;
|
||||
if (_keyLabel(key).isNotEmpty) {
|
||||
result['codePoint'] = _keyLabel(key).codeUnitAt(0);
|
||||
if (resultCharacter.isNotEmpty) {
|
||||
result['codePoint'] = resultCharacter.codeUnitAt(0);
|
||||
}
|
||||
result['modifiers'] = _getFuchsiaModifierFlags(key, isDown);
|
||||
break;
|
||||
@@ -240,34 +245,33 @@ class KeyEventSimulator {
|
||||
result['keyCode'] = keyCode;
|
||||
result['scanCode'] = scanCode;
|
||||
result['modifiers'] = _getGlfwModifierFlags(key, isDown);
|
||||
result['unicodeScalarValues'] = _keyLabel(key).isNotEmpty ? _keyLabel(key).codeUnitAt(0) : 0;
|
||||
result['unicodeScalarValues'] = resultCharacter.isNotEmpty ? resultCharacter.codeUnitAt(0) : 0;
|
||||
break;
|
||||
case 'macos':
|
||||
result['keyCode'] = scanCode;
|
||||
if (_keyLabel(key).isNotEmpty) {
|
||||
result['characters'] = _keyLabel(key);
|
||||
result['charactersIgnoringModifiers'] = _keyLabel(key);
|
||||
if (resultCharacter.isNotEmpty) {
|
||||
result['characters'] = resultCharacter;
|
||||
result['charactersIgnoringModifiers'] = resultCharacter;
|
||||
}
|
||||
result['modifiers'] = _getMacOsModifierFlags(key, isDown);
|
||||
break;
|
||||
case 'ios':
|
||||
result['keyCode'] = scanCode;
|
||||
result['characters'] = _keyLabel(key);
|
||||
result['charactersIgnoringModifiers'] = _keyLabel(key);
|
||||
result['characters'] = resultCharacter;
|
||||
result['charactersIgnoringModifiers'] = resultCharacter;
|
||||
result['modifiers'] = _getIOSModifierFlags(key, isDown);
|
||||
break;
|
||||
case 'web':
|
||||
result['code'] = _getWebKeyCode(key);
|
||||
result['key'] = _keyLabel(key);
|
||||
result['metaState'] = _getWebModifierFlags(key, isDown);
|
||||
break;
|
||||
case 'windows':
|
||||
result['keyCode'] = keyCode;
|
||||
result['scanCode'] = scanCode;
|
||||
if (_keyLabel(key).isNotEmpty) {
|
||||
result['characterCodePoint'] = _keyLabel(key).codeUnitAt(0);
|
||||
if (resultCharacter.isNotEmpty) {
|
||||
result['characterCodePoint'] = resultCharacter.codeUnitAt(0);
|
||||
}
|
||||
result['modifiers'] = _getWindowsModifierFlags(key, isDown);
|
||||
break;
|
||||
case 'web':
|
||||
assignWeb();
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -631,12 +635,12 @@ class KeyEventSimulator {
|
||||
/// See also:
|
||||
///
|
||||
/// - [simulateKeyUpEvent] to simulate the corresponding key up event.
|
||||
static Future<bool> simulateKeyDownEvent(LogicalKeyboardKey key, {String? platform, PhysicalKeyboardKey? physicalKey}) async {
|
||||
static Future<bool> simulateKeyDownEvent(LogicalKeyboardKey key, {String? platform, PhysicalKeyboardKey? physicalKey, String? character}) async {
|
||||
return TestAsyncUtils.guard<bool>(() async {
|
||||
platform ??= Platform.operatingSystem;
|
||||
assert(_osIsSupported(platform!), 'Platform $platform not supported for key simulation');
|
||||
|
||||
final Map<String, dynamic> data = getKeyData(key, platform: platform!, isDown: true, physicalKey: physicalKey);
|
||||
final Map<String, dynamic> data = getKeyData(key, platform: platform!, isDown: true, physicalKey: physicalKey, character: character);
|
||||
bool result = false;
|
||||
await ServicesBinding.instance!.defaultBinaryMessenger.handlePlatformMessage(
|
||||
SystemChannels.keyEvent.name,
|
||||
@@ -715,8 +719,8 @@ class KeyEventSimulator {
|
||||
/// See also:
|
||||
///
|
||||
/// - [simulateKeyUpEvent] to simulate the corresponding key up event.
|
||||
Future<bool> simulateKeyDownEvent(LogicalKeyboardKey key, {String? platform, PhysicalKeyboardKey? physicalKey}) {
|
||||
return KeyEventSimulator.simulateKeyDownEvent(key, platform: platform, physicalKey: physicalKey);
|
||||
Future<bool> simulateKeyDownEvent(LogicalKeyboardKey key, {String? platform, PhysicalKeyboardKey? physicalKey, String? character}) {
|
||||
return KeyEventSimulator.simulateKeyDownEvent(key, platform: platform, physicalKey: physicalKey, character: character);
|
||||
}
|
||||
|
||||
/// Simulates sending a hardware key up event through the system channel.
|
||||
|
||||
Reference in New Issue
Block a user