1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
#[derive(Debug, Clone, Copy)]
pub struct Point {
pub x: i32,
pub y: i32,
}
impl From<(i32, i32)> for Point {
fn from(pos: (i32, i32)) -> Self {
Point { x: pos.0, y: pos.1 }
}
}
/// ## control mouse
/// ```rust
///fn main()->doe::DynError{
/// use std::time::Duration;
/// use doe::mouse::press;
/// move_to(300, 700);
/// press("left");
/// move_to_with_duration(0, 0,Duration::from_secs(5));
/// move_and_press_right(800,900);
/// press("right");
/// Ok(())
///}
///```
#[allow(warnings)]
#[cfg(feature = "mouse")]
pub mod mouse {
pub mod common {
use super::super::Point;
use mouse_rs::{types::keys::Keys, Mouse};
use std::thread::sleep;
use std::time::Duration;
///```
///fn main() {
/// use doe::mouse;
/// mouse::scrool(-2);//scrool down
/// mouse::scrool(2);//scrool up
///}
/// ```
pub fn scrool(delta: i32) {
let mouse = Mouse::new();
mouse.wheel(delta);
}
///move_to_select_all_and_copy_all_then_get_clipboard
/// x is from left to right
///
/// y is from top to bottom
pub fn move_to_select_all_and_copy_all_then_get_clipboard(
x: i32,
y: i32,
) -> Result<String, Box<dyn std::error::Error>> {
use crate::*;
mouse::move_to_press_left(x, y);
let ctrl = if cfg!(target_os = "windows") {
keyboard::KeyCode::CONTROL
} else {
keyboard::KeyCode::CONTROL
};
let a = if cfg!(target_os = "windows") {
keyboard::KeyCode::A
} else {
keyboard::KeyCode::A
};
let c = if cfg!(target_os = "windows") {
keyboard::KeyCode::C
} else {
keyboard::KeyCode::C
};
//ctrl+A select all
keyboard::key_press(ctrl);
keyboard::key_press(a).sleep(1.0);
keyboard::key_release(ctrl);
keyboard::key_release(a);
//ctrl+C copy all
keyboard::key_press(ctrl);
keyboard::key_press(c).sleep(1.0);
keyboard::key_release(ctrl);
keyboard::key_release(c);
clipboard::get_clipboard()
}
/// x is from left to right
///
/// y is from top to bottom
pub fn move_to(x: i32, y: i32) {
let mouse = Mouse::new();
mouse.move_to(x, y).expect("Unable to move mouse");
}
/// x is from left to right
///
/// y is from top to bottom
///
/// duration is sleep duration
pub fn move_to_with_duration(x: i32, y: i32, duration: Duration) {
let mouse = Mouse::new();
sleep(duration);
mouse.move_to(x, y).expect("Unable to move mouse");
}
/// x is from left to right
///
/// y is from top to bottom
pub fn move_to_press_left(x: i32, y: i32) {
let mouse = Mouse::new();
move_to(x, y);
mouse.press(&Keys::LEFT).expect("Unable to press button");
mouse
.release(&Keys::LEFT)
.expect("Unable to release button");
}
/// x is from left to right
///
/// y is from top to bottom
pub fn move_to_press_right(x: i32, y: i32) {
let mouse = Mouse::new();
move_to(x, y);
mouse.press(&Keys::RIGHT).expect("Unable to press button");
mouse
.release(&Keys::RIGHT)
.expect("Unable to release button");
}
/// key just can be 'left' or 'right' or 'middle'
pub fn press(key: impl AsRef<str>) {
let mouse = Mouse::new();
match key.as_ref() {
"left" => {
mouse.press(&Keys::LEFT).expect("Unable to press button");
}
"right" => {
mouse.press(&Keys::RIGHT).expect("Unable to press button");
}
"middle" => {
mouse.press(&Keys::MIDDLE).expect("Unable to press button");
}
_ => {}
}
}
/// key just can be 'left' or 'right' or 'middle'
pub fn release(key: impl AsRef<str>) {
let mouse = Mouse::new();
match key.as_ref() {
"left" => {
mouse
.release(&Keys::LEFT)
.expect("Unable to release button");
}
"right" => {
mouse
.release(&Keys::RIGHT)
.expect("Unable to release button");
}
"middle" => {
mouse
.release(&Keys::MIDDLE)
.expect("Unable to press button");
}
_ => {}
}
}
///
/// ```
/// use doe::mouse;
/// let script = r#"
///move_to::217::135
///sleep::1
///move_to_press_left::189::137
///sleep::1
///move_to_press_left::198::185
///sleep::1
///move_drag::655::343::678::330
///sleep::1
/// # script comment
///move_to_paste::479::715::page demo is ok
/// "#
/// .trim();
/// mouse::mouse_automater(script);
/// ```
pub fn mouse_automater(script: impl ToString) -> () {
use crate::mouse::*;
use std::thread::sleep;
use std::time::Duration;
for script in script.to_string().split("\n") {
let script = script.trim().to_string();
if script.starts_with("#") || script.starts_with("//") {
continue;
}
let tokens: Vec<&str> = script.split("::").map(|s| s.trim()).collect();
match tokens.as_slice() {
["move_to", x, y] => {
let x = x.parse::<i32>().unwrap();
let y = y.parse::<i32>().unwrap();
move_to(x, y)
}
["sleep", t] => {
let t = t.parse::<f64>().unwrap();
sleep(Duration::from_secs_f64(t))
}
["move_to_press_left", x, y] => {
let x = x.parse::<i32>().unwrap();
let y = y.parse::<i32>().unwrap();
move_to_press_left(x, y)
}
["move_to_press_right", x, y] => {
let x = x.parse::<i32>().unwrap();
let y = y.parse::<i32>().unwrap();
move_to_press_right(x, y)
}
["move_to_paste", x, y, content] => {
let x = x.parse::<i32>().unwrap();
let y = y.parse::<i32>().unwrap();
move_to_paste(x, y, content)
}
_ => {
if let Some(command) = tokens.iter().nth(0) {
if command == &"move_drag" {
let x1 = tokens.iter().nth(1).unwrap().parse::<i32>().unwrap();
let x2 = tokens.iter().nth(2).unwrap().parse::<i32>().unwrap();
let y1 = tokens.iter().nth(3).unwrap().parse::<i32>().unwrap();
let y2 = tokens.iter().nth(4).unwrap().parse::<i32>().unwrap();
mouse_drag((x1, y1).into(), (x2, y2).into())
}
} else {
println!("Unsupported input!");
}
}
}
}
}
}
#[cfg(target_os = "linux")]
pub mod linux {
use super::super::Point;
use mouse_rs::{types::keys::Keys, Mouse};
use std::thread::sleep;
use std::time::Duration;
/// listen for mouse movement and print position
///
/// ```rust
/// use doe::mouse::listen_mouse_pisition;
/// listen_mouse_pisition();
/// ```
///
pub fn listen_mouse_position() {
let mut prev_position = Mouse::new().get_position().unwrap();
loop {
let mouse = Mouse::new();
let position = mouse.get_position().unwrap();
if position.to_string() != prev_position.to_string() {
println!("move_to({},{});", position.x, position.y);
prev_position = position;
}
sleep(Duration::from_secs_f64(0.1));
}
}
pub fn listen_mouse_position_by_xdotool() {
use std::{thread, time::Duration};
loop {
use xdotool::mouse::get_mouse_location;
let point = String::from_utf8_lossy(&get_mouse_location().stdout)
.to_string()
.trim()
.to_string();
println!("{}", point);
thread::sleep(Duration::from_millis(100));
}
}
pub fn mouse_drag(from: Point, to: Point) {}
///move_to_paste
///```rust
///use doe::mouse::*;
///move_to_paste(293, 404, "hello world");
/// ```
pub fn move_to_paste(x: i32, y: i32, content: impl ToString) {
use crate::clipboard::set_clipboard;
use crate::keyboard::key_press;
use crate::keyboard::key_release;
use crate::keyboard::KeyCode;
use crate::mouse::move_to_press_left;
set_clipboard(content.to_string()).expect("set clipboad error");
move_to_press_left(x, y);
key_press("ctrl+v");
key_release("ctrl+v");
}
}
#[cfg(target_os = "macos")]
pub mod macos {
use crate::mouse::move_to_press_left;
use super::super::Point;
use mouse_rs::{types::keys::Keys, Mouse};
use std::thread::sleep;
use std::time::Duration;
pub fn listen_mouse_position() {
let mut prev_position = get_mouse_position_by_core_graphics();
loop {
let position = get_mouse_position_by_core_graphics();
if position != prev_position {
println!("move_to({},{});", position.x, position.y);
prev_position = position;
}
sleep(Duration::from_secs_f64(0.1));
}
}
pub fn get_mouse_position_by_core_graphics() -> (f64, f64) {
use core_graphics::event::CGEvent;
use core_graphics::event::CGKeyCode;
use core_graphics::event_source::CGEventSource;
use core_graphics::{event::KeyCode, event_source::CGEventSourceStateID};
let state_id: CGEventSourceStateID = CGEventSourceStateID::HIDSystemState;
let source: CGEventSource = CGEventSource::new(state_id).unwrap();
let keycode: CGKeyCode = KeyCode::SPACE;
let event = CGEvent::new_keyboard_event(source, keycode, true).unwrap();
let xy = event.location();
(xy.x, xy.y)
}
///move_to_paste
///```rust
///use doe::mouse::*;
///move_to_paste(293, 404, "hello world");
/// ```
pub fn move_to_paste(x: i32, y: i32, content: impl ToString) {
use crate::clipboard::set_clipboard;
use crate::keyboard::key_press;
use crate::keyboard::key_release;
use crate::keyboard::KeyCode;
set_clipboard(content.to_string()).expect("set clipboad error");
move_to_press_left(x, y);
key_press(KeyCode::CONTROL);
key_press(KeyCode::V);
key_release(KeyCode::CONTROL);
key_release(KeyCode::V);
}
/// mouse_drag
/// ```rust
///fn main(){
/// use doe::mouse::mouse_drag;
/// mouse_drag((543,446).into(), (590,460).into(),"left");
///}
/// ```
pub fn mouse_left_drag(from: Point, to: Point) {
use core_graphics::event::{CGEvent, CGEventTapLocation, CGEventType, CGMouseButton};
use core_graphics::event_source::{CGEventSource, CGEventSourceStateID};
use core_graphics::geometry::CGPoint;
let state_id: CGEventSourceStateID = CGEventSourceStateID::HIDSystemState;
let event_source: CGEventSource = CGEventSource::new(state_id).unwrap();
// 创建鼠标按下事件
let mouse_down_event = CGEvent::new_mouse_event(
event_source.clone(),
CGEventType::LeftMouseDown,
CGPoint::new(from.x as f64, from.y as f64),
CGMouseButton::Left,
)
.unwrap();
mouse_down_event.post(CGEventTapLocation::HID);
// 创建鼠标移动事件
let mouse_move_event = CGEvent::new_mouse_event(
event_source.clone(),
CGEventType::MouseMoved,
CGPoint::new(to.x as f64, to.y as f64),
CGMouseButton::Left,
)
.unwrap();
mouse_move_event.post(CGEventTapLocation::HID);
// 创建鼠标释放事件
let mouse_up_event = CGEvent::new_mouse_event(
event_source.clone(),
CGEventType::LeftMouseUp,
CGPoint::new(to.x as f64, to.y as f64),
CGMouseButton::Left,
)
.unwrap();
mouse_up_event.post(CGEventTapLocation::HID);
}
#[cfg(target_os = "macos")]
pub fn mouse_drag(from: Point, to: Point) {
use core_graphics::event::{CGEvent, CGEventTapLocation, CGEventType, CGMouseButton};
use core_graphics::event_source::{CGEventSource, CGEventSourceStateID};
use core_graphics::geometry::CGPoint;
let state_id: CGEventSourceStateID = CGEventSourceStateID::HIDSystemState;
let event_source: CGEventSource = CGEventSource::new(state_id).unwrap();
// 创建鼠标按下事件
let mouse_down_event = CGEvent::new_mouse_event(
event_source.clone(),
CGEventType::LeftMouseDown,
CGPoint::new(from.x as f64, from.y as f64),
CGMouseButton::Left,
)
.unwrap();
mouse_down_event.post(CGEventTapLocation::HID);
// 创建鼠标移动事件
let mouse_move_event = CGEvent::new_mouse_event(
event_source.clone(),
CGEventType::MouseMoved,
CGPoint::new(to.x as f64, to.y as f64),
CGMouseButton::Left,
)
.unwrap();
mouse_move_event.post(CGEventTapLocation::HID);
// 创建鼠标释放事件
let mouse_up_event = CGEvent::new_mouse_event(
event_source,
CGEventType::LeftMouseUp,
CGPoint::new(to.x as f64, to.y as f64),
CGMouseButton::Left,
)
.unwrap();
mouse_up_event.post(CGEventTapLocation::HID);
}
}
#[cfg(target_os = "windows")]
pub mod windows {
use crate::mouse::move_to_press_left;
use crate::Sleep;
use super::super::Point;
use mouse_rs::{types::keys::Keys, Mouse};
use std::thread::sleep;
use std::time::Duration;
pub fn listen_mouse_position() {
use winapi::shared::windef::POINT;
use winapi::um::winuser::GetCursorPos;
let mut prev_position: POINT = POINT { x: 0, y: 0 };
unsafe {
if GetCursorPos(&mut prev_position) == 0 {
return;
}
loop {
let mut position: POINT = POINT { x: 0, y: 0 };
if GetCursorPos(&mut position) == 0 {
break;
}
if (position.x != prev_position.x) && (position.y != prev_position.y) {
println!("move_to({},{});", position.x, position.y);
prev_position = position;
}
}
sleep(Duration::from_secs_f64(0.1));
}
}
///move_to_paste
///```rust
///use doe::mouse::*;
///move_to_paste(293, 404, "hello world");
/// ```
pub fn move_to_paste(x: i32, y: i32, content: impl ToString) {
use crate::clipboard::set_clipboard;
use crate::keyboard::key_press;
use crate::keyboard::key_release;
use crate::keyboard::KeyCode;
set_clipboard(content.to_string()).expect("set clipboad error");
move_to_press_left(x, y);
key_press(KeyCode::CONTROL);
key_press(KeyCode::V);
key_release(KeyCode::CONTROL);
key_release(KeyCode::V);
}
/// mouse_drag
/// ```rust
///fn main(){
/// use doe::mouse::mouse_drag;
/// mouse_drag((543,446).into(), (590,460).into());
///}
/// ```
pub fn mouse_drag(from: Point, to: Point) {
use std::thread::sleep;
use std::time::Duration;
unsafe {
use winapi::ctypes::c_int;
use winapi::um::winuser::{
GetSystemMetrics, SendInput, INPUT, INPUT_MOUSE, MOUSEEVENTF_ABSOLUTE,
MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_LEFTUP, MOUSEEVENTF_MOVE, MOUSEINPUT,
SM_CXSCREEN, SM_CYSCREEN,
};
// Screen size
let screen_x: c_int = GetSystemMetrics(SM_CXSCREEN);
let screen_y: c_int = GetSystemMetrics(SM_CYSCREEN);
let mut input = INPUT::default();
input.type_ = INPUT_MOUSE;
let mi: MOUSEINPUT = MOUSEINPUT {
dx: (from.x * 65535) / screen_x, // Normalized absolute coordinate for x (horizontal)
dy: (from.y * 65535) / screen_y, // Normalized absolute coordinate for y (vertical)
mouseData: 0,
dwFlags: MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN,
time: 0,
dwExtraInfo: 0,
};
input.u.mi_mut().clone_from(&mi);
SendInput(1, &mut input, std::mem::size_of::<INPUT>() as i32); // Mouse down at 'from' point
sleep(Duration::from_secs_f64(0.1));
// We need to update the dx, dy for 'to' point to complete the drag
input.u.mi_mut().dx = (to.x * 65535) / screen_x; // Normalized absolute coordinate for x (horizontal)
input.u.mi_mut().dy = (to.y * 65535) / screen_y; // Normalized absolute coordinate for y (vertical)
input.u.mi_mut().dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
SendInput(1, &mut input, std::mem::size_of::<INPUT>() as i32); // Mouse move to 'to' point
// Update the dwFlags for a mouse up event
input.u.mi_mut().dwFlags =
MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTUP;
SendInput(1, &mut input, std::mem::size_of::<INPUT>() as i32);
// Mouse up at 'to' point
}
}
pub fn listen_mouse_click() {
use winapi::shared::minwindef::{LPARAM, WPARAM};
use winapi::um::{libloaderapi, winuser};
unsafe extern "system" fn mouse_hook_callback(
code: i32,
wparam: WPARAM,
lparam: LPARAM,
) -> LPARAM {
if code >= 0 {
if wparam == winuser::WM_LBUTTONDOWN as usize {
println!("press(\"left\");");
} else if wparam == winuser::WM_LBUTTONUP as usize {
println!("release(\"left\");");
} else if wparam == winuser::WM_RBUTTONDOWN as usize {
println!("press(\"right\");");
} else if wparam == winuser::WM_RBUTTONUP as usize {
println!("release(\"right\");");
} else if wparam == winuser::WM_MBUTTONDOWN as usize {
println!("press(\"middle\");");
} else if wparam == winuser::WM_MBUTTONUP as usize {
println!("release(\"middle\");");
}
}
winuser::CallNextHookEx(0 as winapi::shared::windef::HHOOK, code, wparam, lparam)
}
let h_instance = unsafe { libloaderapi::GetModuleHandleW(std::ptr::null_mut()) };
let hook = unsafe {
winuser::SetWindowsHookExW(
winuser::WH_MOUSE_LL,
Some(mouse_hook_callback),
h_instance,
0,
)
};
if hook.is_null() {
panic!("Failed to install hook");
}
let mut msg = winapi::um::winuser::MSG::default();
while unsafe { winuser::GetMessageW(&mut msg, std::ptr::null_mut(), 0, 0) } > 0 {
unsafe {
winuser::TranslateMessage(&msg);
winuser::DispatchMessageW(&msg);
}
}
unsafe { winuser::UnhookWindowsHookEx(hook) };
}
pub fn listen_mouse_scroll() {
use winapi::shared::minwindef::{LPARAM, WPARAM};
use winapi::um::{libloaderapi, winuser};
unsafe extern "system" fn scroll_hook_callback(
code: i32,
wparam: WPARAM,
lparam: LPARAM,
) -> LPARAM {
if code >= 0 {
let msllhook = *(lparam as *const winuser::MSLLHOOKSTRUCT);
if msllhook.flags & winuser::LLMHF_INJECTED == 0 {
// Check if it's a scroll event
if wparam == winuser::WM_MOUSEWHEEL as usize
|| wparam == winuser::WM_MOUSEHWHEEL as usize
{
let flag = msllhook;
println!("move_to({},{});", flag.pt.x, flag.pt.y);
if flag.mouseData == 4287102976 {
println!("scoll_down();");
} else if flag.mouseData == 7864320 {
println!("scoll_up();");
}
}
}
}
winuser::CallNextHookEx(0 as winapi::shared::windef::HHOOK, code, wparam, lparam)
}
let h_instance = unsafe { libloaderapi::GetModuleHandleW(std::ptr::null_mut()) };
let hook = unsafe {
winuser::SetWindowsHookExW(
winuser::WH_MOUSE_LL,
Some(scroll_hook_callback),
h_instance,
0,
)
};
if hook.is_null() {
panic!("Failed to install hook");
}
let mut msg = winapi::um::winuser::MSG::default();
while unsafe { winuser::GetMessageW(&mut msg, std::ptr::null_mut(), 0, 0) } > 0 {
unsafe {
winuser::TranslateMessage(&msg);
winuser::DispatchMessageW(&msg);
}
}
unsafe { winuser::UnhookWindowsHookEx(hook) };
}
/// listen_all keyboard mouse events
/// ```rust
/// use doe::mouse::listen_all;
/// listen_all();
/// ```
///
/// `cargo r > script`
///
pub fn listen_all() {
use crate::keyboard::listen_keybord;
use crate::mouse::listen_mouse_click;
use crate::mouse::listen_mouse_position;
use crate::mouse::listen_mouse_scroll;
let t1 = std::thread::spawn(|| {
listen_keybord();
});
let t2 = std::thread::spawn(|| {
listen_mouse_position();
});
let t3 = std::thread::spawn(|| {
listen_mouse_scroll();
});
let t4 = std::thread::spawn(|| {
listen_mouse_click();
});
t1.join().unwrap();
t2.join().unwrap();
t3.join().unwrap();
t4.join().unwrap();
}
/// automater
///
/// generate script by
/// ```rust
/// fn gen(){
/// use doe::mouse::listen_all;
/// listen_all();
/// }
/// ```
/// script
///```sh
/// move_to(1562,6);
/// press("left");
/// move_to(1453,20);
/// press("left");
/// move_to(342,515);
/// scoll_down();
/// move_to(496,560);
/// press("left");
/// move_to(304,454);
/// key_press(8);
/// key_release(8);
/// press("left");
/// key_press(162);
/// key_press(67);
/// ```
///
/// run script
/// ```rust
/// use doe::mouse::automater;
/// let script = include_str!("../script");
/// automater(script);
/// ```
///
pub fn automater(script: impl ToString) -> () {
use crate::keyboard::*;
use crate::mouse::*;
use std::thread::sleep;
use std::time::Duration;
for script in script.to_string().split("\n") {
let script = script.trim().to_string();
if script.starts_with("#") || script.starts_with("//") {
continue;
}
if script.starts_with("move_to") {
let x = script
.clone()
.replace("move_to(", "")
.replace(");", "")
.to_string()
.split(",")
.nth(0)
.unwrap()
.parse::<i32>()
.unwrap();
let y = script
.clone()
.replace("move_to(", "")
.replace(");", "")
.to_string()
.split(",")
.nth(1)
.unwrap()
.parse::<i32>()
.unwrap();
move_to(x, y);
} else if script.starts_with("scoll_down") {
scrool(-2);
} else if script.starts_with("scoll_up") {
scrool(2);
} else if script.starts_with("key_press") {
let keycode = script
.clone()
.replace("key_press(", "")
.replace(");", "")
.to_string()
.split(",")
.nth(0)
.unwrap()
.parse::<u16>()
.unwrap();
key_press(keycode).sleep(0.2);
} else if script.starts_with("key_release") {
let keycode = script
.clone()
.replace("key_release(", "")
.replace(");", "")
.to_string()
.split(",")
.nth(0)
.unwrap()
.parse::<u16>()
.unwrap();
key_release(keycode).sleep(0.5);
} else if script.starts_with("press") {
let key = script
.clone()
.replace("press(\"", "")
.replace("\");", "")
.to_string()
.split(",")
.nth(0)
.unwrap()
.to_string();
press(key.clone()).sleep(0.2);
} else if script.starts_with("press") {
let key = script
.clone()
.replace("press(\"", "")
.replace("\");", "")
.to_string()
.split(",")
.nth(0)
.unwrap()
.to_string();
release(key).sleep(0.2);
}
}
}
}
pub use common::*;
#[cfg(target_os = "linux")]
pub use linux::*;
#[cfg(target_os = "macos")]
pub use macos::*;
#[cfg(target_os = "windows")]
pub use windows::*;
}
#[cfg(feature = "mouse")]
pub use mouse::*;