Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move mouse between monitors #20

Open
on3iro opened this issue Feb 1, 2023 · 3 comments
Open

Move mouse between monitors #20

on3iro opened this issue Feb 1, 2023 · 3 comments

Comments

@on3iro
Copy link

on3iro commented Feb 1, 2023

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

@AltF02
Copy link
Owner

AltF02 commented Feb 3, 2023

I'm not sure how this would work in windows see: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setcursorpos

for x11, we currently https://github.com/AltF02/mouse-rs/blob/main/src/sys/linux.rs#L59 default to screen 0? I'm not sure if win32.h has something similar?

same thing for macos where we default to the main display https://github.com/AltF02/mouse-rs/blob/main/src/sys/macos.rs#L69

I am unable to test for macos and fully rely on external contributors for that :(

For x11, it could be a new function, or a new parameter. What do u think?

@on3iro
Copy link
Author

on3iro commented Feb 5, 2023

Thanks a lot for your response :)

Actually the system I am currently most concerned about (as its needed for my use case) would be OSX. I understand that it would be most preferable to have this on all target systems, though.
Is it possible to determine displays on OSX or do we only have access to the main display?
If we get access to all possible displays we could implement this for OSX and x11 and make a note, that the feature is not available on windows.
Could that work?

Unfortunately I am not at all familiar with these system specific things (though the core-graphics crate for OSX you are already using looks quite promising)

@bwpge
Copy link
Contributor

bwpge commented Feb 5, 2023

Chiming in for the macOS side. Just a note, with #21 this would change the behavior described above -- coordinates will be treated as global (not relative to the main display) if the PR is accepted.

The core_graphics crate has a lot of 1-to-1 analogs for Core Graphics functions, but others are behind "Rust" specific implementations. For example CGEventCreateMouseEvent is behind CGEvent::new_mouse_event.

To specifically answer your question, I think you would be looking for CGGetDisplaysWithPoint, however I don't see that when searching the core_graphics docs.

Doing some quick digging, I think you mainly want to work with core_graphics::CGDisplay. Do take this with a pinch of salt, I am not a CG expert and the crate documentation is a bit sparse:

  • Get all active displays: CGDisplay::active_displays
  • Create a new CGDisplay from a CGDirectDisplayID (e.g., the list of results from the active_displays associated function): CGDisplay::new
  • Get the bounds of a CGDisplay in global coordinates: CGDisplay::bounds (you can use this to sample points and move to them; again, these are global coordinates, so if the above PR is accepted you can directly use them without converting relative to the main display)
  • Check what monitor the mouse is on: use this library's Mouse::get_position and check which bounds returned from CGDisplay::bounds contains that point

It also has some lower level stuff but I'd recommend staying away from them if you're not comfortable with their patterns (e.g., the whole "call this function with some null/0 values, fill paramter with a length, allocate space for length, call again to fill with results" -- this is very common in low level APIs like Win32, Vulkan, etc.).

Edit: fix wording

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants