#mouse-event #mouse #events #listen #click #action #simulation

bin+lib mouce

A library that aims to help simulating and listening mouse actions across different platforms

16 releases

new 0.2.50 Dec 9, 2024
0.2.44 Mar 2, 2024
0.2.43 May 30, 2023
0.2.41 Nov 10, 2022
0.2.3 Jul 19, 2022

#126 in GUI

Download history 1/week @ 2024-08-22 2/week @ 2024-08-29 5/week @ 2024-09-05 8/week @ 2024-09-12 59/week @ 2024-09-19 19/week @ 2024-09-26 7/week @ 2024-10-03 15/week @ 2024-10-10 28/week @ 2024-10-17 12/week @ 2024-10-24 24/week @ 2024-10-31 25/week @ 2024-11-07 22/week @ 2024-11-14 11/week @ 2024-11-21 19/week @ 2024-11-28 119/week @ 2024-12-05

172 downloads per month
Used in 3 crates

MIT license

78KB
2K SLoC

mouce

Mouce is a library written in Rust that aims to help simulating and listening mouse actions across different platforms.

Supported platforms

  • Windows
    • Tested on Windows 10
    • Uses User32 system library
  • MacOS
    • Tested on a MacBook Pro (Retina, 13-inch, Mid 2014) with Big Sur installed on it
    • Uses CoreGraphics and CoreFoundation frameworks
  • Unix-like systems
    • X11
      • Tested on i3wm Arch Linux
      • Uses X11 and XTest libraries
    • Others (partially supported)
      • For other systems, you can disable the x11 feature and the library will use uinput
        • Use --no-default-features argument with cargo
        • Or disable default features in Cargo.toml
          [dependencies]
          mouce = { version = "x.y.z", default-features = false }
          
      • While using uinput there are some limitations for the library
        • get_position function is not implemented as uinput does not provide such a feature
        • The rest of the actions work and tested on KDE Wayland and sway

Library interface

/// Move the mouse to the given `x`, `y` coordinates
fn move_to(&self, x: usize, y: usize) -> Result<(), Error>;
/// Move the mouse relative to the current position
fn move_relative(&self, x_offset: i32, y_offset: i32) -> Result<(), Error>;
/// Get the current position of the mouse
fn get_position(&self) -> Result<(i32, i32), Error>;
/// Press down the given mouse button
fn press_button(&self, button: &MouseButton) -> Result<(), Error>;
/// Release the given mouse button
fn release_button(&self, button: &MouseButton) -> Result<(), Error>;
/// Click the given mouse button
fn click_button(&self, button: &MouseButton) -> Result<(), Error>;
/// Scroll the mouse wheel towards to the given direction
fn scroll_wheel(&self, direction: &ScrollDirection) -> Result<(), Error>;
/// Attach a callback function to mouse events
fn hook(&mut self, callback: Box<dyn Fn(&MouseEvent) + Send>) -> Result<CallbackId, Error>;
/// Remove the callback function with the given `CallbackId`
fn unhook(&mut self, callback_id: CallbackId) -> Result<(), Error>;
/// Remove all callback functions
fn unhook_all(&mut self) -> Result<(), Error>;

Example

This example program moves the mouse from left to right;

use std::thread;
use std::time::Duration;

use mouce::{Mouse, MouseActions};

fn main() {
    let mouse_manager = Mouse::new();

    let mut x = 0;
    while x < 1920 {
        let _ = mouse_manager.move_to(x, 540);
        x += 1;
        thread::sleep(Duration::from_millis(2));
    }
}

To see more examples, you can look at the documentation by running;

cargo doc --open

CLI binary

mouce comes with an example CLI program that uses mouce library functions. You can install the binary with;

cargo install mouce --features="cli"

and see mouce --help for further details.

Dependencies

~0.2–1MB
~20K SLoC