Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture mouse cursor in screenshot

I am developing Mac desktop application, where I am capturing the screen using

CGImageRef screenShot = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionAll, kCGNullWindowID, kCGWindowImageDefault);

The problem is, I am expecting it should show the mouse cursor too, but it's not showing.

Do I need to enable any settings for that?

I tried the following before calling this function:

CGDisplayShowCursor(kCGDirectMainDisplay);
    
CGAssociateMouseAndMouseCursorPosition(true);

, but it didn't work.

When I checked using following

bool bCursor = CGCursorIsDrawnInFramebuffer(); /* This returns false */

bCursor = CGCursorIsVisible();  /* This returns true */

, te value says the cursor was not drawn in the frame buffer, however the cursor is visible.

I suppose I only need to do is to draw the cursor in the frame buffer, how do I do this?.

like image 533
Amitg2k12 Avatar asked Nov 04 '11 11:11

Amitg2k12


People also ask

How do you snip with mouse pointer?

You can see a Task Settings window, select the option “Capture”. Here make sure that “Show cursor in the Screenshots” is checked. Now place the cursor when you want to take the screenshot and press ctrl + PrtSc, now you can move the mouse and select the portion you want to take a screenshot.

How do I take a screenshot on Windows with a mouse?

To use the Snipping Tool when you have a mouse and a keyboard: Press Windows logo key + Shift + S. The desktop will darken while you select an area for your screenshot.


1 Answers

it seems, framebuffer doesn't give me the mouse cursor, so i am drawing my own, this is the code snippet , might be help full to you guys,

-(CGImageRef)appendMouseCursor:(CGImageRef)pSourceImage{
    // get the cursor image 
    NSPoint mouseLoc; 
    mouseLoc = [NSEvent mouseLocation]; //get cur

    NSLog(@"Mouse location is x=%d,y=%d",(int)mouseLoc.x,(int)mouseLoc.y);

    // get the mouse image 
    NSImage *overlay    =   [[[NSCursor arrowCursor] image] copy];

    NSLog(@"Mouse location is x=%d,y=%d cursor width = %d, cursor height = %d",(int)mouseLoc.x,(int)mouseLoc.y,(int)[overlay size].width,(int)[overlay size].height);

    int x = (int)mouseLoc.x;
    int y = (int)mouseLoc.y;
    int w = (int)[overlay size].width;
    int h = (int)[overlay size].height;
    int org_x = x;
    int org_y = y;

    size_t height = CGImageGetHeight(pSourceImage);
    size_t width =  CGImageGetWidth(pSourceImage);
    int bytesPerRow = CGImageGetBytesPerRow(pSourceImage);

    unsigned int * imgData = (unsigned int*)malloc(height*bytesPerRow);

    // have the graphics context now, 
    CGRect bgBoundingBox = CGRectMake (0, 0, width,height);

    CGContextRef context =  CGBitmapContextCreate(imgData, width, 
                                                  height, 
                                                  8, // 8 bits per component 
                                                  bytesPerRow, 
                                                  CGImageGetColorSpace(pSourceImage), 
                                                  CGImageGetBitmapInfo(pSourceImage));

    // first draw the image 
    CGContextDrawImage(context,bgBoundingBox,pSourceImage);

    // then mouse cursor 
    CGContextDrawImage(context,CGRectMake(0, 0, width,height),pSourceImage);

    // then mouse cursor 
    CGContextDrawImage(context,CGRectMake(org_x, org_y, w,h),[overlay CGImageForProposedRect: NULL context: NULL hints: NULL] );


    // assuming both the image has been drawn then create an Image Ref for that 

    CGImageRef pFinalImage = CGBitmapContextCreateImage(context);

    CGContextRelease(context);

    return pFinalImage; /* to be released by the caller */
}
like image 178
Amitg2k12 Avatar answered Oct 13 '22 14:10

Amitg2k12