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
use crate::error::WindowsError;

#[cfg(not(feature = "minimal"))]
use crate::input::{send_inputs, Input, MouseMotion, WheelDirection};

use winapi::shared::windef;
use winapi::um::winuser;

/// A zero-sized structure that wraps functions related to the mouse.
pub struct Mouse;

impl Mouse {
    /// Retrieve the current position of the mouse, in screen coordinates.
    ///
    /// ## Example
    ///
    /// ```rust, ignore
    /// use winput::Mouse;
    ///
    /// println!("The mouse is at {:?}", Mouse::position());
    /// ```
    pub fn position() -> Result<(i32, i32), WindowsError> {
        unsafe {
            let mut point: windef::POINT = std::mem::zeroed();

            // Calling C code
            if winuser::GetCursorPos(&mut point) != 0 {
                Ok((point.x, point.y))
            } else {
                Err(WindowsError::from_last_error())
            }
        }
    }

    /// Sets the position of the mouse, in screen coordinates.
    ///
    /// ## Example
    ///
    /// ```rust, ignore
    /// use winput::{Vk, Mouse};
    ///
    /// Mouse::set_position(50, 50).unwrap();
    /// ```
    pub fn set_position(x: i32, y: i32) -> Result<(), WindowsError> {
        unsafe {
            // Calling C code
            if winuser::SetCursorPos(x, y) == 0 {
                Err(WindowsError::from_last_error())
            } else {
                Ok(())
            }
        }
    }

    /// Synthesizes a vertical scroll event.
    ///
    /// If the function fails to synthesize the input, no error is emited and the
    /// function fails silently. If you wish to retreive an eventual error, use
    /// `send_inputs` instead.
    ///
    /// ## Example
    ///
    /// ```rust, ignore
    /// use winput::Mouse;
    ///
    /// Mouse::scroll(1.0).unwrap();
    /// ```
    #[cfg(not(feature = "minimal"))]
    pub fn scroll(amount: f32) {
        let input = Input::from_wheel(amount, WheelDirection::Vertical);
        send_inputs(&[input]);
    }

    /// Synthesizes a horizontal scroll event.
    ///
    /// If the function fails to synthesize the input, no error is emited and the
    /// function fails silently. If you wish to retreive an eventual error, use
    /// `send_inputs` instead.
    ///
    /// ## Example
    ///
    /// ```rust, ignore
    /// use winput::Mouse;
    ///
    /// Mouse::scrollh(1.0).unwrap();
    /// ```
    #[cfg(not(feature = "minimal"))]
    pub fn scrollh(amount: f32) {
        let input = Input::from_wheel(amount, WheelDirection::Horizontal);
        send_inputs(&[input]);
    }

    /// Moves the mouse relatively to its current position, in screen coordinates.
    ///
    /// If the function fails to synthesize the input, no error is emited and the
    /// function fails silently. If you wish to retreive an eventual error, use
    /// `send_inputs` instead.
    ///
    /// ## Example
    ///
    /// ```rust, ignore
    /// use winput::Mouse;
    ///
    /// Mouse::move_relative(100, 50).unwrap();
    /// ```
    #[cfg(not(feature = "minimal"))]
    pub fn move_relative(dx: i32, dy: i32) {
        let motion = MouseMotion::Relative { dx, dy };
        let input = Input::from_motion(motion);
        send_inputs(&[input]);
    }

    /// Moves the mouse using absolute normalized coordinates.
    ///
    /// If the function fails to synthesize the input, no error is emited and the
    /// function fails silently. If you wish to retreive an eventual error, use
    /// `send_inputs` instead.
    ///
    /// ## Example
    ///
    /// ```rust, ignore
    /// use winput::Mouse;
    ///
    /// // Move the mouse in the center of the main monitor.
    /// Mouse::move_absolute(0.5, 0.5).unwrap();
    /// ```
    #[cfg(not(feature = "minimal"))]
    pub fn move_absolute(x: f32, y: f32) {
        let motion = MouseMotion::Absolute {
            x,
            y,
            virtual_desk: false,
        };

        let input = Input::from_motion(motion);
        send_inputs(&[input]);
    }
}