diff --git a/keypress_simulator/.clang-format b/keypress_simulator/.clang-format new file mode 100644 index 00000000..3c73f32a --- /dev/null +++ b/keypress_simulator/.clang-format @@ -0,0 +1,12 @@ +# Defines the Chromium style for automatic reformatting. +# http://clang.llvm.org/docs/ClangFormatStyleOptions.html +BasedOnStyle: Chromium +# This defaults to 'Auto'. Explicitly set it for a while, so that +# 'vector >' in existing files gets formatted to +# 'vector>'. ('Auto' means that clang-format will only use +# 'int>>' if the file already contains at least one such instance.) +Standard: Cpp11 +SortIncludes: true +--- +Language: ObjC +ColumnLimit: 100 diff --git a/keypress_simulator/.github/FUNDING.yml b/keypress_simulator/.github/FUNDING.yml new file mode 100644 index 00000000..cb2bfb17 --- /dev/null +++ b/keypress_simulator/.github/FUNDING.yml @@ -0,0 +1 @@ +liberapay: lijy91 diff --git a/keypress_simulator/.github/workflows/build.yml b/keypress_simulator/.github/workflows/build.yml new file mode 100644 index 00000000..1ee5a4aa --- /dev/null +++ b/keypress_simulator/.github/workflows/build.yml @@ -0,0 +1,50 @@ +name: build + +on: + push: + branches: [main, dev] + pull_request: + branches: [main] + +jobs: + build-macos: + runs-on: macos-latest + steps: + - uses: actions/checkout@v3 + - uses: subosito/flutter-action@v2 + with: + flutter-version: "3.16.8" + channel: "stable" + - uses: bluefireteam/melos-action@v2 + - working-directory: ./packages/keypress_simulator/example + run: | + melos bs + flutter build macos --release + + build-web: + runs-on: macos-latest + steps: + - uses: actions/checkout@v3 + - uses: subosito/flutter-action@v2 + with: + flutter-version: "3.16.8" + channel: "stable" + - uses: bluefireteam/melos-action@v2 + - working-directory: ./packages/keypress_simulator/example + run: | + melos bs + flutter build web --release + + build-windows: + runs-on: windows-latest + steps: + - uses: actions/checkout@v3 + - uses: subosito/flutter-action@v2 + with: + flutter-version: "3.16.8" + channel: "stable" + - uses: bluefireteam/melos-action@v2 + - working-directory: ./packages/keypress_simulator/example + run: | + melos bs + flutter build windows --release diff --git a/keypress_simulator/.github/workflows/lint.yml b/keypress_simulator/.github/workflows/lint.yml new file mode 100644 index 00000000..8d68ba7c --- /dev/null +++ b/keypress_simulator/.github/workflows/lint.yml @@ -0,0 +1,31 @@ +name: lint + +on: + push: + branches: [main, dev] + pull_request: + branches: [main] + +jobs: + analyze: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: subosito/flutter-action@v2 + with: + flutter-version: "3.16.8" + channel: "stable" + - uses: bluefireteam/melos-action@v2 + - run: melos run analyze + + format: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: subosito/flutter-action@v2 + with: + flutter-version: "3.16.8" + channel: "stable" + cache: true + - uses: bluefireteam/melos-action@v2 + - run: melos run format-check diff --git a/keypress_simulator/.github/workflows/test.yml b/keypress_simulator/.github/workflows/test.yml new file mode 100644 index 00000000..617f89a8 --- /dev/null +++ b/keypress_simulator/.github/workflows/test.yml @@ -0,0 +1,20 @@ +name: test + +on: + push: + branches: [main, dev] + pull_request: + branches: [main] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: subosito/flutter-action@v2 + with: + flutter-version: "3.16.8" + channel: "stable" + cache: true + - uses: bluefireteam/melos-action@v2 + - run: melos run test --no-select diff --git a/keypress_simulator/.gitignore b/keypress_simulator/.gitignore new file mode 100644 index 00000000..fa08f054 --- /dev/null +++ b/keypress_simulator/.gitignore @@ -0,0 +1,6 @@ +.dart_tool/ +.idea/ + +*.iml +pubspec_overrides.yaml +pubspec.lock diff --git a/keypress_simulator/LICENSE b/keypress_simulator/LICENSE new file mode 100644 index 00000000..eea05ab7 --- /dev/null +++ b/keypress_simulator/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022-2024 LiJianying + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/keypress_simulator/README-ZH.md b/keypress_simulator/README-ZH.md new file mode 100644 index 00000000..91223a21 --- /dev/null +++ b/keypress_simulator/README-ZH.md @@ -0,0 +1,93 @@ +> **🚀 快速发布您的应用**: 试试 [Fastforge](https://fastforge.dev) - 构建、打包和分发您的 Flutter 应用最简单的方式。 + +# keypress_simulator + +[![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 + +这个插件允许 Flutter 桌面应用模拟按键操作。 + +--- + +[English](./README.md) | 简体中文 + +--- + + + + +- [平台支持](#%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) + + + +## 平台支持 + +| Linux | macOS | Windows | +| :---: | :---: | :-----: | +| ➖ | ✔️ | ✔️ | + +## 快速开始 + +### 安装 + +将此添加到你的软件包的 pubspec.yaml 文件: + +```yaml +dependencies: + keypress_simulator: ^0.2.0 +``` + +### 用法 + +```dart +import 'package:keypress_simulator/keypress_simulator.dart'; + +// 1. Simulate pressing ⌘ + C + +// 1.1 Simulate key down +await keyPressSimulator.simulateKeyDown( + PhysicalKeyboardKey.keyC, + [ModifierKey.metaModifier], +); + +// 1.2 Simulate key up +await keyPressSimulator.simulateKeyUp( + PhysicalKeyboardKey.keyC, + [ModifierKey.metaModifier], +); + +// 2. Simulate long pressing ⌘ + space + +// 2.1. Simulate key down +await keyPressSimulator.simulateKeyDown( + PhysicalKeyboardKey.space, + [ModifierKey.metaModifier], +); + +await Future.delayed(const Duration(seconds: 5)); + +// 2.2. Simulate key up +await keyPressSimulator.simulateKeyUp( + PhysicalKeyboardKey.space, + [ModifierKey.metaModifier], +); +``` + +> 请看这个插件的示例应用,以了解完整的例子。 + +## 谁在用使用它? + +- [Biyi (比译)](https://biyidev.com/) - 一个便捷的翻译和词典应用程序。 + +## 许可证 + +[MIT](./LICENSE) diff --git a/keypress_simulator/README.md b/keypress_simulator/README.md new file mode 100644 index 00000000..95ee8f4e --- /dev/null +++ b/keypress_simulator/README.md @@ -0,0 +1,93 @@ +> **🚀 Ship Your App Faster**: Try [Fastforge](https://fastforge.dev) - The simplest way to build, package and distribute your Flutter apps. + +# keypress_simulator + +[![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 + +This plugin allows Flutter desktop apps to simulate key presses. + +--- + +English | [简体中文](./README-ZH.md) + +--- + + + + +- [Platform Support](#platform-support) +- [Quick Start](#quick-start) + - [Installation](#installation) + - [Usage](#usage) +- [Who's using it?](#whos-using-it) +- [License](#license) + + + +## Platform Support + +| Linux | macOS | Windows | +| :---: | :---: | :-----: | +| ➖ | ✔️ | ✔️ | + +## Quick Start + +### Installation + +Add this to your package's pubspec.yaml file: + +```yaml +dependencies: + keypress_simulator: ^0.2.0 +``` + +### Usage + +```dart +import 'package:keypress_simulator/keypress_simulator.dart'; + +// 1. Simulate pressing ⌘ + C + +// 1.1 Simulate key down +await keyPressSimulator.simulateKeyDown( + PhysicalKeyboardKey.keyC, + [ModifierKey.metaModifier], +); + +// 1.2 Simulate key up +await keyPressSimulator.simulateKeyUp( + PhysicalKeyboardKey.keyC, + [ModifierKey.metaModifier], +); + +// 2. Simulate long pressing ⌘ + space + +// 2.1. Simulate key down +await keyPressSimulator.simulateKeyDown( + PhysicalKeyboardKey.space, + [ModifierKey.metaModifier], +); + +await Future.delayed(const Duration(seconds: 5)); + +// 2.2. Simulate key up +await keyPressSimulator.simulateKeyUp( + PhysicalKeyboardKey.space, + [ModifierKey.metaModifier], +); +``` + +> Please see the example app of this plugin for a full example. + +## Who's using it? + +- [Biyi (比译)](https://biyidev.com/) - A convenient translation and dictionary app. + +## License + +[MIT](./LICENSE) diff --git a/keypress_simulator/melos.yaml b/keypress_simulator/melos.yaml new file mode 100644 index 00000000..fc9445c1 --- /dev/null +++ b/keypress_simulator/melos.yaml @@ -0,0 +1,35 @@ +name: keypress_simulator_workspace +repository: https://github.com/leanflutter/keypress_simulator + +packages: + - examples/** + - packages/** + +command: + bootstrap: + # Uses the pubspec_overrides.yaml instead of having Melos modifying the lock file. + usePubspecOverrides: true + +scripts: + analyze: + exec: flutter analyze --fatal-infos + description: Run `flutter analyze` for all packages. + + test: + exec: flutter test + description: Run `flutter test` for a specific package. + packageFilters: + dirExists: + - test + + format: + exec: dart format . --fix + description: Run `dart format` for all packages. + + format-check: + exec: dart format . --fix --set-exit-if-changed + description: Run `dart format` checks for all packages. + + fix: + exec: dart fix . --apply + description: Run `dart fix` for all packages. diff --git a/keypress_simulator/packages/keypress_simulator/.gitignore b/keypress_simulator/packages/keypress_simulator/.gitignore new file mode 100644 index 00000000..01c49d55 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator/.gitignore @@ -0,0 +1,29 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +.vscode/ + +# Flutter/Dart/Pub related +# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. +/pubspec.lock +**/doc/api/ +.dart_tool/ +.packages +build/ diff --git a/keypress_simulator/packages/keypress_simulator/.metadata b/keypress_simulator/packages/keypress_simulator/.metadata new file mode 100644 index 00000000..0b5ddf05 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator/.metadata @@ -0,0 +1,10 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: db747aa1331bd95bc9b3874c842261ca2d302cd5 + channel: stable + +project_type: plugin diff --git a/keypress_simulator/packages/keypress_simulator/CHANGELOG.md b/keypress_simulator/packages/keypress_simulator/CHANGELOG.md new file mode 100644 index 00000000..a2579009 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator/CHANGELOG.md @@ -0,0 +1,7 @@ +## 0.2.0 + +* feat: Convert to federated plugin + +## 0.1.0 + +* First release. diff --git a/keypress_simulator/packages/keypress_simulator/LICENSE b/keypress_simulator/packages/keypress_simulator/LICENSE new file mode 100644 index 00000000..eea05ab7 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022-2024 LiJianying + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/keypress_simulator/packages/keypress_simulator/README-ZH.md b/keypress_simulator/packages/keypress_simulator/README-ZH.md new file mode 120000 index 00000000..7da944f1 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator/README-ZH.md @@ -0,0 +1 @@ +../../README-ZH.md \ No newline at end of file diff --git a/keypress_simulator/packages/keypress_simulator/README.md b/keypress_simulator/packages/keypress_simulator/README.md new file mode 120000 index 00000000..fe840054 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator/README.md @@ -0,0 +1 @@ +../../README.md \ No newline at end of file diff --git a/keypress_simulator/packages/keypress_simulator/analysis_options.yaml b/keypress_simulator/packages/keypress_simulator/analysis_options.yaml new file mode 100644 index 00000000..095b1d67 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator/analysis_options.yaml @@ -0,0 +1 @@ +include: package:mostly_reasonable_lints/flutter.yaml diff --git a/keypress_simulator/packages/keypress_simulator/lib/keypress_simulator.dart b/keypress_simulator/packages/keypress_simulator/lib/keypress_simulator.dart new file mode 100644 index 00000000..4c8a7e89 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator/lib/keypress_simulator.dart @@ -0,0 +1 @@ +export 'src/keypress_simulator.dart'; diff --git a/keypress_simulator/packages/keypress_simulator/lib/src/keypress_simulator.dart b/keypress_simulator/packages/keypress_simulator/lib/src/keypress_simulator.dart new file mode 100644 index 00000000..f1335ec5 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator/lib/src/keypress_simulator.dart @@ -0,0 +1,70 @@ +import 'dart:async'; +import 'dart:io'; + +import 'package:flutter/services.dart'; +import 'package:keypress_simulator_platform_interface/keypress_simulator_platform_interface.dart'; + +class KeyPressSimulator { + KeyPressSimulator._(); + + /// The shared instance of [KeyPressSimulator]. + static final KeyPressSimulator instance = KeyPressSimulator._(); + + KeyPressSimulatorPlatform get _platform => KeyPressSimulatorPlatform.instance; + + Future isAccessAllowed() { + return _platform.isAccessAllowed(); + } + + Future requestAccess({ + bool onlyOpenPrefPane = false, + }) { + return _platform.requestAccess(onlyOpenPrefPane: onlyOpenPrefPane); + } + + /// Simulate key down. + Future simulateKeyDown( + PhysicalKeyboardKey? key, [ + List modifiers = const [], + ]) { + return _platform.simulateKeyPress( + key: key, + modifiers: modifiers, + keyDown: true, + ); + } + + /// Simulate key up. + Future simulateKeyUp( + PhysicalKeyboardKey? key, [ + List modifiers = const [], + ]) { + return _platform.simulateKeyPress( + key: key, + modifiers: modifiers, + keyDown: false, + ); + } + + @Deprecated('Please use simulateKeyDown & simulateKeyUp methods.') + Future simulateCtrlCKeyPress() async { + const key = PhysicalKeyboardKey.keyC; + final modifiers = Platform.isMacOS + ? [ModifierKey.metaModifier] + : [ModifierKey.controlModifier]; + await simulateKeyDown(key, modifiers); + await simulateKeyUp(key, modifiers); + } + + @Deprecated('Please use simulateKeyDown & simulateKeyUp methods.') + Future simulateCtrlVKeyPress() async { + const key = PhysicalKeyboardKey.keyV; + final modifiers = Platform.isMacOS + ? [ModifierKey.metaModifier] + : [ModifierKey.controlModifier]; + await simulateKeyDown(key, modifiers); + await simulateKeyUp(key, modifiers); + } +} + +final keyPressSimulator = KeyPressSimulator.instance; diff --git a/keypress_simulator/packages/keypress_simulator/pubspec.yaml b/keypress_simulator/packages/keypress_simulator/pubspec.yaml new file mode 100644 index 00000000..1f45031d --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator/pubspec.yaml @@ -0,0 +1,33 @@ +name: keypress_simulator +description: This plugin allows Flutter desktop apps to simulate key presses. +version: 0.2.0 +homepage: https://github.com/leanflutter/keypress_simulator + +platforms: + macos: + windows: + +environment: + sdk: ">=3.0.0 <4.0.0" + flutter: ">=3.3.0" + +dependencies: + flutter: + sdk: flutter + keypress_simulator_macos: ^0.2.0 + keypress_simulator_platform_interface: ^0.2.0 + keypress_simulator_windows: ^0.2.0 + +dev_dependencies: + flutter_test: + sdk: flutter + mostly_reasonable_lints: ^0.1.1 + +flutter: + plugin: + platforms: + macos: + default_package: keypress_simulator_macos + windows: + default_package: keypress_simulator_windows + diff --git a/keypress_simulator/packages/keypress_simulator_macos/.gitignore b/keypress_simulator/packages/keypress_simulator_macos/.gitignore new file mode 100644 index 00000000..ac5aa989 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_macos/.gitignore @@ -0,0 +1,29 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ +migrate_working_dir/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. +/pubspec.lock +**/doc/api/ +.dart_tool/ +build/ diff --git a/keypress_simulator/packages/keypress_simulator_macos/.metadata b/keypress_simulator/packages/keypress_simulator_macos/.metadata new file mode 100644 index 00000000..6422a999 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_macos/.metadata @@ -0,0 +1,30 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: "67457e669f79e9f8d13d7a68fe09775fefbb79f4" + channel: "stable" + +project_type: plugin + +# Tracks metadata for the flutter migrate command +migration: + platforms: + - platform: root + create_revision: 67457e669f79e9f8d13d7a68fe09775fefbb79f4 + base_revision: 67457e669f79e9f8d13d7a68fe09775fefbb79f4 + - platform: macos + create_revision: 67457e669f79e9f8d13d7a68fe09775fefbb79f4 + base_revision: 67457e669f79e9f8d13d7a68fe09775fefbb79f4 + + # User provided section + + # List of Local paths (relative to this file) that should be + # ignored by the migrate tool. + # + # Files that are not part of the templates will be ignored by default. + unmanaged_files: + - 'lib/main.dart' + - 'ios/Runner.xcodeproj/project.pbxproj' diff --git a/keypress_simulator/packages/keypress_simulator_macos/CHANGELOG.md b/keypress_simulator/packages/keypress_simulator_macos/CHANGELOG.md new file mode 100644 index 00000000..2f145cea --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_macos/CHANGELOG.md @@ -0,0 +1,3 @@ +## 0.2.0 + +* First release. diff --git a/keypress_simulator/packages/keypress_simulator_macos/LICENSE b/keypress_simulator/packages/keypress_simulator_macos/LICENSE new file mode 100644 index 00000000..eea05ab7 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_macos/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022-2024 LiJianying + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/keypress_simulator/packages/keypress_simulator_macos/README.md b/keypress_simulator/packages/keypress_simulator_macos/README.md new file mode 100644 index 00000000..87a076f0 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_macos/README.md @@ -0,0 +1,12 @@ +# keypress_simulator_macos + +[![pub version][pub-image]][pub-url] + +[pub-image]: https://img.shields.io/pub/v/keypress_simulator_macos.svg +[pub-url]: https://pub.dev/packages/keypress_simulator_macos + +The macOS implementation of [keypress_simulator](https://pub.dev/packages/keypress_simulator). + +## License + +[MIT](./LICENSE) diff --git a/keypress_simulator/packages/keypress_simulator_macos/analysis_options.yaml b/keypress_simulator/packages/keypress_simulator_macos/analysis_options.yaml new file mode 100644 index 00000000..095b1d67 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_macos/analysis_options.yaml @@ -0,0 +1 @@ +include: package:mostly_reasonable_lints/flutter.yaml diff --git a/keypress_simulator/packages/keypress_simulator_macos/macos/Classes/KeypressSimulatorMacosPlugin.swift b/keypress_simulator/packages/keypress_simulator_macos/macos/Classes/KeypressSimulatorMacosPlugin.swift new file mode 100644 index 00000000..8be4f0e3 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_macos/macos/Classes/KeypressSimulatorMacosPlugin.swift @@ -0,0 +1,79 @@ +import Cocoa +import FlutterMacOS + +public class KeypressSimulatorMacosPlugin: NSObject, FlutterPlugin { + public static func register(with registrar: FlutterPluginRegistrar) { + let channel = FlutterMethodChannel(name: "dev.leanflutter.plugins/keypress_simulator", binaryMessenger: registrar.messenger) + let instance = KeypressSimulatorMacosPlugin() + registrar.addMethodCallDelegate(instance, channel: channel) + } + + public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { + switch call.method { + case "isAccessAllowed": + isAccessAllowed(call, result: result) + break + case "requestAccess": + requestAccess(call, result: result) + break + case "simulateKeyPress": + simulateKeyPress(call, result: result) + break + default: + result(FlutterMethodNotImplemented) + } + } + public func isAccessAllowed(_ call: FlutterMethodCall, result: @escaping FlutterResult) { + result(AXIsProcessTrusted()) + } + + public func requestAccess(_ call: FlutterMethodCall, result: @escaping FlutterResult) { + let args:[String: Any] = call.arguments as! [String: Any] + let onlyOpenPrefPane: Bool = args["onlyOpenPrefPane"] as! Bool + + if (!onlyOpenPrefPane) { + let options = [kAXTrustedCheckOptionPrompt.takeRetainedValue(): true] as CFDictionary + AXIsProcessTrustedWithOptions(options) + } else { + let prefpaneUrl = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility")! + NSWorkspace.shared.open(prefpaneUrl) + } + result(true) + } + + public func simulateKeyPress(_ call: FlutterMethodCall, result: @escaping FlutterResult) { + let args:[String: Any] = call.arguments as! [String: Any] + + let keyCode: Int? = args["keyCode"] as? Int + let modifiers: Array = args["modifiers"] as! Array + let keyDown: Bool = args["keyDown"] as! Bool + + let event = _createKeyPressEvent(keyCode, modifiers, keyDown); + event.post(tap: .cghidEventTap); + result(true) + } + + public func _createKeyPressEvent(_ keyCode: Int?, _ modifiers: Array, _ keyDown: Bool) -> CGEvent { + let virtualKey: CGKeyCode = CGKeyCode(UInt32(keyCode ?? 0)) + var flags: CGEventFlags = CGEventFlags() + + if (modifiers.contains("shiftModifier")) { + flags.insert(CGEventFlags.maskShift) + } + if (modifiers.contains("controlModifier")) { + flags.insert(CGEventFlags.maskControl) + } + if (modifiers.contains("altModifier")) { + flags.insert(CGEventFlags.maskAlternate) + } + if (modifiers.contains("metaModifier")) { + flags.insert(CGEventFlags.maskCommand) + } + if (modifiers.contains("functionModifier")) { + flags.insert(CGEventFlags.maskSecondaryFn) + } + let eventKeyPress = CGEvent(keyboardEventSource: nil, virtualKey: virtualKey, keyDown: keyDown); + eventKeyPress!.flags = flags + return eventKeyPress! + } +} diff --git a/keypress_simulator/packages/keypress_simulator_macos/macos/keypress_simulator_macos.podspec b/keypress_simulator/packages/keypress_simulator_macos/macos/keypress_simulator_macos.podspec new file mode 100644 index 00000000..81ed8532 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_macos/macos/keypress_simulator_macos.podspec @@ -0,0 +1,23 @@ +# +# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html. +# Run `pod lib lint keypress_simulator_macos.podspec` to validate before publishing. +# +Pod::Spec.new do |s| + s.name = 'keypress_simulator_macos' + s.version = '0.0.1' + s.summary = 'A new Flutter plugin project.' + s.description = <<-DESC +A new Flutter plugin project. + DESC + s.homepage = 'http://example.com' + s.license = { :file => '../LICENSE' } + s.author = { 'Your Company' => 'email@example.com' } + + s.source = { :path => '.' } + s.source_files = 'Classes/**/*' + s.dependency 'FlutterMacOS' + + s.platform = :osx, '10.11' + s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' } + s.swift_version = '5.0' +end diff --git a/keypress_simulator/packages/keypress_simulator_macos/pubspec.yaml b/keypress_simulator/packages/keypress_simulator_macos/pubspec.yaml new file mode 100644 index 00000000..7dba1849 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_macos/pubspec.yaml @@ -0,0 +1,26 @@ +name: keypress_simulator_macos +description: macOS implementation of the keypress_simulator plugin. +version: 0.2.0 +repository: https://github.com/leanflutter/keypress_simulator/tree/main/packages/keypress_simulator_macos + +environment: + sdk: '>=3.0.0 <4.0.0' + flutter: '>=3.3.0' + +dependencies: + flutter: + sdk: flutter + keypress_simulator_platform_interface: ^0.2.0 + +dev_dependencies: + flutter_test: + sdk: flutter + mostly_reasonable_lints: ^0.1.1 + +flutter: + plugin: + implements: keypress_simulator + platforms: + macos: + pluginClass: KeypressSimulatorMacosPlugin + diff --git a/keypress_simulator/packages/keypress_simulator_platform_interface/.gitignore b/keypress_simulator/packages/keypress_simulator_platform_interface/.gitignore new file mode 100644 index 00000000..ac5aa989 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_platform_interface/.gitignore @@ -0,0 +1,29 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ +migrate_working_dir/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. +/pubspec.lock +**/doc/api/ +.dart_tool/ +build/ diff --git a/keypress_simulator/packages/keypress_simulator_platform_interface/.metadata b/keypress_simulator/packages/keypress_simulator_platform_interface/.metadata new file mode 100644 index 00000000..9e91d2fd --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_platform_interface/.metadata @@ -0,0 +1,27 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: "67457e669f79e9f8d13d7a68fe09775fefbb79f4" + channel: "stable" + +project_type: plugin + +# Tracks metadata for the flutter migrate command +migration: + platforms: + - platform: root + create_revision: 67457e669f79e9f8d13d7a68fe09775fefbb79f4 + base_revision: 67457e669f79e9f8d13d7a68fe09775fefbb79f4 + + # User provided section + + # List of Local paths (relative to this file) that should be + # ignored by the migrate tool. + # + # Files that are not part of the templates will be ignored by default. + unmanaged_files: + - 'lib/main.dart' + - 'ios/Runner.xcodeproj/project.pbxproj' diff --git a/keypress_simulator/packages/keypress_simulator_platform_interface/CHANGELOG.md b/keypress_simulator/packages/keypress_simulator_platform_interface/CHANGELOG.md new file mode 100644 index 00000000..2f145cea --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_platform_interface/CHANGELOG.md @@ -0,0 +1,3 @@ +## 0.2.0 + +* First release. diff --git a/keypress_simulator/packages/keypress_simulator_platform_interface/LICENSE b/keypress_simulator/packages/keypress_simulator_platform_interface/LICENSE new file mode 100644 index 00000000..eea05ab7 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_platform_interface/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022-2024 LiJianying + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/keypress_simulator/packages/keypress_simulator_platform_interface/README.md b/keypress_simulator/packages/keypress_simulator_platform_interface/README.md new file mode 100644 index 00000000..907200d1 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_platform_interface/README.md @@ -0,0 +1,16 @@ +# keypress_simulator_platform_interface + +[![pub version][pub-image]][pub-url] + +[pub-image]: https://img.shields.io/pub/v/keypress_simulator_platform_interface.svg +[pub-url]: https://pub.dev/packages/keypress_simulator_platform_interface + +A common platform interface for the [keypress_simulator](https://pub.dev/packages/keypress_simulator) plugin. + +## Usage + +To implement a new platform-specific implementation of keypress_simulator, extend `KeyPressSimulatorPlatform` with an implementation that performs the platform-specific behavior, and when you register your plugin, set the default `KeyPressSimulatorPlatform` by calling `KeyPressSimulatorPlatform.instance = MyPlatformKeyPressSimulator()`. + +## License + +[MIT](./LICENSE) diff --git a/keypress_simulator/packages/keypress_simulator_platform_interface/analysis_options.yaml b/keypress_simulator/packages/keypress_simulator_platform_interface/analysis_options.yaml new file mode 100644 index 00000000..095b1d67 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_platform_interface/analysis_options.yaml @@ -0,0 +1 @@ +include: package:mostly_reasonable_lints/flutter.yaml diff --git a/keypress_simulator/packages/keypress_simulator_platform_interface/lib/keypress_simulator_platform_interface.dart b/keypress_simulator/packages/keypress_simulator_platform_interface/lib/keypress_simulator_platform_interface.dart new file mode 100644 index 00000000..fdd2ecad --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_platform_interface/lib/keypress_simulator_platform_interface.dart @@ -0,0 +1,4 @@ +library keypress_simulator_platform_interface; + +export 'src/keypress_simulator_method_channel.dart'; +export 'src/keypress_simulator_platform_interface.dart'; diff --git a/keypress_simulator/packages/keypress_simulator_platform_interface/lib/src/keypress_simulator_method_channel.dart b/keypress_simulator/packages/keypress_simulator_platform_interface/lib/src/keypress_simulator_method_channel.dart new file mode 100644 index 00000000..6754ddfe --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_platform_interface/lib/src/keypress_simulator_method_channel.dart @@ -0,0 +1,51 @@ +import 'package:flutter/services.dart'; +import 'package:flutter/widgets.dart'; +import 'package:keypress_simulator_platform_interface/src/keypress_simulator_platform_interfacetorPlatform] that uses method channels. +class MethodChannelKeyPressSimulator extends KeyPressSimulatorPlatform { + /// The method channel used to interact with the native platform. + @visibleForTesting + final methodChannel = const MethodChannel( + 'dev.leanflutter.plugins/keypress_simulator', + ); + + @override + Future isAccessAllowed() async { + if (UniPlatform.isMacOS) { + return await methodChannel.invokeMethod('isAccessAllowed'); + } + return true; + } + + @override + Future requestAccess({ + bool onlyOpenPrefPane = false, + }) async { + if (UniPlatform.isMacOS) { + final Map arguments = { + 'onlyOpenPrefPane': onlyOpenPrefPane, + }; + await methodChannel.invokeMethod('requestAccess', arguments); + } + } + + @override + Future simulateKeyPress({ + KeyboardKey? key, + List modifiers = const [], + bool keyDown = true, + }) async { + PhysicalKeyboardKey? physicalKey = key is PhysicalKeyboardKey ? key : null; + if (key is LogicalKeyboardKey) { + physicalKey = key.physicalKey; + } + if (key != null && physicalKey == null) { + throw UnsupportedError('Unsupported key: $key.'); + } + final Map arguments = { + 'keyCode': physicalKey?.keyCode, + 'modifiers': modifiers.map((e) => e.name).toList(), + 'keyDown': keyDown, + }..removeWhere((key, value) => value == null); + await methodChannel.invokeMethod('simulateKeyPress', arguments); + } +} diff --git a/keypress_simulator/packages/keypress_simulator_platform_interface/lib/src/keypress_simulator_platform_interface.dart b/keypress_simulator/packages/keypress_simulator_platform_interface/lib/src/keypress_simulator_platform_interface.dart new file mode 100644 index 00000000..9d6b0bfd --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_platform_interface/lib/src/keypress_simulator_platform_interface.dart @@ -0,0 +1,43 @@ +import 'package:flutter/services.dart'; +import 'package:keypress_simulator_platform_interface/src/keypress_simulator_method_channel.dart'; +import 'package:plugin_platform_interface/plugin_platform_interface.dart'; + +abstract class KeyPressSimulatorPlatform extends PlatformInterface { + /// Constructs a KeyPressSimulatorPlatform. + KeyPressSimulatorPlatform() : super(token: _token); + + static final Object _token = Object(); + + static KeyPressSimulatorPlatform _instance = MethodChannelKeyPressSimulator(); + + /// The default instance of [KeyPressSimulatorPlatform] to use. + /// + /// Defaults to [MethodChannelKeyPressSimulator]. + static KeyPressSimulatorPlatform get instance => _instance; + + /// Platform-specific implementations should set this with their own + /// platform-specific class that extends [KeyPressSimulatorPlatform] when + /// they register themselves. + static set instance(KeyPressSimulatorPlatform instance) { + PlatformInterface.verifyToken(instance, _token); + _instance = instance; + } + + Future isAccessAllowed() { + throw UnimplementedError('isAccessAllowed() has not been implemented.'); + } + + Future requestAccess({ + bool onlyOpenPrefPane = false, + }) { + throw UnimplementedError('requestAccess() has not been implemented.'); + } + + Future simulateKeyPress({ + KeyboardKey? key, + List modifiers = const [], + bool keyDown = true, + }) { + throw UnimplementedError('simulateKeyPress() has not been implemented.'); + } +} diff --git a/keypress_simulator/packages/keypress_simulator_platform_interface/pubspec.yaml b/keypress_simulator/packages/keypress_simulator_platform_interface/pubspec.yaml new file mode 100644 index 00000000..4b85fcf1 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_platform_interface/pubspec.yaml @@ -0,0 +1,20 @@ +name: keypress_simulator_platform_interface +description: A common platform interface for the keypress_simulator plugin. +version: 0.2.0 +homepage: https://github.com/leanflutter/keypress_simulator/blob/main/packages/keypress_simulator_platform_interface + +environment: + sdk: '>=3.0.0 <4.0.0' + flutter: '>=3.3.0' + +dependencies: + collection: ^1.18.0 + flutter: + sdk: flutter + plugin_platform_interface: ^2.1.8 + uni_platform: ^0.1.2 + +dev_dependencies: + flutter_test: + sdk: flutter + mostly_reasonable_lints: ^0.1.1 diff --git a/keypress_simulator/packages/keypress_simulator_platform_interface/test/src/keypress_simulator_method_channel_test.dart b/keypress_simulator/packages/keypress_simulator_platform_interface/test/src/keypress_simulator_method_channel_test.dart new file mode 100644 index 00000000..af2c3b90 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_platform_interface/test/src/keypress_simulator_method_channel_test.dart @@ -0,0 +1,32 @@ +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:keypress_simulator_platform_interface/src/keypress_simulator_method_channel.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + MethodChannelKeyPressSimulator platform = MethodChannelKeyPressSimulator(); + const MethodChannel channel = MethodChannel( + 'dev.leanflutter.plugins/keypress_simulator', + ); + + setUp(() { + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger + .setMockMethodCallHandler( + channel, + (MethodCall methodCall) async { + if (methodCall.method == 'isAccessAllowed') return true; + return '42'; + }, + ); + }); + + tearDown(() { + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger + .setMockMethodCallHandler(channel, null); + }); + + test('isAccessAllowed', () async { + expect(await platform.isAccessAllowed(), true); + }); +} diff --git a/keypress_simulator/packages/keypress_simulator_windows/.gitignore b/keypress_simulator/packages/keypress_simulator_windows/.gitignore new file mode 100644 index 00000000..ac5aa989 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_windows/.gitignore @@ -0,0 +1,29 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ +migrate_working_dir/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. +/pubspec.lock +**/doc/api/ +.dart_tool/ +build/ diff --git a/keypress_simulator/packages/keypress_simulator_windows/.metadata b/keypress_simulator/packages/keypress_simulator_windows/.metadata new file mode 100644 index 00000000..bcaf04fc --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_windows/.metadata @@ -0,0 +1,30 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: "67457e669f79e9f8d13d7a68fe09775fefbb79f4" + channel: "stable" + +project_type: plugin + +# Tracks metadata for the flutter migrate command +migration: + platforms: + - platform: root + create_revision: 67457e669f79e9f8d13d7a68fe09775fefbb79f4 + base_revision: 67457e669f79e9f8d13d7a68fe09775fefbb79f4 + - platform: windows + create_revision: 67457e669f79e9f8d13d7a68fe09775fefbb79f4 + base_revision: 67457e669f79e9f8d13d7a68fe09775fefbb79f4 + + # User provided section + + # List of Local paths (relative to this file) that should be + # ignored by the migrate tool. + # + # Files that are not part of the templates will be ignored by default. + unmanaged_files: + - 'lib/main.dart' + - 'ios/Runner.xcodeproj/project.pbxproj' diff --git a/keypress_simulator/packages/keypress_simulator_windows/CHANGELOG.md b/keypress_simulator/packages/keypress_simulator_windows/CHANGELOG.md new file mode 100644 index 00000000..2f145cea --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_windows/CHANGELOG.md @@ -0,0 +1,3 @@ +## 0.2.0 + +* First release. diff --git a/keypress_simulator/packages/keypress_simulator_windows/LICENSE b/keypress_simulator/packages/keypress_simulator_windows/LICENSE new file mode 100644 index 00000000..eea05ab7 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_windows/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022-2024 LiJianying + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/keypress_simulator/packages/keypress_simulator_windows/README.md b/keypress_simulator/packages/keypress_simulator_windows/README.md new file mode 100644 index 00000000..a7ee3cad --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_windows/README.md @@ -0,0 +1,12 @@ +# keypress_simulator_windows + +[![pub version][pub-image]][pub-url] + +[pub-image]: https://img.shields.io/pub/v/keypress_simulator_windows.svg +[pub-url]: https://pub.dev/packages/keypress_simulator_windows + +The Windows implementation of [keypress_simulator](https://pub.dev/packages/keypress_simulator). + +## License + +[MIT](./LICENSE) diff --git a/keypress_simulator/packages/keypress_simulator_windows/analysis_options.yaml b/keypress_simulator/packages/keypress_simulator_windows/analysis_options.yaml new file mode 100644 index 00000000..095b1d67 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_windows/analysis_options.yaml @@ -0,0 +1 @@ +include: package:mostly_reasonable_lints/flutter.yaml diff --git a/keypress_simulator/packages/keypress_simulator_windows/pubspec.yaml b/keypress_simulator/packages/keypress_simulator_windows/pubspec.yaml new file mode 100644 index 00000000..7be83923 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_windows/pubspec.yaml @@ -0,0 +1,25 @@ +name: keypress_simulator_windows +description: Windows implementation of the keypress_simulator plugin. +version: 0.2.0 +repository: https://github.com/leanflutter/keypress_simulator/tree/main/packages/keypress_simulator_windows + +environment: + sdk: '>=3.0.0 <4.0.0' + flutter: '>=3.3.0' + +dependencies: + flutter: + sdk: flutter + keypress_simulator_platform_interface: ^0.2.0 + +dev_dependencies: + flutter_test: + sdk: flutter + mostly_reasonable_lints: ^0.1.1 + +flutter: + plugin: + implements: keypress_simulator + platforms: + windows: + pluginClass: KeypressSimulatorWindowsPluginCApi diff --git a/keypress_simulator/packages/keypress_simulator_windows/windows/.gitignore b/keypress_simulator/packages/keypress_simulator_windows/windows/.gitignore new file mode 100644 index 00000000..b3eb2be1 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_windows/windows/.gitignore @@ -0,0 +1,17 @@ +flutter/ + +# Visual Studio user-specific files. +*.suo +*.user +*.userosscache +*.sln.docstates + +# Visual Studio build-related files. +x64/ +x86/ + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ diff --git a/keypress_simulator/packages/keypress_simulator_windows/windows/CMakeLists.txt b/keypress_simulator/packages/keypress_simulator_windows/windows/CMakeLists.txt new file mode 100644 index 00000000..a516e24b --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_windows/windows/CMakeLists.txt @@ -0,0 +1,100 @@ +# The Flutter tooling requires that developers have a version of Visual Studio +# installed that includes CMake 3.14 or later. You should not increase this +# version, as doing so will cause the plugin to fail to compile for some +# customers of the plugin. +cmake_minimum_required(VERSION 3.14) + +# Project-level configuration. +set(PROJECT_NAME "keypress_simulator_windows") +project(${PROJECT_NAME} LANGUAGES CXX) + +# Explicitly opt in to modern CMake behaviors to avoid warnings with recent +# versions of CMake. +cmake_policy(VERSION 3.14...3.25) + +# This value is used when generating builds using this plugin, so it must +# not be changed +set(PLUGIN_NAME "keypress_simulator_windows_plugin") + +# Any new source files that you add to the plugin should be added here. +list(APPEND PLUGIN_SOURCES + "keypress_simulator_windows_plugin.cpp" + "keypress_simulator_windows_plugin.h" +) + +# Define the plugin library target. Its name must not be changed (see comment +# on PLUGIN_NAME above). +add_library(${PLUGIN_NAME} SHARED + "include/keypress_simulator_windows/keypress_simulator_windows_plugin_c_api.h" + "keypress_simulator_windows_plugin_c_api.cpp" + ${PLUGIN_SOURCES} +) + +# Apply a standard set of build settings that are configured in the +# application-level CMakeLists.txt. This can be removed for plugins that want +# full control over build settings. +apply_standard_settings(${PLUGIN_NAME}) + +# Symbols are hidden by default to reduce the chance of accidental conflicts +# between plugins. This should not be removed; any symbols that should be +# exported should be explicitly exported with the FLUTTER_PLUGIN_EXPORT macro. +set_target_properties(${PLUGIN_NAME} PROPERTIES + CXX_VISIBILITY_PRESET hidden) +target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL) + +# Source include directories and library dependencies. Add any plugin-specific +# dependencies here. +target_include_directories(${PLUGIN_NAME} INTERFACE + "${CMAKE_CURRENT_SOURCE_DIR}/include") +target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin) + +# List of absolute paths to libraries that should be bundled with the plugin. +# This list could contain prebuilt libraries, or libraries created by an +# external build triggered from this build file. +set(keypress_simulator_windows_bundled_libraries + "" + PARENT_SCOPE +) + +# === Tests === +# These unit tests can be run from a terminal after building the example, or +# from Visual Studio after opening the generated solution file. + +# Only enable test builds when building the example (which sets this variable) +# so that plugin clients aren't building the tests. +if (${include_${PROJECT_NAME}_tests}) +set(TEST_RUNNER "${PROJECT_NAME}_test") +enable_testing() + +# Add the Google Test dependency. +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/release-1.11.0.zip +) +# Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +# Disable install commands for gtest so it doesn't end up in the bundle. +set(INSTALL_GTEST OFF CACHE BOOL "Disable installation of googletest" FORCE) +FetchContent_MakeAvailable(googletest) + +# The plugin's C API is not very useful for unit testing, so build the sources +# directly into the test binary rather than using the DLL. +add_executable(${TEST_RUNNER} + test/keypress_simulator_windows_plugin_test.cpp + ${PLUGIN_SOURCES} +) +apply_standard_settings(${TEST_RUNNER}) +target_include_directories(${TEST_RUNNER} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}") +target_link_libraries(${TEST_RUNNER} PRIVATE flutter_wrapper_plugin) +target_link_libraries(${TEST_RUNNER} PRIVATE gtest_main gmock) +# flutter_wrapper_plugin has link dependencies on the Flutter DLL. +add_custom_command(TARGET ${TEST_RUNNER} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${FLUTTER_LIBRARY}" $ +) + +# Enable automatic test discovery. +include(GoogleTest) +gtest_discover_tests(${TEST_RUNNER}) +endif() diff --git a/keypress_simulator/packages/keypress_simulator_windows/windows/include/keypress_simulator_windows/keypress_simulator_windows_plugin_c_api.h b/keypress_simulator/packages/keypress_simulator_windows/windows/include/keypress_simulator_windows/keypress_simulator_windows_plugin_c_api.h new file mode 100644 index 00000000..f69a1c28 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_windows/windows/include/keypress_simulator_windows/keypress_simulator_windows_plugin_c_api.h @@ -0,0 +1,23 @@ +#ifndef FLUTTER_PLUGIN_KEYPRESS_SIMULATOR_WINDOWS_PLUGIN_C_API_H_ +#define FLUTTER_PLUGIN_KEYPRESS_SIMULATOR_WINDOWS_PLUGIN_C_API_H_ + +#include + +#ifdef FLUTTER_PLUGIN_IMPL +#define FLUTTER_PLUGIN_EXPORT __declspec(dllexport) +#else +#define FLUTTER_PLUGIN_EXPORT __declspec(dllimport) +#endif + +#if defined(__cplusplus) +extern "C" { +#endif + +FLUTTER_PLUGIN_EXPORT void KeypressSimulatorWindowsPluginCApiRegisterWithRegistrar( + FlutterDesktopPluginRegistrarRef registrar); + +#if defined(__cplusplus) +} // extern "C" +#endif + +#endif // FLUTTER_PLUGIN_KEYPRESS_SIMULATOR_WINDOWS_PLUGIN_C_API_H_ diff --git a/keypress_simulator/packages/keypress_simulator_windows/windows/keypress_simulator_windows_plugin.cpp b/keypress_simulator/packages/keypress_simulator_windows/windows/keypress_simulator_windows_plugin.cpp new file mode 100644 index 00000000..cba7da58 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_windows/windows/keypress_simulator_windows_plugin.cpp @@ -0,0 +1,95 @@ +#include "keypress_simulator_windows_plugin.h" + +// This must be included before many other Windows headers. +#include + +#include +#include +#include + +#include +#include + +using flutter::EncodableList; +using flutter::EncodableMap; +using flutter::EncodableValue; + +namespace keypress_simulator_windows { + +// static +void KeypressSimulatorWindowsPlugin::RegisterWithRegistrar( + flutter::PluginRegistrarWindows* registrar) { + auto channel = + std::make_unique>( + registrar->messenger(), "dev.leanflutter.plugins/keypress_simulator", + &flutter::StandardMethodCodec::GetInstance()); + + auto plugin = std::make_unique(); + + channel->SetMethodCallHandler( + [plugin_pointer = plugin.get()](const auto& call, auto result) { + plugin_pointer->HandleMethodCall(call, std::move(result)); + }); + + registrar->AddPlugin(std::move(plugin)); +} + +KeypressSimulatorWindowsPlugin::KeypressSimulatorWindowsPlugin() {} + +KeypressSimulatorWindowsPlugin::~KeypressSimulatorWindowsPlugin() {} + +void KeypressSimulatorWindowsPlugin::SimulateKeyPress( + const flutter::MethodCall& method_call, + std::unique_ptr> result) { + const EncodableMap& args = std::get(*method_call.arguments()); + + UINT keyCode = std::get(args.at(EncodableValue("keyCode"))); + std::vector modifiers; + bool keyDown = std::get(args.at(EncodableValue("keyDown"))); + + EncodableList key_modifier_list = + std::get(args.at(EncodableValue("modifiers"))); + for (flutter::EncodableValue key_modifier_value : key_modifier_list) { + std::string key_modifier = std::get(key_modifier_value); + modifiers.push_back(key_modifier); + } + + INPUT input[6]; + + for (int32_t i = 0; i < modifiers.size(); i++) { + if (modifiers[i].compare("shiftModifier") == 0) { + input[i].ki.wVk = VK_SHIFT; + } else if (modifiers[i].compare("controlModifier") == 0) { + input[i].ki.wVk = VK_CONTROL; + } else if (modifiers[i].compare("altModifier") == 0) { + input[i].ki.wVk = VK_MENU; + } else if (modifiers[i].compare("metaModifier") == 0) { + input[i].ki.wVk = VK_LWIN; + } + + input[i].ki.dwFlags = keyDown ? 0 : KEYEVENTF_KEYUP; + input[i].type = INPUT_KEYBOARD; + } + + int keyIndex = static_cast(modifiers.size()); + input[keyIndex].ki.wVk = static_cast(keyCode); + input[keyIndex].ki.dwFlags = keyDown ? 0 : KEYEVENTF_KEYUP; + input[keyIndex].type = INPUT_KEYBOARD; + + // Send key sequence to system + SendInput(static_cast(std::size(input)), input, sizeof(INPUT)); + + result->Success(flutter::EncodableValue(true)); +} + +void KeypressSimulatorWindowsPlugin::HandleMethodCall( + const flutter::MethodCall& method_call, + std::unique_ptr> result) { + if (method_call.method_name().compare("simulateKeyPress") == 0) { + SimulateKeyPress(method_call, std::move(result)); + } else { + result->NotImplemented(); + } +} + +} // namespace keypress_simulator_windows diff --git a/keypress_simulator/packages/keypress_simulator_windows/windows/keypress_simulator_windows_plugin.h b/keypress_simulator/packages/keypress_simulator_windows/windows/keypress_simulator_windows_plugin.h new file mode 100644 index 00000000..654c59d1 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_windows/windows/keypress_simulator_windows_plugin.h @@ -0,0 +1,37 @@ +#ifndef FLUTTER_PLUGIN_KEYPRESS_SIMULATOR_WINDOWS_PLUGIN_H_ +#define FLUTTER_PLUGIN_KEYPRESS_SIMULATOR_WINDOWS_PLUGIN_H_ + +#include +#include + +#include + +namespace keypress_simulator_windows { + +class KeypressSimulatorWindowsPlugin : public flutter::Plugin { + public: + static void RegisterWithRegistrar(flutter::PluginRegistrarWindows* registrar); + + KeypressSimulatorWindowsPlugin(); + + virtual ~KeypressSimulatorWindowsPlugin(); + + // Disallow copy and assign. + KeypressSimulatorWindowsPlugin(const KeypressSimulatorWindowsPlugin&) = + delete; + KeypressSimulatorWindowsPlugin& operator=( + const KeypressSimulatorWindowsPlugin&) = delete; + + void KeypressSimulatorWindowsPlugin::SimulateKeyPress( + const flutter::MethodCall& method_call, + std::unique_ptr> result); + + // Called when a method is called on this plugin's channel from Dart. + void HandleMethodCall( + const flutter::MethodCall& method_call, + std::unique_ptr> result); +}; + +} // namespace keypress_simulator_windows + +#endif // FLUTTER_PLUGIN_KEYPRESS_SIMULATOR_WINDOWS_PLUGIN_H_ diff --git a/keypress_simulator/packages/keypress_simulator_windows/windows/keypress_simulator_windows_plugin_c_api.cpp b/keypress_simulator/packages/keypress_simulator_windows/windows/keypress_simulator_windows_plugin_c_api.cpp new file mode 100644 index 00000000..5c6fb9ba --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_windows/windows/keypress_simulator_windows_plugin_c_api.cpp @@ -0,0 +1,12 @@ +#include "include/keypress_simulator_windows/keypress_simulator_windows_plugin_c_api.h" + +#include + +#include "keypress_simulator_windows_plugin.h" + +void KeypressSimulatorWindowsPluginCApiRegisterWithRegistrar( + FlutterDesktopPluginRegistrarRef registrar) { + keypress_simulator_windows::KeypressSimulatorWindowsPlugin::RegisterWithRegistrar( + flutter::PluginRegistrarManager::GetInstance() + ->GetRegistrar(registrar)); +} diff --git a/keypress_simulator/packages/keypress_simulator_windows/windows/test/keypress_simulator_windows_plugin_test.cpp b/keypress_simulator/packages/keypress_simulator_windows/windows/test/keypress_simulator_windows_plugin_test.cpp new file mode 100644 index 00000000..8e051899 --- /dev/null +++ b/keypress_simulator/packages/keypress_simulator_windows/windows/test/keypress_simulator_windows_plugin_test.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "keypress_simulator_windows_plugin.h" + +namespace keypress_simulator_windows { +namespace test { + +namespace { + +using flutter::EncodableMap; +using flutter::EncodableValue; +using flutter::MethodCall; +using flutter::MethodResultFunctions; + +} // namespace + +TEST(KeypressSimulatorWindowsPlugin, GetPlatformVersion) { + KeypressSimulatorWindowsPlugin plugin; + // Save the reply value from the success callback. + std::string result_string; + plugin.HandleMethodCall( + MethodCall("getPlatformVersion", std::make_unique()), + std::make_unique>( + [&result_string](const EncodableValue* result) { + result_string = std::get(*result); + }, + nullptr, nullptr)); + + // Since the exact string varies by host, just ensure that it's a string + // with the expected format. + EXPECT_TRUE(result_string.rfind("Windows ", 0) == 0); +} + +} // namespace test +} // namespace keypress_simulator_windows diff --git a/keypress_simulator/pubspec.yaml b/keypress_simulator/pubspec.yaml new file mode 100644 index 00000000..9d261665 --- /dev/null +++ b/keypress_simulator/pubspec.yaml @@ -0,0 +1,9 @@ +name: keypress_simulator_workspace +homepage: https://github.com/leanflutter/keypress_simulator +publish_to: none + +environment: + sdk: ">=3.0.0 <4.0.0" + +dev_dependencies: + melos: ^3.1.0 diff --git a/pubspec.lock b/pubspec.lock index cf8dde8f..933d20d4 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -386,10 +386,9 @@ packages: keypress_simulator: dependency: "direct main" description: - name: keypress_simulator - sha256: d5aa5ed472b6b396f41fd6dcee99f4afb2c7ac6202af622b4cec7955de6ed7f6 - url: "https://pub.dev" - source: hosted + path: "keypress_simulator/packages/keypress_simulator" + relative: true + source: path version: "0.2.0" keypress_simulator_macos: dependency: transitive diff --git a/pubspec.yaml b/pubspec.yaml index dc797b3f..d49cd3eb 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -18,7 +18,8 @@ dependencies: dartx: any image_picker: ^1.1.2 pointycastle: any - keypress_simulator: ^0.2.0 + keypress_simulator: + path: keypress_simulator/packages/keypress_simulator shared_preferences: ^2.5.3 flex_color_scheme: ^8.2.0 package_info_plus: ^8.3.0