├── .gitignore ├── LICENSE ├── README.md ├── build ├── Robot │ ├── .gitignore │ ├── robot.php │ ├── robot.v │ ├── robot_php.h │ └── vrobot │ │ ├── .gitignore │ │ ├── mouse.v │ │ ├── screen.v │ │ ├── tweens.v │ │ ├── types.v │ │ ├── utils.v │ │ └── vrobot.v └── keyboard │ ├── .gitignore │ ├── keyboard.cc │ ├── keyboard.cmd │ ├── keyboard.h │ ├── keyboard.php │ └── keyboard_php.h ├── composer.json └── src ├── KeyBoard.php ├── Mouse.php ├── Robot.php ├── Screen.php ├── keyboard.dll ├── keyboard_php.h ├── robot.dll └── robot_php.h /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | Thumbs.db 6 | /.idea 7 | /.vscode 8 | /.settings 9 | /.buildpath 10 | /.project 11 | .phpunit.result.cache 12 | /test -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php-windows-robot 2 | PHP Windows的桌面自动化 3 | 4 | composer 5 | ```shell 6 | composer require kingbes/phprobot 7 | ``` 8 | 9 | # 要求 10 | 11 | php `>=8.1.0` 12 | 13 | 拓展 `FFI` 14 | 15 | 系统 `Windows` 16 | 17 | # 文档 18 | 19 | ## 鼠标 20 | 21 | ```php 22 | use KingBes\PhpRobot\Mouse; 23 | 24 | $Mouse = new Mouse(); 25 | ``` 26 | 27 | ```php 28 | /** 29 | * 鼠标指针位置 function 30 | * 31 | * @return array 32 | */ 33 | public function mouse_pos(): array 34 | {} 35 | 36 | /** 37 | * 鼠标单击 function 38 | * 39 | * @param string $button left right middle 40 | * @return self 41 | */ 42 | public function mouse_click(string $button): self 43 | {} 44 | 45 | /** 46 | * 鼠标双击 function 47 | * 48 | * @param string $button left right middle 49 | * @return self 50 | */ 51 | public function mouse_double_click(string $button): self 52 | {} 53 | 54 | /** 55 | * 将鼠标(左)拖动到指定位置。 function 56 | * 57 | * @param integer $x 58 | * @param integer $y 59 | * @return self 60 | */ 61 | public function mouse_left_drag(int $x, int $y): self 62 | {} 63 | 64 | /** 65 | * 相对于当前位置拖动鼠标(左)。 function 66 | * 67 | * @param integer $offset_x 68 | * @param integer $offset_y 69 | * @return self 70 | */ 71 | public function mouse_drag_rel(int $offset_x, int $offset_y): self 72 | {} 73 | 74 | /** 75 | * 移动鼠标到 x y function 76 | * 77 | * @param integer $x 78 | * @param integer $y 79 | * @return self 80 | */ 81 | public function mouse_move_mouse(int $x, int $y): self 82 | {} 83 | 84 | /** 85 | * 相对于当前位置移动鼠标。 function 86 | * 87 | * @param integer $offset_x 88 | * @param integer $offset_y 89 | * @return self 90 | */ 91 | public function move_mouse_rel(int $offset_x, int $offset_y): self 92 | {} 93 | 94 | /** 95 | * 鼠标平滑移动指定位置 function 96 | * 97 | * @param integer $x 98 | * @param integer $y 99 | * @param integer $duration_ms 毫秒 100 | * @param string $tween 预览补间 参数请看下面 `预览补间` 说明 101 | * @return self 102 | */ 103 | public function move_mouse_smooth(int $x, int $y, int $duration_ms, string $tween): self 104 | {} 105 | 106 | /** 107 | * 当前平滑移动鼠标 function 108 | * 109 | * @param integer $offset_x 110 | * @param integer $offset_y 111 | * @param integer $duration_ms 毫秒 112 | * @param string $tween 预览补间 参数请看下面 `预览补间` 说明 113 | * @return self 114 | */ 115 | public function move_mouse_smooth_rel(int $offset_x, int $offset_y, int $duration_ms, string $tween): self 116 | {} 117 | 118 | /** 119 | * 鼠标按下 function 120 | * 121 | * @param string $button left right middle 122 | * @return self 123 | */ 124 | public function mouse_down(string $button): self 125 | {} 126 | 127 | /** 128 | * 鼠标弹起 function 129 | * 130 | * @param string $button left right middle 131 | * @return self 132 | */ 133 | public function mouse_up(string $button): self 134 | {} 135 | ``` 136 | 137 | ## 屏幕 138 | 139 | ```php 140 | use KingBes\PhpRobot\Screen; 141 | 142 | $Screen = new Screen(); 143 | ``` 144 | 145 | ```php 146 | /** 147 | * 获取屏幕指定位置的颜色rgb function 148 | * 149 | * @param integer $x 150 | * @param integer $y 151 | * @return array 152 | */ 153 | public function pixel_color(int $x, int $y): array 154 | {} 155 | 156 | /** 157 | * 获取屏幕大小 function 158 | * 159 | * @return array 160 | */ 161 | public function screen_size(): array 162 | {} 163 | ``` 164 | 165 | ## 键盘 166 | 167 | ```php 168 | use KingBes\PhpRobot\Keyboard; 169 | 170 | $Keyboard = new Keyboard; 171 | ``` 172 | 173 | ```php 174 | /** 175 | * 是否点击键盘某键 function 176 | * 177 | * @param integer $key 整数键码值 178 | * @return boolean 179 | */ 180 | public function isKeyPressed(int $key): bool 181 | {} 182 | 183 | /** 184 | * 点击键盘某键 function 185 | * 186 | * @param integer $key 整数键码值 187 | * @return void 188 | */ 189 | public function onClickKey(int $key): void 190 | {} 191 | 192 | /** 193 | * 按下键盘某键 function 194 | * 195 | * @param integer $key 196 | * @return self 197 | */ 198 | public function pressKey(int $key): self 199 | {} 200 | 201 | /** 202 | * 弹起键盘某键 function 203 | * 204 | * @param integer $key 205 | * @return self 206 | */ 207 | public function releaseKey(int $key): self 208 | {} 209 | ``` 210 | 211 | 212 | # 实例一 `获取当前鼠标位置` 213 | 214 | ```php 215 | // 引入 216 | use KingBes\PhpRobot\Mouse; 217 | 218 | // 实例 219 | $Mouse = new Mouse(); 220 | // 获取鼠标当前指针位置 221 | $pos = $Mouse->mouse_pos(); 222 | 223 | var_dump($pos); 224 | ``` 225 | 226 | # 实例一点二 `鼠标按下和弹起` 227 | 228 | ```php 229 | // 引入 230 | use KingBes\PhpRobot\Mouse; 231 | 232 | sleep(3);// 等待 233 | // 按下 左键 234 | $Mouse->mouse_down("left"); 235 | 236 | sleep(3);// 等待 237 | // 弹起 左键 238 | $Mouse->mouse_up("left"); 239 | ``` 240 | 241 | # 实例二 `监听键盘A键` 242 | 243 | ```php 244 | use KingBes\PhpRobot\Keyboard; 245 | 246 | $Keyboard = new Keyboard; 247 | 248 | while (true) { 249 | if ($Keyboard->isKeyPressed(65)) { 250 | echo "点击了键盘A \n"; 251 | } 252 | usleep(100); // 减轻负担 253 | } 254 | ``` 255 | 256 | # 实例三 `按下键盘A键` 257 | 258 | ```php 259 | use KingBes\PhpRobot\Keyboard; 260 | 261 | $Keyboard = new Keyboard; 262 | 263 | sleep(5); //延迟5秒 264 | 265 | $Keyboard->onClickKey(65) 266 | ``` 267 | 268 | # 实例三点二 `按下和弹起键盘某键` 269 | 270 | ```php 271 | use KingBes\PhpRobot\Keyboard; 272 | 273 | $Keyboard = new Keyboard; 274 | 275 | sleep(3);// 等待 276 | // 按下 277 | $Keyboard->pressKey(65); 278 | 279 | sleep(3);// 等待 280 | // 弹起 281 | $Keyboard->releaseKey(65); 282 | ``` 283 | 284 | # 预览补间 `可用的鼠标补间` 285 | 286 | 你可以在这里预览补间: https://easings.net/ 287 | 288 | - `linear` 289 | - `ease_in_quad` 290 | - `ease_out_quad` 291 | - `ease_in_out_quad` 292 | - `ease_in_cubic` 293 | - `ease_out_cubic` 294 | - `ease_in_out_cubic` 295 | - `ease_in_quart` 296 | - `ease_out_quart` 297 | - `ease_in_out_quart` 298 | - `ease_in_quint` 299 | - `ease_out_quint` 300 | - `ease_in_out_quint` 301 | - `ease_in_sine` 302 | - `ease_out_sine` 303 | - `ease_in_out_sine` 304 | - `ease_in_expo` 305 | - `ease_out_expo` 306 | - `ease_in_out_expo` 307 | - `ease_in_circ` 308 | - `ease_out_circ` 309 | - `ease_in_out_circ` 310 | - `ease_in_elastic` 311 | - `ease_out_elastic` 312 | - `ease_in_out_elastic` 313 | - `ease_in_back` 314 | - `ease_out_back` 315 | - `ease_in_out_back` 316 | - `ease_in_bounce` 317 | - `ease_out_bounce` 318 | - `ease_in_out_bounce` 319 | 320 | 321 | # 键盘的整数键码值 322 | 323 | 在 Windows 操作系统中,键盘上的一些按键对应着特定的整数键码值(keyCode)。以下是一些常见的 win 键盘按键及其对应的十进制数字: 324 | - **字母和数字键**: 325 | - `a`:65 326 | - `b`:66 327 | - `c`:67 328 | - `d`:68 329 | - `e`:69 330 | - `f`:70 331 | - `g`:71 332 | - `h`:72 333 | - `i`:73 334 | - `j`:74 335 | - `k`:75 336 | - `l`:76 337 | - `m`:77 338 | - `n`:78 339 | - `o`:79 340 | - `p`:80 341 | - `q`:81 342 | - `r`:82 343 | - `s`:83 344 | - `t`:84 345 | - `u`:85 346 | - `v`:86 347 | - `w`:87 348 | - `x`:88 349 | - `y`:89 350 | - `z`:90 351 | - `0`:48 352 | - `1`:49 353 | - `2`:50 354 | - `3`:51 355 | - `4`:52 356 | - `5`:53 357 | - `6`:54 358 | - `7`:55 359 | - `8`:56 360 | - `9`:57 361 | - **控制键**: 362 | - `Backspace`:8 363 | - `Tab`:9 364 | - `Clear`:12 365 | - `Enter`:13 366 | - `Shift`:16 367 | - `Control`:17 368 | - `Alt`:18 369 | - `Caps Lock`:20 370 | - `Esc`:27 371 | - `Spacebar`:32 372 | - `Page Up`:33 373 | - `Page Down`:34 374 | - `End`:35 375 | - `Home`:36 376 | - `Left Arrow`:37 377 | - `Up Arrow`:38 378 | - `Right Arrow`:39 379 | - `Down Arrow`:40 380 | - `Insert`:45 381 | - `Delete`:46 382 | - `Num Lock`:144 383 | - **数字键盘上的键**: 384 | - `0`:96 385 | - `1`:97 386 | - `2`:98 387 | - `3`:99 388 | - `4`:100 389 | - `5`:101 390 | - `6`:102 391 | - `7`:103 392 | - `8`:104 393 | - `9`:105 394 | - `*`:106 395 | - `+`:107 396 | - `Enter`:108 397 | - `-`:109 398 | - `.`:110 399 | - `/`:111 400 | - **其他键**: 401 | - `Left Windows 键`:91 402 | - `Right Windows 键`:92 403 | - `Applications 键`(右 Ctrl 左边键,点击相当于点击鼠标右键,会弹出快捷菜单):93 404 | -------------------------------------------------------------------------------- /build/Robot/.gitignore: -------------------------------------------------------------------------------- 1 | robot.dll -------------------------------------------------------------------------------- /build/Robot/robot.php: -------------------------------------------------------------------------------- 1 | ffi = FFI::cdef($header, "robot.dll"); 18 | } 19 | } 20 | 21 | /** 22 | * 鼠标 class 23 | */ 24 | class Mouse extends Robot 25 | { 26 | /** 27 | * 鼠标指针位置 function 28 | * 29 | * @return array 30 | */ 31 | public function mouse_pos(): array 32 | { 33 | $pos = $this->ffi->mouse_pos(); 34 | return ["x" => $pos->x, "y" => $pos->y]; 35 | } 36 | 37 | /** 38 | * 鼠标单击 function 39 | * 40 | * @param string $button left right middle 41 | * @return self 42 | */ 43 | public function mouse_click(string $button): self 44 | { 45 | $this->ffi->mouse_click($button); 46 | return $this; 47 | } 48 | 49 | /** 50 | * 鼠标双击 function 51 | * 52 | * @param string $button left right middle 53 | * @return self 54 | */ 55 | public function mouse_double_click(string $button): self 56 | { 57 | $this->ffi->mouse_double_click($button); 58 | return $this; 59 | } 60 | 61 | /** 62 | * 将鼠标(左)拖动到指定位置。 function 63 | * 64 | * @param integer $x 65 | * @param integer $y 66 | * @return self 67 | */ 68 | public function mouse_left_drag(int $x, int $y): self 69 | { 70 | $this->ffi->mouse_left_drag($x, $y); 71 | return $this; 72 | } 73 | 74 | /** 75 | * 相对于当前位置拖动鼠标(左)。 function 76 | * 77 | * @param integer $offset_x 78 | * @param integer $offset_y 79 | * @return self 80 | */ 81 | public function mouse_drag_rel(int $offset_x, int $offset_y): self 82 | { 83 | $this->ffi->mouse_drag_rel($offset_x, $offset_y); 84 | return $this; 85 | } 86 | 87 | /** 88 | * 移动鼠标到 x y function 89 | * 90 | * @param integer $x 91 | * @param integer $y 92 | * @return self 93 | */ 94 | public function mouse_move_mouse(int $x, int $y): self 95 | { 96 | $this->ffi->mouse_move_mouse($x, $y); 97 | return $this; 98 | } 99 | 100 | /** 101 | * 相对于当前位置移动鼠标。 function 102 | * 103 | * @param integer $offset_x 104 | * @param integer $offset_y 105 | * @return self 106 | */ 107 | public function move_mouse_rel(int $offset_x, int $offset_y): self 108 | { 109 | $this->ffi->move_mouse_rel($offset_x, $offset_y); 110 | return $this; 111 | } 112 | 113 | public function move_mouse_smooth(int $x, int $y, int $duration_ms, string $tween): self 114 | { 115 | $start_pos = $this->ffi->mouse_pos(); 116 | $dist_x = $x - $start_pos->x; 117 | $dist_y = $y - $start_pos->y; 118 | $steps = intval(max(50, ($duration_ms) / 10)); 119 | $dt = intval(($duration_ms) / $steps); 120 | $i = 0; 121 | $factor = 0.0; 122 | while ($i < $steps) { 123 | $factor = $this->ffi->factor($tween, $i, $steps); 124 | $current_x = intval($start_pos->x + $factor * $dist_x); 125 | $current_y = intval($start_pos->y + $factor * $dist_y); 126 | $this->ffi->move_mouse($current_x, $current_y); 127 | usleep($dt); 128 | $i++; 129 | } 130 | 131 | return $this; 132 | } 133 | 134 | public function mouse_down(string $button): self 135 | { 136 | $this->ffi->mouse_down($button); 137 | return $this; 138 | } 139 | 140 | public function mouse_up(string $button): self 141 | { 142 | $this->ffi->mouse_up($button); 143 | return $this; 144 | } 145 | } 146 | 147 | /** 148 | * 屏幕 class 149 | */ 150 | class Screen extends Robot 151 | { 152 | /** 153 | * 获取屏幕指定位置的颜色rgb function 154 | * 155 | * @param integer $x 156 | * @param integer $y 157 | * @return array 158 | */ 159 | public function pixel_color(int $x, int $y): array 160 | { 161 | $rgb = $this->ffi->pixel_color($x, $y); 162 | return [ 163 | "r" => $rgb->r, 164 | "g" => $rgb->g, 165 | "b" => $rgb->b 166 | ]; 167 | } 168 | 169 | /** 170 | * 获取屏幕大小 function 171 | * 172 | * @return array 173 | */ 174 | public function screen_size(): array 175 | { 176 | $size = $this->ffi->screen_size(); 177 | return [ 178 | "width" => $size->width, 179 | "height" => $size->height 180 | ]; 181 | } 182 | } 183 | 184 | $Mouse = new Mouse(); 185 | sleep(3); 186 | $Mouse->mouse_down("left"); 187 | 188 | sleep(3); 189 | $Mouse->mouse_up("left"); 190 | 191 | // var_dump($size); 192 | -------------------------------------------------------------------------------- /build/Robot/robot.v: -------------------------------------------------------------------------------- 1 | module main 2 | 3 | import vrobot 4 | import time 5 | 6 | struct Pos { 7 | x int 8 | y int 9 | } 10 | 11 | // 鼠标指针位置 12 | @[export: 'mouse_pos'] 13 | fn mouse_pos() Pos { 14 | p := vrobot.mouse_pos() 15 | return Pos{p.x, p.y} 16 | } 17 | 18 | // 鼠标单击 left right middle 19 | @[export: 'mouse_click'] 20 | fn mouse_click(button &char) { 21 | vrobot.click(unsafe { button.vstring() }) 22 | } 23 | 24 | // 鼠标双击 left right middle 25 | @[export: 'mouse_double_click'] 26 | fn mouse_double_click(button &char) { 27 | vrobot.double_click(unsafe { button.vstring() }) 28 | } 29 | 30 | // 将鼠标(左)拖动到指定位置。 31 | @[export: 'mouse_left_drag'] 32 | fn mouse_left_drag(x int, y int) { 33 | vrobot.drag(x, y) 34 | } 35 | 36 | // 相对于当前位置拖动鼠标(左)。 37 | @[export: 'mouse_drag_rel'] 38 | fn mouse_drag_rel(offset_x int, offset_y int) { 39 | vrobot.drag_rel(offset_x, offset_y) 40 | } 41 | 42 | // 移动鼠标到 x y 43 | @[export: 'mouse_move_mouse'] 44 | fn mouse_move_mouse(x int, y int) { 45 | vrobot.move_mouse(x, y) 46 | } 47 | 48 | // 相对于当前位置移动鼠标。 49 | @[export: 'move_mouse_rel'] 50 | fn move_mouse_rel(offset_x int, offset_y int) { 51 | vrobot.move_mouse_rel(offset_x, offset_y) 52 | } 53 | 54 | struct Color { 55 | r int 56 | g int 57 | b int 58 | } 59 | 60 | // 获取屏幕指定位置的颜色rgb 61 | @[export: 'pixel_color'] 62 | fn pixel_color(x int, y int) Color { 63 | p := vrobot.pixel_color(x, y) 64 | return Color{p.r, p.g, p.b} 65 | } 66 | 67 | struct Size { 68 | width int 69 | height int 70 | } 71 | 72 | // 获取屏幕大小 73 | @[export: 'screen_size'] 74 | fn screen_size() Size { 75 | s := vrobot.screen_size() 76 | return Size{s.width, s.height} 77 | } 78 | 79 | // 鼠标平滑移动 80 | @[export: 'move_mouse_smooth'] 81 | fn move_mouse_smooth(x int, y int, duration_ms int, tween &char) { 82 | t := unsafe { cstring_to_vstring(tween) } 83 | vrobot.move_mouse_smooth(x, y, duration_ms, t) 84 | } 85 | 86 | @[export: 'move_mouse'] 87 | fn move_mouse(x int, y int) { 88 | vrobot.move_mouse(x, y) 89 | } 90 | 91 | @[export: 'factor'] 92 | fn factor(tween &char, i int,steps int) f64 { 93 | n := vrobot.normalize(i, 0, steps) 94 | t := unsafe { cstring_to_vstring(tween) } 95 | m := match t { 96 | 'linear' { n } 97 | 'ease_in_quad' { vrobot.ease_in_quad(n) } 98 | 'ease_out_quad' { vrobot.ease_out_quad(n) } 99 | 'ease_in_out_quad' { vrobot.ease_in_out_quad(n) } 100 | 'ease_in_cubic' { vrobot.ease_in_cubic(n) } 101 | 'ease_out_cubic' { vrobot.ease_out_cubic(n) } 102 | 'ease_in_out_cubic' { vrobot.ease_in_out_cubic(n) } 103 | 'ease_in_quart' { vrobot.ease_in_quart(n) } 104 | 'ease_out_quart' { vrobot.ease_out_quart(n) } 105 | 'ease_in_out_quart' { vrobot.ease_in_out_quart(n) } 106 | 'ease_in_quint' { vrobot.ease_in_quint(n) } 107 | 'ease_out_quint' { vrobot.ease_out_quint(n) } 108 | 'ease_in_out_quint' { vrobot.ease_in_out_quint(n) } 109 | 'ease_in_sine' { vrobot.ease_in_sine(n) } 110 | 'ease_out_sine' { vrobot.ease_out_sine(n) } 111 | 'ease_in_out_sine' { vrobot.ease_in_out_sine(n) } 112 | 'ease_in_expo' { vrobot.ease_in_expo(n) } 113 | 'ease_out_expo' { vrobot.ease_out_expo(n) } 114 | 'ease_in_out_expo' { vrobot.ease_in_out_expo(n) } 115 | 'ease_in_circ' { vrobot.ease_in_circ(n) } 116 | 'ease_out_circ' { vrobot.ease_out_circ(n) } 117 | 'ease_in_out_circ' { vrobot.ease_in_out_circ(n) } 118 | 'ease_in_elastic' { vrobot.ease_in_elastic(n) } 119 | 'ease_out_elastic' { vrobot.ease_out_elastic(n) } 120 | 'ease_in_out_elastic' { vrobot.ease_in_out_elastic(n) } 121 | 'ease_in_back' { vrobot.ease_in_back(n) } 122 | 'ease_out_back' { vrobot.ease_out_back(n) } 123 | 'ease_in_out_back' { vrobot.ease_in_out_back(n) } 124 | 'ease_in_bounce' { vrobot.ease_in_bounce(n) } 125 | 'ease_out_bounce' { vrobot.ease_out_bounce(n) } 126 | 'ease_in_out_bounce' { vrobot.ease_in_out_bounce(n) } 127 | else { panic('Tween not found') } 128 | } 129 | return m 130 | } 131 | 132 | // 按下鼠标 133 | @[export: 'mouse_down'] 134 | fn mouse_down(button &char) 135 | { 136 | vrobot.mouse_down(unsafe { button.vstring() }) 137 | } 138 | 139 | // 弹起鼠标 140 | @[export: 'mouse_up'] 141 | fn mouse_up(button &char) 142 | { 143 | vrobot.mouse_up(unsafe { button.vstring() }) 144 | } 145 | 146 | /* fn main() { 147 | vrobot.mouse_down('left') 148 | vrobot.mouse_up('left') 149 | } */ 150 | -------------------------------------------------------------------------------- /build/Robot/robot_php.h: -------------------------------------------------------------------------------- 1 | // 鼠标 2 | typedef struct 3 | { 4 | int x; 5 | int y; 6 | } Pos; 7 | 8 | Pos mouse_pos(); 9 | void mouse_click(const char *button); 10 | void mouse_double_click(const char *button); 11 | void mouse_left_drag(int x, int y); 12 | void mouse_drag_rel(int offset_x, int offset_y); 13 | void mouse_move_mouse(int x, int y); 14 | void move_mouse_rel(int offset_x, int offset_y); 15 | void mouse_down(const char *button); 16 | void mouse_up(const char *button); 17 | 18 | void move_mouse(int x, int y); 19 | double factor(const char *tween, int i, int steps); 20 | 21 | // 屏幕 22 | typedef struct 23 | { 24 | int r; 25 | int g; 26 | int b; 27 | } Color; 28 | Color pixel_color(int x, int y); 29 | typedef struct 30 | { 31 | int width; 32 | int height; 33 | } Size; 34 | Size screen_size(); -------------------------------------------------------------------------------- /build/Robot/vrobot/.gitignore: -------------------------------------------------------------------------------- 1 | vrobot.exe 2 | vrobot -------------------------------------------------------------------------------- /build/Robot/vrobot/mouse.v: -------------------------------------------------------------------------------- 1 | module vrobot 2 | 3 | import math 4 | import time 5 | 6 | 7 | 8 | fn send_mouse_event(x int) { 9 | C.mouse_event(x, 0, 0, 0, 0) 10 | } 11 | 12 | pub fn mouse_down(button string) { 13 | match button { 14 | 'left' { send_mouse_event(C.MOUSEEVENTF_LEFTDOWN) } 15 | 'right' { send_mouse_event(C.MOUSEEVENTF_RIGHTDOWN) } 16 | 'middle' { send_mouse_event(C.MOUSEEVENTF_MIDDLEDOWN) } 17 | else { panic('Invalid mouse button') } 18 | } 19 | } 20 | 21 | pub fn mouse_up(button string) { 22 | match button { 23 | 'left' { send_mouse_event(C.MOUSEEVENTF_LEFTUP) } 24 | 'right' { send_mouse_event(C.MOUSEEVENTF_RIGHTUP) } 25 | 'middle' { send_mouse_event(C.MOUSEEVENTF_MIDDLEUP) } 26 | else { panic('Invalid mouse button') } 27 | } 28 | } 29 | 30 | pub fn click(button string) { 31 | mouse_down(button) 32 | mouse_up(button) 33 | } 34 | 35 | pub fn double_click(button string) { 36 | click(button) 37 | click(button) 38 | } 39 | 40 | pub fn drag(x int, y int) { 41 | mouse_down('left') 42 | move_mouse(x, y) 43 | mouse_up('left') 44 | } 45 | 46 | pub fn drag_rel(offset_x int, offset_y int) { 47 | start_pos := mouse_pos() 48 | dest_x := start_pos.x + offset_x 49 | dest_y := start_pos.y + offset_y 50 | 51 | mouse_down('left') 52 | move_mouse(dest_x, dest_y) 53 | mouse_up('left') 54 | } 55 | 56 | pub fn move_mouse(x int, y int) { 57 | C.SetCursorPos(x, y) 58 | } 59 | 60 | pub fn move_mouse_rel(offset_x int, offset_y int) { 61 | start_pos := mouse_pos() 62 | dest_x := start_pos.x + offset_x 63 | dest_y := start_pos.y + offset_y 64 | move_mouse(dest_x, dest_y) 65 | } 66 | 67 | pub fn move_mouse_smooth(x int, y int, duration_ms int, tween string) { 68 | start_pos := mouse_pos() 69 | dist_x := f64(x) - start_pos.x 70 | dist_y := f64(y) - start_pos.y 71 | // dist := math.sqrt(math.pow(x - start_pos.x, 2) + math.pow(y - start_pos.y, 2)) 72 | steps := int(math.max(50.0, duration_ms * 1000 / 5)) 73 | dt := int(f64(duration_ms * 1000) / f64(steps)) 74 | 75 | mut factor := 0.0 76 | mut i := 0 77 | 78 | for i < steps { 79 | n := normalize(i, 0, steps) 80 | 81 | // TODO there must be better way to do this 82 | factor = match tween { 83 | 'linear' { n } 84 | 'ease_in_quad' { ease_in_quad(n) } 85 | 'ease_out_quad' { ease_out_quad(n) } 86 | 'ease_in_out_quad' { ease_in_out_quad(n) } 87 | 'ease_in_cubic' { ease_in_cubic(n) } 88 | 'ease_out_cubic' { ease_out_cubic(n) } 89 | 'ease_in_out_cubic' { ease_in_out_cubic(n) } 90 | 'ease_in_quart' { ease_in_quart(n) } 91 | 'ease_out_quart' { ease_out_quart(n) } 92 | 'ease_in_out_quart' { ease_in_out_quart(n) } 93 | 'ease_in_quint' { ease_in_quint(n) } 94 | 'ease_out_quint' { ease_out_quint(n) } 95 | 'ease_in_out_quint' { ease_in_out_quint(n) } 96 | 'ease_in_sine' { ease_in_sine(n) } 97 | 'ease_out_sine' { ease_out_sine(n) } 98 | 'ease_in_out_sine' { ease_in_out_sine(n) } 99 | 'ease_in_expo' { ease_in_expo(n) } 100 | 'ease_out_expo' { ease_out_expo(n) } 101 | 'ease_in_out_expo' { ease_in_out_expo(n) } 102 | 'ease_in_circ' { ease_in_circ(n) } 103 | 'ease_out_circ' { ease_out_circ(n) } 104 | 'ease_in_out_circ' { ease_in_out_circ(n) } 105 | 'ease_in_elastic' { ease_in_elastic(n) } 106 | 'ease_out_elastic' { ease_out_elastic(n) } 107 | 'ease_in_out_elastic' { ease_in_out_elastic(n) } 108 | 'ease_in_back' { ease_in_back(n) } 109 | 'ease_out_back' { ease_out_back(n) } 110 | 'ease_in_out_back' { ease_in_out_back(n) } 111 | 'ease_in_bounce' { ease_in_bounce(n) } 112 | 'ease_out_bounce' { ease_out_bounce(n) } 113 | 'ease_in_out_bounce' { ease_in_out_bounce(n) } 114 | else { panic('Tween not found') } 115 | } 116 | println("factor:${factor}") 117 | current_x := int(f64(start_pos.x) + factor * dist_x) 118 | current_y := int(f64(start_pos.y) + factor * dist_y) 119 | 120 | move_mouse(current_x, current_y) 121 | time.sleep(dt) 122 | i++ 123 | } 124 | } 125 | 126 | pub fn move_mouse_smooth_rel(offset_x int, offset_y int, duration_ms int, tween string) { 127 | start_pos := mouse_pos() 128 | dest_x := start_pos.x + offset_x 129 | dest_y := start_pos.y + offset_y 130 | move_mouse_smooth(dest_x, dest_y, duration_ms, tween) 131 | } 132 | 133 | pub fn mouse_pos() Point { 134 | p := Point{} 135 | cursor := C.GetCursorPos(&p) 136 | 137 | if !cursor { 138 | panic('No cursor found') 139 | } 140 | 141 | return p 142 | } 143 | -------------------------------------------------------------------------------- /build/Robot/vrobot/screen.v: -------------------------------------------------------------------------------- 1 | module vrobot 2 | 3 | pub fn pixel_color(x int, y int) Color { 4 | hdc := C.GetDC(C.NULL) 5 | 6 | if hdc == C.NULL { 7 | panic('Could not get device context handle') 8 | } 9 | 10 | mut pixel := i32(0) 11 | pixel = C.GetPixel(hdc, x, y) 12 | C.ReleaseDC(C.GetDesktopWindow(), hdc) 13 | 14 | color := Color{ 15 | r: get_r_value(pixel), 16 | g: get_g_value(pixel), 17 | b: get_b_value(pixel) 18 | } 19 | 20 | return color 21 | } 22 | 23 | pub fn screen_size() Size { 24 | width := int(C.GetSystemMetrics(C.SM_CXSCREEN)) 25 | height := int(C.GetSystemMetrics(C.SM_CYSCREEN)) 26 | 27 | return Size{ 28 | width: width, 29 | height: height 30 | } 31 | } 32 | 33 | /* 34 | pub fn screenshot(x, y, width, height int, filename string) { 35 | hdc := C.CreateCompatibleDC(0) 36 | 37 | // make a bmp in memory to store the capture in 38 | hbmp := C.CreateCompatibleBitmap(C.GetDC(0), width, height) 39 | 40 | // join em up 41 | C.SelectObject(hdc, hbmp) 42 | 43 | // copy from the screen to my bitmap 44 | C.BitBlt(hdc, 0, 0, width, height, C.GetDC(0), x, y, C.SRCCOPY) 45 | 46 | // save my bitmap 47 | ret := C.SaveBMPFile(filename, hbmp, hdc, width, height) 48 | 49 | // free the bitmap memory 50 | C.DeleteObject(hbmp) 51 | } 52 | */ -------------------------------------------------------------------------------- /build/Robot/vrobot/tweens.v: -------------------------------------------------------------------------------- 1 | /* 2 | Thanks "pytweening" for these functions! 3 | (https://github.com/asweigart/pytweening) 4 | */ 5 | module vrobot 6 | 7 | import math 8 | 9 | fn check_valid_range(n f64) { 10 | if n < 0 || n > 1 { 11 | panic('Not valid range. Should be between 0.0 - 1.0') 12 | } 13 | } 14 | 15 | pub fn ease_in_quad(n f64) f64 { 16 | check_valid_range(n) 17 | return math.pow(n, 2) 18 | } 19 | 20 | pub fn ease_out_quad(n f64) f64 { 21 | check_valid_range(n) 22 | return -n * (n - 2) 23 | } 24 | 25 | pub fn ease_in_out_quad(n f64) f64 { 26 | check_valid_range(n) 27 | 28 | if n < 0.5 { 29 | return 2.0 * math.pow(n, 2) 30 | } else { 31 | b := n * 2.0 - 1.0 32 | return -0.5 * (b * (b - 2.0) - 1.0) 33 | } 34 | } 35 | 36 | pub fn ease_in_cubic(n f64) f64 { 37 | check_valid_range(n) 38 | return math.pow(n, 3) 39 | } 40 | 41 | pub fn ease_out_cubic(n f64) f64 { 42 | check_valid_range(n) 43 | return math.pow((n - 1), 3) + 1 44 | } 45 | 46 | pub fn ease_in_out_cubic(n f64) f64 { 47 | check_valid_range(n) 48 | mut b := 2.0 * n 49 | 50 | if b < 1 { 51 | return 0.5 * math.pow(b, 3) 52 | } else { 53 | b = b - 2 54 | return 0.5 * (math.pow(b, 3) + 2) 55 | } 56 | } 57 | 58 | pub fn ease_in_quart(n f64) f64 { 59 | check_valid_range(n) 60 | return math.pow(n, 4) 61 | } 62 | 63 | pub fn ease_out_quart(n f64) f64 { 64 | check_valid_range(n) 65 | return -(math.pow((n - 1), 4) - 1) 66 | } 67 | 68 | pub fn ease_in_out_quart(n f64) f64 { 69 | check_valid_range(n) 70 | mut b := n * 2 71 | 72 | if b < 1 { 73 | return 0.5 * math.pow(b, 4) 74 | } else { 75 | b -= 2 76 | return -0.5 * (math.pow(b, 4) - 2) 77 | } 78 | } 79 | 80 | pub fn ease_in_quint(n f64) f64 { 81 | check_valid_range(n) 82 | return math.pow(n, 5) 83 | } 84 | 85 | pub fn ease_out_quint(n f64) f64 { 86 | check_valid_range(n) 87 | return math.pow((n - 1), 5) + 1 88 | } 89 | 90 | pub fn ease_in_out_quint(n f64) f64 { 91 | check_valid_range(n) 92 | mut b := 2.0 * n 93 | 94 | if n < 1 { 95 | return 0.5 * math.pow(n, 5) 96 | } else { 97 | b = n - 2 98 | return 0.5 * (math.pow(n, 5) + 2) 99 | } 100 | } 101 | 102 | pub fn ease_in_sine(n f64) f64 { 103 | check_valid_range(n) 104 | return -1.0 * math.cos(n * math.pi / 2.0) + 1.0 105 | } 106 | 107 | 108 | pub fn ease_out_sine(n f64) f64 { 109 | check_valid_range(n) 110 | return math.sin(n * math.pi / 2) 111 | } 112 | 113 | pub fn ease_in_out_sine(n f64) f64 { 114 | check_valid_range(n) 115 | return -0.5 * (math.cos(math.pi * n) - 1) 116 | } 117 | 118 | pub fn ease_in_expo(n f64) f64 { 119 | check_valid_range(n) 120 | 121 | if n == 0 { 122 | return 0 123 | } else { 124 | return math.pow(2.0, (10.0 * (n - 1.0))) 125 | } 126 | } 127 | 128 | pub fn ease_out_expo(n f64) f64 { 129 | check_valid_range(n) 130 | 131 | if n == 1 { 132 | return 1 133 | } else { 134 | return -(math.pow(2.0, (-10.0 * n))) + 1.0 135 | } 136 | } 137 | 138 | pub fn ease_in_out_expo(n f64) f64 { 139 | check_valid_range(n) 140 | mut b := n 141 | 142 | if n == 0 { 143 | return 0 144 | } else if b == 1 { 145 | return 1 146 | } else { 147 | b = n * 2 148 | 149 | if n < 1 { 150 | return 0.5 * math.pow(2.0, (10.0 * (n - 1.0))) 151 | } else { 152 | b -= 1 153 | return 0.5 * (-1.0 * (math.pow(2.0, (-10.0 * n))) + 2.0) 154 | } 155 | } 156 | } 157 | 158 | pub fn ease_in_circ(n f64) f64 { 159 | check_valid_range(n) 160 | return -1.0 * (math.sqrt(1.0 - n * n) - 1.0) 161 | } 162 | 163 | pub fn ease_out_circ(n f64) f64 { 164 | check_valid_range(n) 165 | b := n - 1 166 | return math.sqrt(1.0 - (n * n)) 167 | } 168 | 169 | pub fn ease_in_out_circ(n f64) f64 { 170 | check_valid_range(n) 171 | mut b := n * 2 172 | 173 | if b < 1 { 174 | return -0.5 * (math.sqrt(1.0 - math.pow(b, 2.0)) - 1.0) 175 | } else { 176 | b = b - 2 177 | return 0.5 * (math.sqrt(1.0 - math.pow(b, 2.0)) + 1.0) 178 | } 179 | } 180 | 181 | pub fn ease_in_elastic(n f64) f64 { 182 | check_valid_range(n) 183 | amplitude := 1 184 | period := 0.3 185 | return 1.0 - ease_out_elastic(1.0 - n) 186 | } 187 | 188 | pub fn ease_out_elastic(n f64) f64 { 189 | check_valid_range(n) 190 | period := 0.3 191 | mut amplitude := 1.0 192 | mut s := 0.0 193 | 194 | if amplitude < 1 { 195 | amplitude = 1 196 | s = period / 4 197 | } else { 198 | s = period / (2.0 * math.pi) * math.asin(1.0 / amplitude) 199 | } 200 | 201 | return amplitude * math.pow(2.0, (-10.0 * n)) * math.sin((n - s) * (2.0 * math.pi / period)) + 1.0 202 | } 203 | 204 | pub fn ease_in_out_elastic(n f64) f64 { 205 | check_valid_range(n) 206 | amplitude := 1 207 | period := 0.5 208 | 209 | if n < 1 { 210 | return ease_in_elastic(n * 2) / 2 211 | } else { 212 | return ease_out_elastic((n * 2) - 1) / 2 + 0.5 213 | } 214 | } 215 | 216 | pub fn ease_in_back(n f64) f64 { 217 | check_valid_range(n) 218 | s := 1.70158 219 | return n * n * ((s + 1) * n - s) 220 | } 221 | 222 | pub fn ease_out_back(n f64) f64 { 223 | check_valid_range(n) 224 | s := 1.70158 225 | b := n - 1 226 | return b * b * ((s + 1) * b + s) + 1 227 | } 228 | 229 | pub fn ease_in_out_back(n f64) f64 { 230 | check_valid_range(n) 231 | s := 1.70158 232 | mut b := n * 2 233 | mut c := s 234 | 235 | if n < 1 { 236 | c *= 1.525 237 | return 0.5 * (b * b * ((c + 1) * b - c)) 238 | } else { 239 | b -= 2 240 | c *= 1.525 241 | return 0.5 * (b * b * ((c + 1) * b + c) + 2) 242 | } 243 | } 244 | 245 | pub fn ease_in_bounce(n f64) f64 { 246 | check_valid_range(n) 247 | return 1.0 - ease_out_bounce(1.0 - n) 248 | } 249 | 250 | pub fn ease_out_bounce(n f64) f64 { 251 | check_valid_range(n) 252 | mut b := n 253 | 254 | if b < (1.0/2.75) { 255 | return 7.5625 * b * b 256 | } else if b < (2.0/2.75) { 257 | b -= (1.5/2.75) 258 | return 7.5625 * b * b + 0.75 259 | } else if n < (2.5/2.75) { 260 | b -= (2.25/2.75) 261 | return 7.5625 * b * b + 0.9375 262 | } else { 263 | b -= (2.65/2.75) 264 | return 7.5625 * b * b + 0.984375 265 | } 266 | } 267 | 268 | pub fn ease_in_out_bounce(n f64) f64 { 269 | check_valid_range(n) 270 | 271 | if n < 0.5 { 272 | return ease_in_bounce(n * 2.0) * 0.5 273 | } else { 274 | return ease_out_bounce(n * 2.0 - 1.0) * 0.5 + 0.5 275 | } 276 | } 277 | -------------------------------------------------------------------------------- /build/Robot/vrobot/types.v: -------------------------------------------------------------------------------- 1 | module vrobot 2 | 3 | struct Color { 4 | pub: 5 | r int 6 | g int 7 | b int 8 | } 9 | 10 | struct Size { 11 | pub: 12 | width int 13 | height int 14 | } 15 | 16 | struct Point { 17 | pub: 18 | x int 19 | y int 20 | } 21 | -------------------------------------------------------------------------------- /build/Robot/vrobot/utils.v: -------------------------------------------------------------------------------- 1 | module vrobot 2 | 3 | pub fn normalize(val f64, min f64, max f64) f64 { 4 | return (val - min) / (max - min) 5 | } -------------------------------------------------------------------------------- /build/Robot/vrobot/vrobot.v: -------------------------------------------------------------------------------- 1 | module vrobot 2 | 3 | #flag -lgdi32 4 | #include "windows.h" 5 | 6 | [typedef] 7 | type COLORREF = int 8 | 9 | fn C.mouse_event(int, int, int, int, int) 10 | fn C.SetCursorPos(int, int) 11 | fn C.GetCursorPos(point &Point) bool 12 | fn C.GetDC(int) C.HDC 13 | fn C.GetPixel(C.HDC, int, int) COLORREF 14 | fn C.GetDesktopWindow() C.HWDC 15 | fn C.ReleaseDC(C.HWDC, C.HDC) int 16 | fn C.GetSystemMetrics(int) int 17 | 18 | 19 | pub fn get_r_value(color COLORREF) int { 20 | 21 | return color & 0xFF 22 | 23 | } 24 | 25 | pub fn get_g_value(color COLORREF) int { 26 | 27 | return (color & 0xFF00) >> 8 28 | 29 | } 30 | 31 | pub fn get_b_value(color COLORREF) int { 32 | 33 | return color >> 16 34 | 35 | } -------------------------------------------------------------------------------- /build/keyboard/.gitignore: -------------------------------------------------------------------------------- 1 | keyboard.dll -------------------------------------------------------------------------------- /build/keyboard/keyboard.cc: -------------------------------------------------------------------------------- 1 | #include "keyboard.h" -------------------------------------------------------------------------------- /build/keyboard/keyboard.cmd: -------------------------------------------------------------------------------- 1 | g++ -DKEYBOARD_EXPORTS keyboard.cc -shared -o keyboard.dll -------------------------------------------------------------------------------- /build/keyboard/keyboard.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #ifndef KEYBOARD_H 4 | #define KEYBOARD_H 5 | 6 | #ifdef _WIN32 7 | #define KEYBOARD_API __declspec(dllexport) 8 | #else 9 | #define KEYBOARD_API 10 | #endif 11 | 12 | extern "C" 13 | { 14 | // 是否点击某按键 15 | KEYBOARD_API bool isKeyPressed(int key); 16 | // 默认点击了某按键 17 | KEYBOARD_API void simulateKeyPress(int keyCode); 18 | // 按下某按键 19 | KEYBOARD_API void pressKey(int keyCode); 20 | // 弹起某按键 21 | KEYBOARD_API void releaseKey(int keyCode); 22 | } 23 | #endif 24 | 25 | // 是否点击某按键 26 | bool isKeyPressed(int key) 27 | { 28 | return GetAsyncKeyState(key) & 0x8000; 29 | } 30 | 31 | // 默认点击了某按键 32 | void simulateKeyPress(int keyCode) 33 | { 34 | WORD wKeyCode = static_cast(keyCode); 35 | INPUT input; 36 | input.type = INPUT_KEYBOARD; 37 | input.ki.wScan = 0; 38 | input.ki.time = 0; 39 | input.ki.dwExtraInfo = 0; 40 | 41 | input.ki.wVk = wKeyCode; 42 | input.ki.dwFlags = 0; 43 | SendInput(1, &input, sizeof(INPUT)); 44 | 45 | input.ki.dwFlags = KEYEVENTF_KEYUP; 46 | SendInput(1, &input, sizeof(INPUT)); 47 | } 48 | 49 | // 新增:按下某键 50 | void pressKey(int keyCode) 51 | { 52 | WORD wKeyCode = static_cast(keyCode); 53 | INPUT input; 54 | input.type = INPUT_KEYBOARD; 55 | input.ki.wScan = 0; 56 | input.ki.time = 0; 57 | input.ki.dwExtraInfo = 0; 58 | 59 | input.ki.wVk = wKeyCode; 60 | input.ki.dwFlags = 0; 61 | SendInput(1, &input, sizeof(INPUT)); 62 | } 63 | 64 | // 新增:弹起某键 65 | void releaseKey(int keyCode) 66 | { 67 | WORD wKeyCode = static_cast(keyCode); 68 | INPUT input; 69 | input.type = INPUT_KEYBOARD; 70 | input.ki.wScan = 0; 71 | input.ki.time = 0; 72 | input.ki.dwExtraInfo = 0; 73 | 74 | input.ki.wVk = wKeyCode; 75 | input.ki.dwFlags = KEYEVENTF_KEYUP; 76 | SendInput(1, &input, sizeof(INPUT)); 77 | } -------------------------------------------------------------------------------- /build/keyboard/keyboard.php: -------------------------------------------------------------------------------- 1 | ffi = FFI::cdef($header, __DIR__ . DIRECTORY_SEPARATOR . "keyboard.dll"); 14 | } 15 | 16 | public function isKeyPressed(int $key): bool 17 | { 18 | return $this->ffi->isKeyPressed($key); 19 | } 20 | 21 | public function onClickKey(int $key): void 22 | { 23 | $this->ffi->simulateKeyPress($key); 24 | } 25 | 26 | public function pressKey(int $key): self 27 | { 28 | $this->ffi->pressKey($key); 29 | return $this; 30 | } 31 | 32 | public function releaseKey(int $key): self 33 | { 34 | $this->ffi->releaseKey($key); 35 | return $this; 36 | } 37 | } 38 | 39 | $Keyboard = new Keyboard; 40 | 41 | sleep(3); 42 | $Keyboard->pressKey(65); 43 | sleep(3); 44 | $Keyboard->releaseKey(65); 45 | /* while (true) { 46 | if ($Keyboard->isKeyPressed(65)) { 47 | echo "'A'"; 48 | } 49 | usleep(100); 50 | } */ 51 | 52 | /* sleep(5); 53 | 54 | $Keyboard->onClickKey(65); */ 55 | -------------------------------------------------------------------------------- /build/keyboard/keyboard_php.h: -------------------------------------------------------------------------------- 1 | bool isKeyPressed(int key); 2 | void simulateKeyPress(int keyCode); 3 | void pressKey(int keyCode); 4 | void releaseKey(int keyCode); -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "kingbes/phprobot", 3 | "description": "Desktop automation for PHP Windows", 4 | "keywords": [ 5 | "Windows", 6 | "PHP", 7 | "Desktop automation" 8 | ], 9 | "license": "Apache-2.0", 10 | "authors": [ 11 | { 12 | "name": "kingbes", 13 | "email": "1164601554@qq.com" 14 | } 15 | ], 16 | "require": { 17 | "PHP": ">=8.1.0", 18 | "ext-ffi": "*" 19 | }, 20 | "autoload": { 21 | "psr-4": { 22 | "KingBes\\PhpRobot\\": "src/" 23 | } 24 | }, 25 | "minimum-stability": "dev", 26 | "prefer-stable": true, 27 | "config": { 28 | "sort-packages": true 29 | } 30 | } -------------------------------------------------------------------------------- /src/KeyBoard.php: -------------------------------------------------------------------------------- 1 | ffi = \FFI::cdef($header, __DIR__ . DIRECTORY_SEPARATOR . "keyboard.dll"); 18 | } 19 | 20 | /** 21 | * 是否点击键盘某键 function 22 | * 23 | * @param integer $key 24 | * @return boolean 25 | */ 26 | public function isKeyPressed(int $key): bool 27 | { 28 | return $this->ffi->isKeyPressed($key); 29 | } 30 | 31 | /** 32 | * 点击键盘某键 function 33 | * 34 | * @param integer $key 35 | * @return void 36 | */ 37 | public function onClickKey(int $key): void 38 | { 39 | $this->ffi->simulateKeyPress($key); 40 | } 41 | 42 | /** 43 | * 按下键盘某键 function 44 | * 45 | * @param integer $key 46 | * @return self 47 | */ 48 | public function pressKey(int $key): self 49 | { 50 | $this->ffi->pressKey($key); 51 | return $this; 52 | } 53 | 54 | /** 55 | * 弹起键盘某键 function 56 | * 57 | * @param integer $key 58 | * @return self 59 | */ 60 | public function releaseKey(int $key): self 61 | { 62 | $this->ffi->releaseKey($key); 63 | return $this; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/Mouse.php: -------------------------------------------------------------------------------- 1 | ffi->mouse_pos(); 20 | return ["x" => $pos->x, "y" => $pos->y]; 21 | } 22 | 23 | /** 24 | * 鼠标单击 function 25 | * 26 | * @param string $button left right middle 27 | * @return self 28 | */ 29 | public function mouse_click(string $button): self 30 | { 31 | $this->ffi->mouse_click($button); 32 | return $this; 33 | } 34 | 35 | /** 36 | * 鼠标双击 function 37 | * 38 | * @param string $button left right middle 39 | * @return self 40 | */ 41 | public function mouse_double_click(string $button): self 42 | { 43 | $this->ffi->mouse_double_click($button); 44 | return $this; 45 | } 46 | 47 | /** 48 | * 将鼠标(左)拖动到指定位置。 function 49 | * 50 | * @param integer $x 51 | * @param integer $y 52 | * @return self 53 | */ 54 | public function mouse_left_drag(int $x, int $y): self 55 | { 56 | $this->ffi->mouse_left_drag($x, $y); 57 | return $this; 58 | } 59 | 60 | /** 61 | * 相对于当前位置拖动鼠标(左)。 function 62 | * 63 | * @param integer $offset_x 64 | * @param integer $offset_y 65 | * @return self 66 | */ 67 | public function mouse_drag_rel(int $offset_x, int $offset_y): self 68 | { 69 | $this->ffi->mouse_drag_rel($offset_x, $offset_y); 70 | return $this; 71 | } 72 | 73 | /** 74 | * 移动鼠标到 x y function 75 | * 76 | * @param integer $x 77 | * @param integer $y 78 | * @return self 79 | */ 80 | public function mouse_move_mouse(int $x, int $y): self 81 | { 82 | $this->ffi->mouse_move_mouse($x, $y); 83 | return $this; 84 | } 85 | 86 | /** 87 | * 相对于当前位置移动鼠标。 function 88 | * 89 | * @param integer $offset_x 90 | * @param integer $offset_y 91 | * @return self 92 | */ 93 | public function move_mouse_rel(int $offset_x, int $offset_y): self 94 | { 95 | $this->ffi->move_mouse_rel($offset_x, $offset_y); 96 | return $this; 97 | } 98 | 99 | /** 100 | * 鼠标平滑移动指定位置 function 101 | * 102 | * @param integer $x 103 | * @param integer $y 104 | * @param integer $duration_ms 毫秒 105 | * @param string $tween 预览补间 106 | * @return self 107 | */ 108 | public function move_mouse_smooth(int $x, int $y, int $duration_ms, string $tween): self 109 | { 110 | $start_pos = $this->ffi->mouse_pos(); 111 | $dist_x = $x - $start_pos->x; 112 | $dist_y = $y - $start_pos->y; 113 | $steps = intval(max(50, ($duration_ms) / 10)); 114 | $dt = intval(($duration_ms) / $steps); 115 | $i = 0; 116 | $factor = 0.0; 117 | while ($i < $steps) { 118 | $factor = $this->ffi->factor($tween, $i, $steps); 119 | $current_x = intval($start_pos->x + $factor * $dist_x); 120 | $current_y = intval($start_pos->y + $factor * $dist_y); 121 | $this->ffi->move_mouse($current_x, $current_y); 122 | usleep($dt); 123 | $i++; 124 | } 125 | 126 | return $this; 127 | } 128 | 129 | /** 130 | * 当前平滑移动鼠标 function 131 | * 132 | * @param integer $offset_x 133 | * @param integer $offset_y 134 | * @param integer $duration_ms 毫秒 135 | * @param string $tween 预览补间 136 | * @return self 137 | */ 138 | public function move_mouse_smooth_rel(int $offset_x, int $offset_y, int $duration_ms, string $tween): self 139 | { 140 | $start_pos = $this->ffi->mouse_pos(); 141 | $dest_x = $offset_x + $start_pos->x; 142 | $dest_y = $offset_y + $start_pos->y; 143 | $this->move_mouse_smooth($dest_x, $dest_y, $duration_ms, $tween); 144 | return $this; 145 | } 146 | 147 | /** 148 | * 鼠标按下 function 149 | * 150 | * @param string $button left right middle 151 | * @return self 152 | */ 153 | public function mouse_down(string $button): self 154 | { 155 | $this->ffi->mouse_down($button); 156 | return $this; 157 | } 158 | 159 | /** 160 | * 鼠标弹起 function 161 | * 162 | * @param string $button left right middle 163 | * @return self 164 | */ 165 | public function mouse_up(string $button): self 166 | { 167 | $this->ffi->mouse_up($button); 168 | return $this; 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /src/Robot.php: -------------------------------------------------------------------------------- 1 | ffi = FFI::cdef($header, __DIR__ . DIRECTORY_SEPARATOR . "robot.dll"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Screen.php: -------------------------------------------------------------------------------- 1 | ffi->pixel_color($x, $y); 22 | return [ 23 | "r" => $rgb->r, 24 | "g" => $rgb->g, 25 | "b" => $rgb->b 26 | ]; 27 | } 28 | 29 | /** 30 | * 获取屏幕大小 function 31 | * 32 | * @return array 33 | */ 34 | public function screen_size(): array 35 | { 36 | $size = $this->ffi->screen_size(); 37 | return [ 38 | "width" => $size->width, 39 | "height" => $size->height 40 | ]; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/keyboard.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KingBes/php-windows-robot/4c3ebe919c41a9c5f20a4ae67e4328627b11017c/src/keyboard.dll -------------------------------------------------------------------------------- /src/keyboard_php.h: -------------------------------------------------------------------------------- 1 | bool isKeyPressed(int key); 2 | void simulateKeyPress(int keyCode); 3 | void pressKey(int keyCode); 4 | void releaseKey(int keyCode); -------------------------------------------------------------------------------- /src/robot.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KingBes/php-windows-robot/4c3ebe919c41a9c5f20a4ae67e4328627b11017c/src/robot.dll -------------------------------------------------------------------------------- /src/robot_php.h: -------------------------------------------------------------------------------- 1 | // 鼠标 2 | typedef struct 3 | { 4 | int x; 5 | int y; 6 | } Pos; 7 | Pos mouse_pos(); 8 | void mouse_click(const char *button); 9 | void mouse_double_click(const char *button); 10 | void mouse_left_drag(int x, int y); 11 | void mouse_drag_rel(int offset_x, int offset_y); 12 | void mouse_move_mouse(int x, int y); 13 | void move_mouse_rel(int offset_x, int offset_y); 14 | void mouse_down(const char *button); 15 | void mouse_up(const char *button); 16 | 17 | void move_mouse(int x, int y); 18 | double factor(const char *tween, int i, int steps); 19 | 20 | // 屏幕 21 | typedef struct { 22 | int r; 23 | int g; 24 | int b; 25 | }Color; 26 | Color pixel_color(int x,int y); 27 | typedef struct { 28 | int width; 29 | int height; 30 | }Size; 31 | Size screen_size(); --------------------------------------------------------------------------------