mouse_rs/
mouse.rs

1use crate::types::keys::Keys;
2use crate::{sys, types::Point};
3
4/// Struct for the mouse
5///
6/// This struct represents a mouse and doesn't hold any values
7pub struct Mouse(sys::Mouse);
8
9#[allow(unreachable_code, unused_variables)]
10impl Mouse {
11    /// This method creates a new mouse instance, must always be run before anything else
12    pub fn new() -> Mouse {
13        Mouse(sys::Mouse::new())
14    }
15
16    /// This method moves the mouse around
17    ///
18    /// # Examples
19    ///
20    /// ```no_run
21    /// use mouse_rs::{types::keys::*, Mouse};
22    ///
23    /// fn move_mouse() {
24    ///     let mouse = Mouse::new();
25    ///     mouse.move_to(500, 500).expect("Unable to move mouse");
26    /// }
27    ///
28    /// ```
29    pub fn move_to(&self, x: i32, y: i32) -> Result<(), Box<dyn std::error::Error>> {
30        self.0.move_to(x, y)
31    }
32
33    /// This method presses the given button in
34    ///
35    /// *NOTE: This doesn't release the button so it will keep pressing*
36    ///
37    /// # Examples
38    ///
39    /// ```no_run
40    /// use mouse_rs::{types::keys::Keys, Mouse};
41    ///
42    /// fn press_button() {
43    ///     let mouse = Mouse::new();
44    ///     mouse.press(&Keys::LEFT).expect("Unable to press button"); // This will keep pressing
45    /// }
46    ///
47    /// fn press_and_release_button() {
48    ///     let mouse = Mouse::new();
49    ///     mouse.press(&Keys::RIGHT).expect("Unable to press button");
50    ///     mouse.release(&Keys::RIGHT).expect("Unable to release button"); // This will press the right mouse quickly
51    /// }
52    /// ```
53    pub fn press<'a>(&self, button: &'a Keys) -> Result<(), Box<dyn std::error::Error + 'a>> {
54        self.0.press(button)
55    }
56
57    /// This will release the button as noted above
58    pub fn release<'a>(&self, button: &'a Keys) -> Result<(), Box<dyn std::error::Error + 'a>> {
59        self.0.release(button)
60    }
61
62    /// This gets the current mouse position
63    ///
64    /// # Example
65    ///
66    /// ```no_run
67    /// use mouse_rs::Mouse;
68    ///
69    /// fn get_post() {
70    ///     let mouse = Mouse::new();
71    ///     let pos = mouse.get_position().unwrap();
72    ///     println!("X = {}, Y = {}", pos.x, pos.y)
73    /// }
74    /// ```
75    pub fn get_position(&self) -> Result<Point, Box<dyn std::error::Error>> {
76        self.0.get_position()
77    }
78
79    /// This will scroll the mouse,
80    ///
81    /// For scrolling down use negative values, for scrolling up use positive values
82    ///
83    /// # Examples
84    ///
85    /// ```no_run
86    /// use mouse_rs::{types::keys::*, Mouse};
87    ///
88    /// fn scroll_up() {
89    ///     let mouse = Mouse::new();
90    ///     mouse.wheel(1);
91    /// }
92    ///
93    /// fn scroll_down() {
94    ///     let mouse = Mouse::new();
95    ///     mouse.wheel(-1);
96    /// }
97    /// ```
98    pub fn wheel(&self, delta: i32) -> Result<(), Box<dyn std::error::Error>> {
99        self.0.wheel(delta)
100    }
101
102    /// This is the exact same as wheel
103    pub fn scroll(&self, delta: i32) -> Result<(), Box<dyn std::error::Error>> {
104        self.0.wheel(delta)
105    }
106
107    // Does the exact same thing as press and release combined, but into one function
108    pub fn click<'a>(&self, button: &'a Keys) -> Result<(), Box<dyn std::error::Error + 'a>> {
109        self.0.press(button).unwrap_or(());
110        self.0.release(button)
111    }
112}