control_craft/
lib.rs

1use std::thread;
2use std::time::Duration;
3use windows::{
4    Win32::UI::Input::KeyboardAndMouse::*, Win32::UI::WindowsAndMessaging::SetCursorPos,
5};
6
7#[derive(Debug)]
8pub enum MouseButton {
9    Left,
10    Right,
11}
12
13/// Moves the mouse cursor to the specified screen coordinates (x, y).
14///
15/// # Parameters
16/// - `x`: The x-coordinate on the screen.
17/// - `y`: The y-coordinate on the screen.
18///
19/// # Usage
20/// Useful for UI automation, moving the cursor to a specific location on the screen.
21pub fn set_cursor_pos(x: i32, y: i32) {
22    unsafe {
23        let _ = SetCursorPos(x, y);
24    }
25}
26
27/// Simulates a drag-and-drop operation from the current cursor position
28/// to the specified screen coordinates (x, y).
29///
30/// # Parameters
31/// - `x`: The x-coordinate of the drop location.
32/// - `y`: The y-coordinate of the drop location.
33///
34/// # Behavior
35/// - Presses and holds the left mouse button.
36/// - Moves the cursor to the specified coordinates.
37/// - Releases the left mouse button after a delay.
38pub fn drag_and_drop(x: i32, y: i32) {
39    unsafe {
40        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
41        thread::sleep(Duration::from_millis(100));
42
43        let _ = SetCursorPos(x, y);
44        thread::sleep(Duration::from_millis(500));
45
46        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
47    }
48}
49
50/// Simulates a mouse button click (left or right).
51///
52/// # Parameters
53/// - `button`: Specifies which mouse button to click (`MouseButton::Left` or `MouseButton::Right`).
54///
55/// # Behavior
56/// - Presses and releases the specified mouse button with a small delay.
57pub fn click_mouse_button(button: MouseButton) {
58    unsafe {
59        match button {
60            MouseButton::Left => {
61                mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
62                thread::sleep(Duration::from_millis(100));
63                mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
64            }
65            MouseButton::Right => {
66                mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
67                thread::sleep(Duration::from_millis(100));
68                mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
69            }
70        }
71    }
72}
73
74/// Simulates a left mouse button click.
75///
76/// # Behavior
77/// - Presses and releases the left mouse button with a small delay.
78/// - Equivalent to calling `click_mouse_button(MouseButton::Left)`.
79pub fn click_mouse_button_left() {
80    unsafe {
81        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
82        thread::sleep(Duration::from_millis(100));
83        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
84    }
85}
86
87/// Simulates a right mouse button click.
88///
89/// # Behavior
90/// - Presses and releases the right mouse button with a small delay.
91/// - Equivalent to calling `click_mouse_button(MouseButton::Right)`.
92pub fn click_mouse_button_right() {
93    unsafe {
94        mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
95        thread::sleep(Duration::from_millis(100));
96        mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
97    }
98}
99
100/// Simulates pressing and releasing a keyboard key.
101///
102/// # Parameters
103/// - `key`: The virtual-key code of the key to press (e.g., `0x41` for 'A').
104///
105/// # Behavior
106/// - Presses the specified key.
107/// - Releases the specified key immediately after pressing.
108pub fn press_key(key: u8) {
109    unsafe {
110        keybd_event(key, 0, KEYBD_EVENT_FLAGS(0), 0);
111        keybd_event(key, 0, KEYBD_EVENT_FLAGS(2), 0);
112    }
113}