How can I set the mouse position?

前端 未结 5 1402
谎友^
谎友^ 2021-01-15 03:54

I need to set the position of the mouse on the screen. In some other similar question, it was suggested to use CGDisplayMoveCursorToPoint(CGDirectDisplayID display, CG

5条回答
  •  北海茫月
    2021-01-15 04:04

    Here's a way to do it:

    // coordinate at (10,10) on the screen
    CGPoint pt;
    pt.x = 10;
    pt.y = 10;
    
    CGEventRef moveEvent = CGEventCreateMouseEvent(
        NULL,               // NULL to create a new event
        kCGEventMouseMoved, // what type of event (move)
        pt,                 // screen coordinate for the event
        kCGMouseButtonLeft  // irrelevant for a move event
    );
    
    // post the event and cleanup
    CGEventPost(kCGSessionEventTap, moveEvent);
    CFRelease(moveEvent);
    

    This will move the cursor to point (10,10) on screen (the upper left, next to the Apple menu).

提交回复
热议问题