How to detect when the mouse is moving outside the window? - c++

I'd like to detect the mouse move event when it's moving outside the window by using MFC. I have used OnNcMouseMove() but it only detects the mouse move event when it's moving to the non-client area, not outside the window. So how can I do to detect while the mouse is moving outside the window?

All I did was set up a 100ms timer that calls GetCursorPos(). You can tweak that timer value to make it higher resolution if needed, but for my specific UI need, 100ms was good enough - it dumped (x,y) coordinates to a status bar.

Related

How to move mouse to second screen monitor?

I'm making an application that needs to programmatically move the mouse pointer.
My ide is VC++ 2013.
I'm using the winuser.h header with this function:
SetCursorPos(int x, int y);
But this moves the mouse only on the primary screen.
For the case that has only one monitor this works fine.
But if I'm in presentation mode, with 2 monitors I need to move the mouse to the second monitor.
so, I to need detect if has 2 monitors and if it has, move the mouse to the second monitor.
Conceptually, the mouse moves (and windows are positioned) on a virtual desktop - a large desktop surface that spans all monitors. A monitor is a viewport onto this surface - it shows whatever happens to fall into a certain rectangle.
EnumDisplayMonitors gives you the list of all monitors, and for each a rectangle that this monitor reveals on the virtual desktop. To move a mouse cursor to a particular monitor, simply move it to a point that falls within the rectangle corresponding to that monitor.

Qt MouseMoveEvent only triggers with a mouse button press

I have an odd problem here.
I'm working on an application, and within one of my classes I'm monitoring my mouse events.
The weird thing is, my mouse move event will only get called if any mouse button is pressed.
I'm not even filtering for any button presses within the method; the method itself doesn't even get called unless I click on this object itself (the one that's monitoring it).
What generally causes this type of error to happen?
I'm not sure if it's relevant, but I have 2 different things monitoring my mouse inputs: 1) the main program monitoring the global mouse coordinates, and 2) an object within my program monitoring the mouse coordinates within itself.
Edit
So the problem has to be because mouse move event is generally used when people are dragging the cursor along the screen right?
My reason for not needing it like that is because I'm building a custom context menu of sorts, and I need to know when an item is hovered over.
It turns out that I didn't truly set everything within my class to enable mouse tracking.
I somehow thought if the class itself was set to have it enabled, I wouldn't need to set it to all the sub objects, but now I see how that wouldn't make any sense at all.
So just to clarify my solution:
The items that I needed to track my cursor's position needed to have
setMouseTracking(true);

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 can I receive or be able to process mouseMoveEvent(s) outside my widget window?

I am writing Qt application which plays a fade-in animation whenever the mouse is moved to a certain area in the screen, and a fade out animation whenever the mouse is moved out of that same area.
I've already found a similar question here in stack overflow, however, it does not quite answer my question. (similar question here)
If I install an event filter to the application, will I be able to see ALL the events in the application even if it's outside my widget window?
If not, I am aware of an alternative involving QWidget::grabMouse() inside a reimplementation of leaveEvent(). But if I do so, will I be able to interact with anything OUTSIDE my application?
edit: though i am using the Qt library, my application is only for deployment to Windows.
I'm fairly the certain the answer is no, because events outside of your widgets are handled by the OSs window manager (and propagated onto whatever application is in that space).
However you can get the mouse position anywhere on the screen by calling QCursor::pos(), you could poll at regular intervals to find out where the mouse is.
You could try creating a completely transparent window that stays on top of the area where you want to receive mouse events, with the Qt::WindowStaysOnTopHint, Qt::FramelessWindowHint and Qt::ToolTip flags (the last one might prevent the window from receiving the focus), the Qt::WA_TranslucentBackground attribute and the mouse tracking enabled.
If you are on Windows, you can create a global hook to receive every mouse message (right before it's sent to the window under the mouse pointer). Unfortunately I don't know whether this functionality exists in other OSs.