move_mouse/
move_mouse.rs

1use std::{thread::sleep, time::Duration};
2
3use inputbot::{KeybdKey::*, MouseCursor};
4
5/// This example demonstrates moving the mouse around on the screen both relative to its position,
6/// and absolute. To use these functions effectively, you would ideally combine it with a library
7/// which exposes your system display resolution, as well as separates different monitors.
8
9fn main() {
10    // Bind our 1 key to a function that moves the mouse absolute to your monitors. Note: if you
11    // have multiple monitors, 0, 0 might be not where you're expecting. If we wanted to get the
12    // absolute position of your primary (or a specific) monitor, we would need to bring in extra
13    // libraries.
14    Numrow1Key.bind(|| {
15        for x in 0..=600 {
16            MouseCursor::move_abs(x as i32, 300);
17            sleep(Duration::from_millis(1));
18        }
19    });
20
21    // Bind our 2 key to a function that moves the mouse relative to its current position.
22    // This will be 100 pixels over and 100 pixels down.
23    Numrow2Key.bind(|| {
24        MouseCursor::move_rel(100, 100);
25        sleep(Duration::from_millis(1));
26    });
27
28    // Call this to start listening for bound inputs.
29    inputbot::handle_input_events();
30}