Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement a PressKey action #153

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions packages/fluttium/lib/src/actions/press_key.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import 'dart:ui';

import 'package:flutter/services.dart';
import 'package:fluttium/fluttium.dart';

/// {@template press_key}
/// TODO: docs
/// {@endtemplate}
class PressKey extends Action {
/// {@macro press_key}
const PressKey({
required this.key,
this.downFor = 1,
});

/// The key that is being pressed.
final String key;

/// For how long the key will be down.
final int downFor;

@override
Future<bool> execute(Tester tester) async {
final logicalKey = LogicalKeyboardKey.knownLogicalKeys.firstWhere((key) {
return key.debugName == this.key;
});

final physicalKey = PhysicalKeyboardKey.knownPhysicalKeys.firstWhere((key) {
return logicalKey.debugName == key.debugName;
});

tester.keyEventManager.handleKeyData(
KeyData(
type: KeyEventType.down,
physical: physicalKey.usbHidUsage,
logical: logicalKey.keyId,
timeStamp: Duration.zero,
character: null,
synthesized: false,
),
);

await tester.pump(duration: Duration(milliseconds: downFor));

tester.keyEventManager.handleKeyData(
KeyData(
type: KeyEventType.up,
physical: physicalKey.usbHidUsage,
logical: logicalKey.keyId,
timeStamp: Duration.zero,
character: null,
synthesized: false,
),
);

return true;
}

@override
String description() => 'Press key: $key';
}