kwhat / libuiohook

A multi-platform C library to provide global keyboard and mouse hooks from userland.
Other
490 stars 123 forks source link

Getting mouse position without listening to an event. #177

Open LightCannon opened 11 months ago

LightCannon commented 11 months ago

Hello. First, I would like to thank you for this library

Second, I'm trying to do some logic with moving mouse in my Qt app. Basically, I want to move the mouse and send clicks/releases based on my logic (I'm going to control another apps from my app).

Here are the cross platform ways I can think of with what blocks me: 1- Use Qt's QCursor.setPos which moves mouse and listen to mouse events using your library. I'm storing x and y of mouse move to track the mouse so when I send press/release event the library can use the tracked x and y to press at this location.

problem: QCursor.setPos does not trigger any mouse move event. This solution would have been ideal if this was not happening.

2- Same as 1 but instead QCursor.setPos, I will use your library to send mouse press/releases problem: coordinates are not dpi aware, and has problems when multiple monitors are connected.

3- Find some way to get the mouse position without listening to event, and I will use that in the event when I press. problem: I do not know how to do so.

What is the right solution for this based on your library? Thanks in advance

kwhat commented 11 months ago

problem: QCursor.setPos does not trigger any mouse move event. This solution would have been ideal if this was not happening.

What OS is this happening on? I suspect it is Linux and that is a rather long conversation I don't want to type yet... You should try this on all 3 supported platforms, it will likely work on windows, macOS is a 50/50 but I suspect this works as well.

I will use your library to send mouse press/releases

This is the ideal solution as the library should see it's own generated events.

coordinates are not dpi aware, and has problems when multiple monitors are connected.

Please explain this part further... I don't quite understand why/how this is an issue. I don't have an issues with multiple monitors but many others have expressed frustration.

Find some way to get the mouse position without listening to event, and I will use that in the event when I press.

This is possible on all platforms, I just don't know the exact API for windows and macOS. In X11, the API is XQueryPointer and there are examples on how to use it.

justdie386 commented 11 months ago

I have wrote a function to get the x and y of the mouse for macos windows and x11 so that i could use it with libuiohook i could show the code here. Not sure how good it is but it seems to work fine when i tested it.

LightCannon commented 11 months ago

I have wrote a function to get the x and y of the mouse for macos windows and x11 so that i could use it with libuiohook i could show the code here. Not sure how good it is but it seems to work fine when i tested it.

Could you share it?

justdie386 commented 11 months ago

Ignore all the other files, all you'll need is in the osx, win32, and x11 folders https://github.com/justdie386/luaohook/tree/main/src/mouse i made some #define to make the data type different depending on each platform, the code might not be the most optimized, or the way files are placed too, idk, im quite far from being proficient in C. There could be better ways for the x11 to do it but the osx and win32 are I think the bare minimum to work.

#ifdef _WIN32
#include <windows.h>
#include <winuser.h>
#include "win32/win32.h"
#define coords POINT
#elif __APPLE__
#include <unistd.h>
#include "osx/osx.h"
#define coords CGPoint
#else
#include <unistd.h>
#include "x11/x11.h"
#define coords MouseCoordinates
#endif