-1

I'm trying to get and set the cursor position before the system(Windows) update the cursor position or somehow set a limit to where the cursor can move.

Tried GetCursorPos(), It execute too late.

Tried WH_MOUSE_LL hook, It slowed down the cursor movement (basically introduced massive lags), and I couldn't change the mouse position..

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Barak
  • 45
  • 7
  • Sounds like an [XY Problem](http://xyproblem.info) to me. What are you ultimately trying to accomplish? – IInspectable Apr 22 '19 at 08:35
  • @IInspectable I'm trying to limit the cursor positioning globally(outside the application bounds) , let's say that I have an invisible 400px*400px box how can I limit the cursor to be in the box area? Sure that I can check if it's out of bounds and then clamp the position to the box bounds, but at this point the cursor is already out of the bounds, that's why I need to check where the cursor is going to be and then clamp it before the system change it's position. – Barak Apr 22 '19 at 09:25
  • That does not answer the question. What does keeping the cursor inside that rectangle accomplish? – Anders Apr 22 '19 at 09:38
  • @Anders literally nothing, I just need the cursor to be in the box. – Barak Apr 22 '19 at 09:52

2 Answers2

1

Call ClipCursor inside your low-level mouse hook. This is rather evil because the mouse is a shared resource.

A nice application only calls ClipCursor when it is the foreground application.

Anders
  • 97,548
  • 12
  • 110
  • 164
0

WH_MOUSE is more effective (WH_MOUSE_LL may significantly slow down your computer) but hook programs must be placed in DLL.

This allows hook processes to be loaded and run in each process.

On the other hand, the WH_MOUSE_LL process must switch back to the original process each time to run outside the process. This is a very slow operation.

Strive Sun
  • 5,988
  • 1
  • 9
  • 26
  • I can't test it atm so lets say that it worked and now I just need to set the cursor position, How do I do that? because `SetCursorPos()` will change the old cursor position. – Barak Apr 22 '19 at 10:11
  • @Barak A similar example:https://stackoverflow.com/questions/11180773/setwindowshookex-for-wh-mouse – Strive Sun Apr 22 '19 at 10:17
  • I know how to set it up but I don't know how to then change the future position.. – Barak Apr 22 '19 at 13:26
  • @Barak Is it not your intention to hook the mouse position and reset it? Why not use `SetCursorPos ()`? – Strive Sun Apr 23 '19 at 05:58
  • @Barak In addition, as @Anders said, because cursor is a shared resource. If an application confines the cursor, it must release the cursor by using `ClipCursor` before relinquishing control to another application. The function confines the cursor to a rectangular area on the screen. – Strive Sun Apr 23 '19 at 06:01