Getting "global" mouse position in Mac OS X

radex picture radex · Feb 14, 2010 · Viewed 14.3k times · Source

How can I get in Mac OS X "global" mouse position - I mean how can I in cocoa/cf/whatever find out cursor position even if it's outside the window, and even if my window is inactive?

I know it's somehow possible (even without admin permissions), because I've seen something like that in Java - but I want to write it in ObjC

Sorry for my English - I hope you'll understand what I mean ;)

Answer

Matt S. picture Matt S. · Feb 14, 2010
NSPoint mouseLoc;
mouseLoc = [NSEvent mouseLocation]; //get current mouse position
NSLog(@"Mouse location: %f %f", mouseLoc.x, mouseLoc.y);

If you want it to continuously get the coordinates then make sure you have an NSTimer or something similar