Giter Club home page Giter Club logo

mouse-rs's Introduction

Rust Docs Crates.io

Mouse-rs

Mouse-rs is a rust library for controlling your mouse from a rust program, without having to go into your kernel yourself.

This project was loosely based on the python mouse library. Currently it supports macos, windows and linux (X11)! If you need any other OS added please open an issue

Installation

Add mouse-rs to your cargo.toml

[dependencies]
mouse-rs = "0.4"

Example

This is a simple example that moves your mouse to a position on screen and presses the left button.

use mouse_rs::{types::keys::Keys, Mouse};

fn move_and_press() {
    let mouse = Mouse::new();
    mouse.move_to(500, 500).expect("Unable to move mouse");
    mouse.press(&Keys::RIGHT).expect("Unable to press button");
    mouse.release(&Keys::RIGHT).expect("Unable to release button");
}

Usage

For more information please visit the docs

Linux disclaimer

If you're running into problems building on linux you need to install libxdo-dev.

Debian-based

sudo apt-get install -y libxdo-dev

Arch

sudo pacman -Sy xdotool

Fedora

sudo dnf install libX11-devel libxdo-devel

Gentoo

sudo emerge xdotool

mouse-rs's People

Contributors

altf02 avatar billyb2 avatar bwpge avatar dtibbs avatar mxve avatar s1gtrap avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mouse-rs's Issues

Animation?

I know that this crate doesn't have any animation logic, however I'd like to find a way to use this lib (move mouse cursor) with animation. Is there any crates to do this?

Windows scaling also scales mouse-rs locations

I was going mad at how the pointer location was inaccurate before I figured this out: windows scaling options also scales the mouse location when using move_to().
image
Despite my monitor being 1920x1080, move_to() is also scaled by 125%, meaning I reach the bottom right corner of the screen at move_to(1536, 864)

I realize this project hasn't been updated in a year but this is probably unintentional and I hope someone can get around to fixing it.

Worth noting it's not a huge deal if you know the current scale, but the problem is if I'm giving the program to someone else.

let mouse = Mouse::new();
let loc_x = (f64::from(INTENDED_X) / 1.25).floor() as i32; // Assuming you're at 125% scale. 
let loc_y = (f64::from(INTENDED_Y) / 1.25).floor() as i32; // Not 100% accurate because it has to be i32, so I have to floor or get average.
mouse.move_to(loc_x, loc_y);

Implement move_by

This function would move mouse by x, y. Instead of moving mouse absolute it would move relative.

[feature request] lock mouse in a rectangle

first of all, great package, fits my use case perfectly.
it'd be awesome to be able to lock the mouse in a box (not sure if this would be outside the scope of this crate).
Say for example I have coordinates, width and height.
I want to lock the mouse in that rectangle.
For example calling a function like fn lock_mouse(x: i32, y: i32, width: i32, height: i32) would lock the mouse in that rectangle.
kind of an equivalent to winit's set_cursor_grab (see here)
and, of course, a function to unlock it as well.

Getting the mouse position relative to the window

Hello, this library looks pretty good. Mouse::get_position returns the mouse's global position anywhere on the screen. How about having a method for getting the mouse's position relative to the window?

Mouse move does not generate events on macOS

Really appreciate the work done on this library, thank you :)

Issue

The Mouse::move_to method under sys::macos does not fire an event when the mouse is moved. From Apple docs, CGDisplayMoveCursorToPoint does not generate events. The result is that on macOS, the cursor is moved but nothing in the UI reacts to those movements (unlike Windows, where SetCursorPos generates mouse moved messages in the Win32 message loop).

Potential Solution

Use core_graphics::event::CGEvent::new_mouse_event with the CGEventType variant MouseMoved.

Commentary

I'm not sure if this intentional or not, but it is divergent from the Windows behavior. SetCursorPos does fire events (or messages) as a part of its implementation in the Win32 API.

Additional consideration is that generating mouse moved events on macOS may prompt the user to enable the application to take control through accessibility features, which may not be clear to the library consumer or the user of the resulting application.

Edit: fixed typo

Move mouse between monitors

Hi,
is there a way to determine on what monitor the mouse cursor is currently at?
I would like to write a simple utility tool, that allows to move the mouse cursor between monitors easily.

Thanks in advance

MIT license?

Hi!
Would you be willing to relicense under MIT?

I ask because:

  • Most of the rust ecosystem uses an MIT license.
  • MIT provides a few additional freedoms to the end user that GPL does not (source publishing, etc)
  • I'd like to use mouse-rs in a public library of my own, and I'd like to release it under an MIT license,
    but would not be able to as mouse-rs is GPL.

If not, that's fine, I thought I would ask.

Thank you!

Wayland support

Currently we only support X11, with the fast adoption of wayland we need to provide native support

move_to and get_position are using different types

I'm pretty new to rust and lower level programming languages in general, so this might just be missing knowledge on my side.
Is there a reason for get_position returning the coordinates as usize while move_to accepts i32?

I was just trying out some stuff and noticed I had to convert the position I'm moving to, to usize if I want to check wether the position was changed from the outside.
It's obviously not a huge issue, as long as theres a reason behind it. Otherwise I think changing it to both use the same type would improve ease of use.

Example code

use std::{thread, time};
use mouse_rs::Mouse;

fn main() {
    let mouse = Mouse::new();
    for i in 0..=1000 {
        mouse.move_to(i, i).expect("Unable to move mouse");

        thread::sleep(time::Duration::from_millis(1));

        let pos = mouse.get_position().unwrap();
        println!("{:?}", pos);

        // Convert i:i32 to i2:usize
        let i2 = i as usize;
        if pos.x != i2 || pos.y != i2 {
            break;
        }
    }
}

Edit:
I've opened a pull request for changing the Point struct to use i32 instead of usize, incase you don't like this change or have a reason why its plainly wrong please let me know :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.