Weird cursor behavioir in Qt - c++

In Qt (C++, MinGW, Windows 7), QCursor::setPos(...) seems to change the position of the cursor at the application level but not at the system level. For example, if you intercept the mouseMoveEvent and cancel out the movement of the cursor (with setMouseTracking(false) such that you do this cancellation only when a mouse button is pressed), then you'll see no cursor move as long as you maintain the mouse button pressed. But then, when you release the button and move a little, the cursor will go to the place it would have been if you haven't blocked its movement. Is this behavior normal? How to get around it such that the mouse cursor is changed at the system level?
If it is not clear enough, I can give code snippet.

Moving the cursor of a QWidget always uses the widget's coordinates. For global mouse positioning, you should use the desktop widget (QDesktopWidget) instead. You can get it through the QApplication::desktop() static function. Example:
QApplication::desktop()->cursor().setPos(0,0);
This should move the mouse cursor to the top-left corner of the desktop.

Related

Prevent touch input moving mouse cursor on Windows

I'm writing a C++ Win32 application that supports both mouse and touch input.
However when I put my finger on the touchscreen, then the OS moves the mouse cursor into that position.
If I move the finger across the screen, and move the mouse at the same time, then OS will cause mouse cursor flickering, one frame it's under finger, 2nd frame under old mouse position.
I want the touch input to stop changing mouse cursor position.
I want to keep the 2 inputs separate - touch not affecting the mouse.
I want to keep reading 2 inputs at the same time - touch not affecting the mouse.
Here's a solution.
Use WH_MOUSE_LL hook, and block mouse event cause by touching. Then compare MSLLHOOKSTRUCT->dwExtraInfo and 0xFF515700 to determine if it's a touch-caused mouse event.

Mouse click on a background window without moving mouse c++

I need to know if there is a code in c++ that allows to simulate a click, on a background window (if that's impossible, a foreground window will do fine), without moving the mouse.
I also need to click on specific coordinates, and drag an item to some other position (always without moving the mouse).
Example:
my mouse pointer is at (500,700),
but i need to left click at (100,150),
and drag to (700,300).
I need to be able to move my mouse pointer without affecting the program,
and the program must run correctly without moving, or locking, the mouse pointer.
If this action is impossible in c++, a VB code will be apreciated.
From a C++ Windows application, you can call Windows API directly by first finding the window using FindWindow that will give you the window handle you want, then finding the area within this window that you want to click. You can use API like GetWindowRect for this.
Finally, you can send this window, or an area within it, mouse messages using mouse_event function to cause the mouse to move, click, drag and let go at a new location.

Getting mouse cursor position and button state on Windows

What is the most appropriate way of getting the mouse cursor position or button state on Windows (Windows 7 and above)? I previously used DirectInput, but I am not longer using it and do not wish to. I saw there is GetCursorPos, however, I do not see anything for getting mouse button states. I have read previously that just reading the window messages (such as WM_LBUTTONUP) was considered "slow" for real time applications, so I do not know of any other option.
If you want to poll/query the current cursor position, you can use GetCursorPos. To see the button states, use GetAsyncKeyState.
If you are implementing a message loop in a window, the notification you will get for a mouse movement is WM_MOUSEMOVE. You will be notified of mouse inputs through the notifications listed here.
WM_LBUTTONUP is as good as any window message, for windowed games is great because it is generated only when the mouse clicks the client area, so you can resize and move the window freely.
As an alternative to direct input, you can use raw inputs which take up some more code to initialize, but it's the best way to go with the mouse movement since WM_INPUT is generated when the physical mouse moves, not the cursor, so you can clip the cursor in the client area without worrying that the user may hit the side of the clip rect and the mouse movement messages won't be generated anymore.
link

How to simulate mouse move and mouse click on Mac using C or C++

I am trying to simulate mouse move and mouse click on Mac using C or C++.
But unfortunately I don't find any Libraries for the same.
I have seen windows.h (works only for Windows) and also swinput (works for linux)
Is there anything like that for Mac?
CGPostMouseEvent has been deprecated in SnowLeopard. You can replace it with something like
CGEventRef mouseDownEv = CGEventCreateMouseEvent (NULL,kCGEventLeftMouseDown,pt,kCGMouseButtonLeft);
CGEventPost (kCGHIDEventTap, mouseDownEv);
CGEventRef mouseUpEv = CGEventCreateMouseEvent (NULL,kCGEventLeftMouseUp,pt,kCGMouseButtonLeft);
CGEventPost (kCGHIDEventTap, mouseUpEv );
CGEventRef CGEventCreateMouseEvent(
CGEventSourceRef source, // The event source may be taken from another event, or may be NULL.
CGEventType mouseType, // `mouseType' should be one of the mouse event types.
CGPoint mouseCursorPosition, // `mouseCursorPosition' should be the position of the mouse cursor in global coordinates.
CGMouseButton mouseButton); // `mouseButton' should be the button that's changing state;
// `mouseButton' is ignored unless `mouseType' is one of
// `kCGEventOtherMouseDown', `kCGEventOtherMouseDragged', or `kCGEventOtherMouseUp'.
Mouse button 0 is the primary button on the mouse.
Mouse button 1 is the secondary mouse button (right).
Mouse button 2 is the center button, and the remaining buttons are in USB device order.
kCGEventLeftMouseDown
kCGEventLeftMouseUp
kCGEventRightMouseDown
kCGEventRightMouseUp
kCGEventMouseMoved
kCGEventLeftMouseDragged
kCGEventRightMouseDragged
are now at your disposal.
My recommendation is that you check how the Mac ports of VNC do it.

Finding current mouse position in QT

This is my first attempt at writing a QT app, and I'm just trying to get a feel for how it works. My goal is to have a 400x400 widget which knows the exact position of the mouse when the mouse is hovering over it. For example, if the mouse was hovering in the top left corner, the position might be 10,10 (or something similar). If the mouse is in the bottom right corner, it might say 390,390.
Eventually, these coordinates will be displayed in a label on the main window, but that should be trivial. I'm stuck at the actual fetching of the coordinates. Any ideas?
For your widget, you must enable mouse tracking.
Then, you can either install an event filter, paying attention to mouse events and looking for the move event, or you can inherit from QWidget and override the mouse event, looking for mouse move events.
http://doc.qt.io/qt-4.8/qwidget.html#mouseTracking-prop
http://doc.qt.io/qt-4.8/eventsandfilters.html
http://doc.qt.io/qt-4.8/qmouseevent.html
If you are ever in a situation when you don't need actual tracking, just position at the moment, you can use QCursor::pos().