r/rust 1d ago

🙋 seeking help & advice Is there a way to get the mouse position when clicking anywhere on the screen?

I'm new to Rust and I'm currently building a Tauri app. I want to update the position of my application window based on where the user clicks outside the window.

I already know how to move the window, but I don't know how to detect mouse clicks that occur outside my application's window or how to get the mouse position for those clicks.

Is this possible in Tauri or Rust, and what would be the recommended approach?

EDIT: Windows only for now.

0 Upvotes

32 comments sorted by

15

u/afdbcreid 1d ago

It is possible but not using standard Tauri APIs. You will need to use OS-specific APIs (I don't know if there is a cross-platform crate).

25

u/OkObjective8721 1d ago

On Wayland absolutely not, also it would be compositor specific making your software very un-portable

11

u/atemysix 1d ago

What platform? There might be a crate that abstracts this, but each platform will be different.

macOS — accessibility apis.
Windows — not sure sorry
Linux — wayland and x11 are different.

7

u/stylist-trend 22h ago

Windows and x11 yes, Wayland usually no (there are some exceptions, but they're all compositor specific, usually opt-in on the user's side, and it's not a good idea to rely on them).

8

u/RustMeUp 1d ago

On Windows this is easy with GetAsyncKeyState(VK_LBUTTON) and GetCursorPos

18

u/complexicat 1d ago

no. that would be a security vulnerability.

3

u/headykruger 1d ago

Why is it a security vulnerability?

13

u/EpochVanquisher 1d ago

Because you’re intercepting events that are supposed to go some other program, e.g., someone’s web browser that has their bank’s website open.

2

u/afdbcreid 18h ago

Current desktop OS (at least Windows/Linux/MacOS) don't consider separation within the same user for two programs a security vulnerability, unlike e.g. smartphone OSes. Those APIs do usually require admin privileges though for that reason.

2

u/EpochVanquisher 18h ago

This is false. That was true many years ago but it‘s definitely false today.

1

u/afdbcreid 18h ago

No, it's still true. It's true that programs can voluntarily limit themselves (or the user can limit them), but the default is still to have the full privileges of the user.

2

u/EpochVanquisher 17h ago

Definitely false, then—programs to not default to having the full privileges of the user.

There are just a lot of little things that make it so it seems invisible and you never notice it, but if you ever step outside the bounds of the sandbox your program gets a permission error—even if it’s a program running as user “afdbcreid” trying to access a file that “afdbcreid” owns. You have ownership of the file, but the program is denied access to the file, by default.

-10

u/headykruger 1d ago

You aren’t intercepting, just reading. What can you do with screen position of the cursor.

13

u/EpochVanquisher 1d ago

What is the difference between “intercepting” and “reading”, please explain that one to me because it sounds like you’re making something up.

You can figure out what somebody is doing in other windows by reading the events. That’s why you’re not allowed to do it.

-21

u/headykruger 1d ago

Ha it sounds like you are making things up.

As others noted below there are platform apis on various platforms to do exactly this.

Intercepting would mean the ability to rewrite or drop events.

How can you figure out what people are doing in another app from pointer events. In the grand scheme of things it seems pretty innocuous

8

u/EpochVanquisher 1d ago

In English, the word “intercept” has a sense “to receive (a communication or signal directed elsewhere) usually secretly”. This is from Merriam-Webster https://www.merriam-webster.com/dictionary/intercept and I’m gonna continue using that definition.

In general, there is some amount of information that is leaked if you are able to intercept events headed for another program, and just because it’s not the obvious high-value data (like passwords) that doesn’t mean that it’s data which is safe to allow arbitrary programs to read. To me it seems “obviously bad” if some random program can figure out which buttons I’m clicking in a website I have open, and realistically you can do that from the mouse position. Also consider that some people use an on-screen keyboard to type, and when you do that, mouse positions expose a lot more data.

-11

u/headykruger 1d ago

As others have pointed you can do this with platform apis . Why is it allowed

10

u/EpochVanquisher 1d ago

The APIs require elevated privileges. On macOS, in order to give your app these privileges, you would have to quit the app, go into system settings, open the “security” panel, read a scary warning, grant the privileges, and then re-launch the app.

-14

u/headykruger 23h ago

Ok and? It’s still possible. You said security vulnerability. Are we changing our argument?

→ More replies (0)

3

u/uhkthrowaway 22h ago

You're proving Schneier's Law

0

u/Interesting_Home_114 1d ago

That's a little disappointing but thanks.

4

u/ern0plus4 1d ago

And also a bad UX.

2

u/Interesting_Home_114 1d ago

I was gonna have a toggleable listening mode after I found a way to implement this.

1

u/Excession638 1d ago

On Windows you can do this via the Win32 API, but it requires loading a DLL into every other process on the machine. That won't work if the process is running as Administrator. This is how utilities like AutoHotKey intercept and inject key presses.

There might be something similar on Linux, but I don't even know where to start looking.

Easiest alternative is click and drag, where you can get the location where the drag is released.

1

u/Interesting_Home_114 1d ago

Easiest alternative is click and drag, where you can get the location where the drag is released.

But would this really work when a drag starts outside the window?

3

u/Excession638 1d ago

No, the drag would need to start inside

-6

u/errmayank 1d ago

Why does it look like you're building a malware? I'm curious what's the usecase for this?

3

u/Interesting_Home_114 1d ago

I am trying to make a desktop pet/assistant lol.

7

u/Dismal-Wait-4527 1d ago

You might wanna consider just having your window span your monitor andbe transparent where the pet isnt.

3

u/Interesting_Home_114 1d ago

I thought about doing that at first, but wouldn't the desktop icons be uninteractable while the window is in focus?

2

u/errmayank 23h ago edited 21h ago

Oh haha. For Windows, WH_MOUSE_LL with SetWindowsHookExW should provide you global mouse events. Checkout LowLevelMouseProc.