views:

288

answers:

2

What is the best way to store the current mouse position (system-wide) and then (later) put the mouse at that stored point?

[NSEvent mouseLocation] gets me the position, and I can move the mouse with a CGEventMouseMoved, but they each use a different co-ordinates system (I believe y=0 is the top for NSEvent and the bottom for a CGEvent).

I'm worried about the robustness of capturing the screen height and using it to convert between the two - or is this the best approach?

A: 

Yes, using the main screen's height is the standard way of flipping screen coordinates.

smorgan
Do you happen to know how I get the user's screen resolution in order to flip the y axis?
Ben Packard
Yes, see the various methods of `NSScreen`.
Rob Keniger
Great, thanks for your help.
Ben Packard
A: 

You could use a CGEventTap to get the mouse events instead of NSEvent. Then you have your choice of flipped or unflipped at the time the event arrives.

(I believe y=0 is the top for NSEvent and the bottom for a CGEvent).

You have that the wrong way around. Quartz uses flipped, which is y=0=top. AppKit uses unflipped, which is y=0=bottom.

Peter Hosey
But doesn't a tap just capture events? Maybe I understand it wrong. The mouse may be stationary (in fact likely will be) when I want to snap it to a new position and back again. The need to do this is not determined by another keyboard or mouse event, but something that happens periodically.
Ben Packard
Oh, so you're not monitoring events, but just getting and saving the mouse location at a specific time? Never mind about the tap, then. The part about the origins stands, though.
Peter Hosey

related questions