Capture Cursor

2010-03-13 14:41:03 UTC

Most people don’t know how to get the cursor that’s currently on the screen—a useful ability, especially if you’re writing screenshot or screen-recording software. I’ve written an app that demonstrates the technique, or at least tries to.

It’s a bit flaky. The API it uses, IOFramebuffer, doesn’t tell me how many frames there are or what format they’re in, so the app assumes ARGB in native byte-order and doesn’t worry about frames. This gives wrong results more of the time than I like.

I’ve filed a request for a higher-level API, which would make the task much easier and the app much shorter.

I’ve posted a build in the repository’s downloads area, in case you’d like to see it in action. If you want to build it yourself, you’ll need to download SGHotKeysLib and put the source where the Capture Cursor Xcode project expects it to be.

UPDATE 2010-08-28: I’ve pushed a change, [9cbec7dd5169], that deletes the IOFramebuffer-based category method and uses Snow Leopard’s new, far more reliable [NSCursor currentSystemCursor] instead. I suggest you do the same.

4 Responses to “Capture Cursor”

  1. Karsten Says:

    you could probably do it much simpler by calling CGSCreateRegisteredCursorImage

    here’s a snippet:

    NSInteger cid = CGSMainConnectionID();
    CGPoint outHotSpot;
    CGImageRef image = CGSCreateRegisteredCursorImage(cid, “com.apple.coregraphics.GlobalCurrent”, &outHotSpot);
    NSSize imageSize = NSMakeSize(CGImageGetWidth(image),CGImageGetHeight(image));
    NSImage* nsImage = [[NSImage alloc] initWithCGImage:image size:imageSize];

    [imageView setImage: nsImage];

    [nsImage release];
    [(NSObject*)image release];

    That’s how the screencapture command line tool does it and it’s pretty straight forward :-)

    Karsten

  2. Sean Says:

    Maybe I misunderstand, but can’t you use NSCursor’s currentSystemCursor or QDGetCursorData()?

  3. Peter Hosey Says:

    Oooh! Nice find, Sean. I must have missed that back when I was looking through the new-in-10.6 docs. ☺

  4. iosif hamlatzis Says:

    I cannot get the spinning ball (aka beach ball) image

Leave a Reply

Do not delete the second sentence.