Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting cursor position on Mac OS X

I want to get the cursor position. Is there any standard function for this?

I'm trying to make my program in C++. I'd like to avoid Cocoa. Not that I have anything against it, but I'd like to make my program cross-platform.

like image 401
Marc Avatar asked Jun 08 '11 00:06

Marc


People also ask

How do I get my mouse cursor position?

In Mouse Properties, on the Pointer Options tab, at the bottom, select Show location of pointer when I press the CTRL key, and then select OK. To see it in action, press CTRL.

How do I find the pixel position of my Mac screen?

In the ColorSync Utility app on your Mac, click Calculator in the toolbar of the ColorSync Utility window. Click the magnifying glass , then move the pointer over an area on the screen that you want to examine. The color values of pixels are shown in the ColorSync Utility window as you move the pointer.

How do you get the pixel coordinates of a cursor?

The closest stock solution I personally use is to take activate the utility to take a screenshot of a screen portion ( Command ⌘ + Shift ⇧ + 4 ). The cursor will turn into a crosshair with the screen's horizontal and vertical pixel coordinates.


1 Answers

You can use the following Core Graphics API, in CGEvent.h:

CGEventRef event = CGEventCreate(NULL);
CGPoint cursor = CGEventGetLocation(event);
CFRelease(event);

(Note that you can still use Cocoa in a cross-platform program, you just need to separate platform-specific code into different files instead of using #defines.)

like image 75
John Calsbeek Avatar answered Oct 21 '22 21:10

John Calsbeek