How can I set the mouse position?
Asked Answered
I

5

5

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, CGPoint point), but I cannot figure out how to get the CGDirectDisplayID. Please tell me how to get the CGDirectDisplayID or a different method to set the mouse position.

Intercom answered 21/4, 2012 at 3:42 Comment(0)
R
7

The real answer to this question is:

CGMainDisplayID()

https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/Quartz_Services_Ref/index.html#//apple_ref/c/func/CGMainDisplayID

CGDisplayMoveCursorToPoint(CGMainDisplayID(), point);
Rue answered 26/1, 2015 at 13:26 Comment(4)
This worked for me. I also tried some of the swift solutions after converting them to the new syntax, but they no longer seem to work.Foretime
What's the difference between CGMainDisplayID() and kCGDirectMainDisplay?Abuzz
That’s the best solution, even for Swift developer. Thank you!Street
Surely that's "the real answer" only as soon as you can rid the world of second monitors?Immure
R
4

Try CGWarpMouseCursorPosition(). It doesn't require a display ID.

If you want a display ID, you can pick an element, using whatever criteria you like, from the array returned by [NSScreen screens]. Invoke -deviceDescription on that NSScreen object. From the dictionary that's returned, invoke -objectForKey: with the key @"NSScreenNumber".

Rubenrubens answered 21/4, 2012 at 11:51 Comment(0)
S
2

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).

Subchaser answered 21/4, 2012 at 3:49 Comment(2)
Does not work. For some reason, it sets the x of the mouse correctly, but if I try to change the Y, it flickers between the top and bottom of the screen. Even if I use the current position of the mouse.Intercom
I just tried it with a variety of x and y values, and it moved to each of them. Are you posting any other events, or modifying this one, in some other fashion before posting it?Subchaser
B
2

Here's a Swift version, just for kicks:

let pt = CGPoint(x: 100, y: 10)
let moveEvent = CGEventCreateMouseEvent(nil, .MouseMoved, pt, .Left)
CGEventPost(.CGSessionEventTap, moveEvent);
Byrann answered 7/9, 2016 at 3:37 Comment(0)
J
2

Swift 3 (and beyond) version of the answer that helped me:

let pt = CGPoint(x: 100, y: 10)

let moveEvent = CGEvent(mouseEventSource: nil, mouseType: .mouseMoved, 
mouseCursorPosition: pt, mouseButton: .left)

moveEvent?.post(tap: .cgSessionEventTap)
Jook answered 20/3, 2017 at 9:12 Comment(1)
still valid in Swift 5Actuality

© 2022 - 2024 — McMap. All rights reserved.