Reputation:
I know [NSEvent mouseLocation]
gives me the current mouse position outside the event stream. Is there a way to get the mouse position in screen space but at the time of current event (so correlated with the mouse pos I get from mouse events)? I just want to avoid tracking it myself from all mouse events of all windows and storing it in a global.
Thanks.
Upvotes: 0
Views: 768
Reputation: 2117
If I understand correctly what you want, you can call [NSApp currentEvent]
to get the current event, and then call [NSEvent locationInWindow]
on the event returned by that method. That will give you the mouse location of the current event being processed by your application, as opposed to the mouse location at the present moment in time, supplied by [NSEvent mouseLocation]
.
If you need to convert the window-based location to a screen-based location, you can use the NSEvent
method -window
to get the window for the event, and then use [window convertRectToScreen:]
to convert the point to screen coordinates. It's a little weird that you have to use that NSRect
-based API; I don't understand why Apple deprecated convertBaseToScreen:
, which seemed like a perfectly good API. But if you put the window location in as the origin of an NSRect
, with a size of {0,0}
using NSMakeRect
, and then use convertRectToScreen:
and extract the origin back out to get an NSPoint
, it seems to convert the point just fine.
Upvotes: 2