Skip to content

Commit

Permalink
chore: Replace LogicalKeyboardKey to PhysicalKeyboardKey
Browse files Browse the repository at this point in the history
  • Loading branch information
lijy91 committed Jan 30, 2024
1 parent 332a7a0 commit cc6621b
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 124 deletions.
60 changes: 20 additions & 40 deletions README-ZH.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# keypress_simulator

[![pub version][pub-image]][pub-url] [![][discord-image]][discord-url] ![][visits-count-image]
[![pub version][pub-image]][pub-url] [![][discord-image]][discord-url]

[pub-image]: https://img.shields.io/pub/v/keypress_simulator.svg
[pub-url]: https://pub.dev/packages/keypress_simulator

[discord-image]: https://img.shields.io/discord/884679008049037342.svg
[discord-url]: https://discord.gg/zPa6EZ2jqb

[visits-count-image]: https://img.shields.io/badge/dynamic/json?label=Visits%20Count&query=value&url=https://api.countapi.xyz/hit/leanflutter.keypress_simulator/visits

这个插件允许 Flutter 桌面应用模拟按键操作。

---
Expand All @@ -21,21 +18,20 @@
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [keypress_simulator](#keypress_simulator)
- [平台支持](#平台支持)
- [快速开始](#快速开始)
- [安装](#安装)
- [用法](#用法)
- [谁在用使用它?](#谁在用使用它)
- [许可证](#许可证)
- [平台支持](#%E5%B9%B3%E5%8F%B0%E6%94%AF%E6%8C%81)
- [快速开始](#%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B)
- [安装](#%E5%AE%89%E8%A3%85)
- [用法](#%E7%94%A8%E6%B3%95)
- [谁在用使用它?](#%E8%B0%81%E5%9C%A8%E7%94%A8%E4%BD%BF%E7%94%A8%E5%AE%83)
- [许可证](#%E8%AE%B8%E5%8F%AF%E8%AF%81)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## 平台支持

| Linux | macOS | Windows |
| :---: | :---: | :-----: |
| | ✔️ | ✔️ |
|| ✔️ | ✔️ |

## 快速开始

Expand All @@ -45,17 +41,7 @@

```yaml
dependencies:
keypress_simulator: ^0.1.0
```
```yaml
dependencies:
keypress_simulator:
git:
url: https://github.com/leanflutter/keypress_simulator.git
ref: main
keypress_simulator: ^0.2.0
```
### 用法
Expand All @@ -66,37 +52,31 @@ import 'package:keypress_simulator/keypress_simulator.dart';
// 1. Simulate pressing ⌘ + C

// 1.1 Simulate key down
await keyPressSimulator.simulateKeyPress(
key: LogicalKeyboardKey.keyC,
await keyPressSimulator.simulateKeyDown(
key: PhysicalKeyboardKey.keyC,
modifiers: [ModifierKey.metaModifier],
);

// 1.2 Simulate key up
await keyPressSimulator.simulateKeyPress(
key: LogicalKeyboardKey.keyC,
await keyPressSimulator.simulateKeyUp(
key: PhysicalKeyboardKey.keyC,
modifiers: [ModifierKey.metaModifier],
keyDown: false,
);

// 2. Simulate long pressing ⌘ + space

// 2.1. Simulate key down
await keyPressSimulator.simulateKeyPress(
key: LogicalKeyboardKey.space,
modifiers: [
ModifierKey.metaModifier,
],
await keyPressSimulator.simulateKeyDown(
key: PhysicalKeyboardKey.space,
modifiers: [ModifierKey.metaModifier],
);

await Future.delayed(const Duration(seconds: 5));

// 2.2. Simulate key up
await keyPressSimulator.simulateKeyPress(
key: LogicalKeyboardKey.space,
modifiers: [
ModifierKey.metaModifier,
],
keyDown: false,
await keyPressSimulator.simulateKeyUp(
key: PhysicalKeyboardKey.space,
modifiers: [ModifierKey.metaModifier],
);
```

Expand Down
52 changes: 16 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# keypress_simulator

[![pub version][pub-image]][pub-url] [![][discord-image]][discord-url] ![][visits-count-image]
[![pub version][pub-image]][pub-url] [![][discord-image]][discord-url]

[pub-image]: https://img.shields.io/pub/v/keypress_simulator.svg
[pub-url]: https://pub.dev/packages/keypress_simulator

[discord-image]: https://img.shields.io/discord/884679008049037342.svg
[discord-url]: https://discord.gg/zPa6EZ2jqb

[visits-count-image]: https://img.shields.io/badge/dynamic/json?label=Visits%20Count&query=value&url=https://api.countapi.xyz/hit/leanflutter.keypress_simulator/visits

This plugin allows Flutter desktop apps to simulate key presses.

---
Expand All @@ -21,21 +18,20 @@ English | [简体中文](./README-ZH.md)
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [keypress_simulator](#keypress_simulator)
- [Platform Support](#platform-support)
- [Quick Start](#quick-start)
- [Installation](#installation)
- [Usage](#usage)
- [Who's using it?](#whos-using-it)
- [License](#license)
- [Platform Support](#platform-support)
- [Quick Start](#quick-start)
- [Installation](#installation)
- [Usage](#usage)
- [Who's using it?](#whos-using-it)
- [License](#license)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Platform Support

| Linux | macOS | Windows |
| :---: | :---: | :-----: |
| | ✔️ | ✔️ |
|| ✔️ | ✔️ |

## Quick Start

Expand All @@ -45,17 +41,7 @@ Add this to your package's pubspec.yaml file:

```yaml
dependencies:
keypress_simulator: ^0.1.0
```
Or
```yaml
dependencies:
keypress_simulator:
git:
url: https://github.com/leanflutter/keypress_simulator.git
ref: main
keypress_simulator: ^0.2.0
```
### Usage
Expand All @@ -66,37 +52,31 @@ import 'package:keypress_simulator/keypress_simulator.dart';
// 1. Simulate pressing ⌘ + C

// 1.1 Simulate key down
await keyPressSimulator.simulateKeyPress(
await keyPressSimulator.simulateKeyDown(
key: LogicalKeyboardKey.keyC,
modifiers: [ModifierKey.metaModifier],
);

// 1.2 Simulate key up
await keyPressSimulator.simulateKeyPress(
await keyPressSimulator.simulateKeyUp(
key: LogicalKeyboardKey.keyC,
modifiers: [ModifierKey.metaModifier],
keyDown: false,
);

// 2. Simulate long pressing ⌘ + space

// 2.1. Simulate key down
await keyPressSimulator.simulateKeyPress(
await keyPressSimulator.simulateKeyDown(
key: LogicalKeyboardKey.space,
modifiers: [
ModifierKey.metaModifier,
],
modifiers: [ModifierKey.metaModifier],
);

await Future.delayed(const Duration(seconds: 5));

// 2.2. Simulate key up
await keyPressSimulator.simulateKeyPress(
await keyPressSimulator.simulateKeyUp(
key: LogicalKeyboardKey.space,
modifiers: [
ModifierKey.metaModifier,
],
keyDown: false,
modifiers: [ModifierKey.metaModifier],
);
```

Expand Down
40 changes: 20 additions & 20 deletions packages/keypress_simulator/example/lib/pages/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ class _HomePageState extends State<HomePage> {
print('simulateCtrlAKeyPress');
}
await keyPressSimulator.simulateKeyDown(
key: LogicalKeyboardKey.keyA,
modifiers: [
PhysicalKeyboardKey.keyA,
[
Platform.isMacOS
? ModifierKey.metaModifier
: ModifierKey.controlModifier,
],
);
await keyPressSimulator.simulateKeyUp(
key: LogicalKeyboardKey.keyA,
modifiers: [
PhysicalKeyboardKey.keyA,
[
Platform.isMacOS
? ModifierKey.metaModifier
: ModifierKey.controlModifier,
Expand Down Expand Up @@ -167,26 +167,26 @@ class _HomePageState extends State<HomePage> {
title: const Text('Active Spotlight'),
onTap: () async {
await keyPressSimulator.simulateKeyDown(
key: LogicalKeyboardKey.space,
modifiers: [ModifierKey.metaModifier],
PhysicalKeyboardKey.space,
[ModifierKey.metaModifier],
);
await keyPressSimulator.simulateKeyUp(
key: LogicalKeyboardKey.space,
modifiers: [ModifierKey.metaModifier],
PhysicalKeyboardKey.space,
[ModifierKey.metaModifier],
);
},
),
PreferenceListItem(
title: const Text('Active Siri'),
onTap: () async {
await keyPressSimulator.simulateKeyDown(
key: LogicalKeyboardKey.space,
modifiers: [ModifierKey.metaModifier],
PhysicalKeyboardKey.space,
[ModifierKey.metaModifier],
);
await Future.delayed(const Duration(seconds: 6));
await keyPressSimulator.simulateKeyUp(
key: LogicalKeyboardKey.space,
modifiers: [ModifierKey.metaModifier],
PhysicalKeyboardKey.space,
[ModifierKey.metaModifier],
);
},
),
Expand All @@ -195,29 +195,29 @@ class _HomePageState extends State<HomePage> {
onTap: () async {
if (Platform.isMacOS) {
await keyPressSimulator.simulateKeyDown(
key: LogicalKeyboardKey.digit4,
modifiers: [
PhysicalKeyboardKey.digit4,
[
ModifierKey.shiftModifier,
ModifierKey.metaModifier,
],
);
await keyPressSimulator.simulateKeyUp(
key: LogicalKeyboardKey.digit4,
modifiers: [
PhysicalKeyboardKey.digit4,
[
ModifierKey.shiftModifier,
ModifierKey.metaModifier,
],
);
} else if (Platform.isWindows) {
await keyPressSimulator.simulateKeyDown(
key: LogicalKeyboardKey.f1,
modifiers: [
PhysicalKeyboardKey.f1,
[
ModifierKey.controlModifier,
],
);
await keyPressSimulator.simulateKeyUp(
key: LogicalKeyboardKey.f1,
modifiers: [
PhysicalKeyboardKey.f1,
[
ModifierKey.controlModifier,
],
);
Expand Down
24 changes: 12 additions & 12 deletions packages/keypress_simulator/lib/src/keypress_simulator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class KeyPressSimulator {
}

/// Simulate key down.
Future<void> simulateKeyDown({
LogicalKeyboardKey? key,
Future<void> simulateKeyDown(
PhysicalKeyboardKey? key, [
List<ModifierKey> modifiers = const [],
}) {
]) {
return _platform.simulateKeyPress(
key: key,
modifiers: modifiers,
Expand All @@ -35,10 +35,10 @@ class KeyPressSimulator {
}

/// Simulate key up.
Future<void> simulateKeyUp({
LogicalKeyboardKey? key,
Future<void> simulateKeyUp(
PhysicalKeyboardKey? key, [
List<ModifierKey> modifiers = const [],
}) {
]) {
return _platform.simulateKeyPress(
key: key,
modifiers: modifiers,
Expand All @@ -48,22 +48,22 @@ class KeyPressSimulator {

@Deprecated('Please use simulateKeyDown & simulateKeyUp methods.')
Future<void> simulateCtrlCKeyPress() async {
const key = LogicalKeyboardKey.keyC;
const key = PhysicalKeyboardKey.keyC;
final modifiers = Platform.isMacOS
? [ModifierKey.metaModifier]
: [ModifierKey.controlModifier];
await simulateKeyDown(key: key, modifiers: modifiers);
await simulateKeyUp(key: key, modifiers: modifiers);
await simulateKeyDown(key, modifiers);
await simulateKeyUp(key, modifiers);
}

@Deprecated('Please use simulateKeyDown & simulateKeyUp methods.')
Future<void> simulateCtrlVKeyPress() async {
const key = LogicalKeyboardKey.keyV;
const key = PhysicalKeyboardKey.keyV;
final modifiers = Platform.isMacOS
? [ModifierKey.metaModifier]
: [ModifierKey.controlModifier];
await simulateKeyDown(key: key, modifiers: modifiers);
await simulateKeyUp(key: key, modifiers: modifiers);
await simulateKeyDown(key, modifiers);
await simulateKeyUp(key, modifiers);
}
}

Expand Down
Loading

0 comments on commit cc6621b

Please sign in to comment.