Claudiu
Claudiu

Reputation: 229461

win32: moving mouse with SetCursorPos vs. mouse_event

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?

Upvotes: 7

Views: 11056

Answers (2)

Lord-JulianXLII
Lord-JulianXLII

Reputation: 1249

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.

Upvotes: 0

jay.lee
jay.lee

Reputation: 19837

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.

Upvotes: 7

Related Questions