win32: moving mouse with SetCursorPos vs. mouse_event
Asked Answered
A

2

7

Is there any difference between moving the mouse in windows using the following two techniques?

win32api.SetCursorPos((x,y))

vs:

nx = x*65535/win32api.GetSystemMetrics(0)
ny = y*65535/win32api.GetSystemMetrics(1)
win32api.mouse_event(win32con.MOUSEEVENTF_ABSOLUTE|win32con.MOUSEEVENTF_MOVE,nx,ny)

Does anything happen differently in the way Windows processes the movements?

Antre answered 15/9, 2010 at 19:8 Comment(0)
A
7

I believe that mouse_event works by inserting the events into the mouse input stream where as SetCursorPos just moves the cursor around the screen. I don't believe that SetCursorPos generates any input events either (though I may be wrong).

The practical implications are that when you use SetCursorPos, it just moves the cursor around. Where as when you use mouse_event, it inserts the events in the input stream which will in turn generate input events for any programs that are listening. This has implications with programs that listen for lower level mouse events rather than just cursor clicks; games for instance. Also, if you're using mouse_event to move the cursor around and have cursor/pointer acceleration on, than the resulting mouse motion should be subject to whatever acceleration curves windows is using.

Amphichroic answered 15/9, 2010 at 19:27 Comment(2)
Another difference is that SetCursorPos works for setting the mouse position to a second monitor whereas absolute positioning on SendInput/mouse_event is restricted to the primary display.Rossi
@JaredUpdike that is not entirely correct. By combining the Bit Flags ABSOLUTE MOVE and VIRTUALDESK you can move the mouse to a secondary displayBiogen
B
0

The answer by jay.lee is correct. I just want to give an easy example on how the difference he pointed out may present itself in a concrete use case.

You can select/mark text by holding your left mouse button and dragging your cursor across. (in other words moving your cursor to a new position).

If we simulate the movement of the cursor/mouse with SetCursorPos no text will be selected.

If we however use the move input with SendInput (or mouse_event) the text in between our start pos and end pos will be selected/highlighted.

Biogen answered 29/5, 2023 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.