views:

244

answers:

2

I've been using the following code to issue clicks programmatically on a Mac

void PostMouseEvent(CGMouseButton button, CGEventType type, const CGPoint point) 
{
    CGEventRef theEvent = CGEventCreateMouseEvent(NULL, type, point, button);
    CGEventSetType(theEvent, type);
    CGEventPost(kCGHIDEventTap, theEvent);
    CFRelease(theEvent);
}

void Click(const CGPoint point) 
{
    PostMouseEvent(kCGMouseButtonLeft, kCGEventMouseMoved, point);
    NSLog(@"Click!");
    PostMouseEvent(kCGMouseButtonLeft, kCGEventLeftMouseDown, point);
    PostMouseEvent(kCGMouseButtonLeft, kCGEventLeftMouseUp, point);
}

Now, I'm trying to click down to be able to drag objects, like a scroll bar or an application's window. I'm using the following:

PostMouseEvent(kCGMouseButtonLeft, kCGEventMouseMoved, point);
NSLog(@"Click Down!");
PostMouseEvent(kCGMouseButtonLeft, kCGEventLeftMouseDown, point);

When i ran the code above something interesting will happen, when the left mouse down is issue nothing seem to happen, I move my mouse and the window doesn't move, however when I added a mouse up event then the window jumped to the location where I supposedly dragged it. this is sort of OK, however, how do I can make the mouse click down and drag an object?

Note: I do have a whole method to see when the mouse stopped moving so I can click up.

please post code. Thanks

A: 

To drag, you must put the mouse button down first, then move the mouse.

Programmatically, you should probably post kCGEventLeftMouseDragged, not kCGEventMouseMoved, between the LeftMouseDown and LeftMouseUp events. You can install a custom event tap and log real mouse events to confirm or correct this.

Peter Hosey
I still see the same behavior when using kCGEventLeftMouseDragged.Any chance you can give me a snippet? I'm sure I'm doing something wrong. Thanks!
Uri
Ok, i tried creating an event with CGEventCreateMouseEvent() but I still see the same behavior...
Uri
Anyone? No one has an answer?
Uri
A: 

The only way to do what you want is setting a active CGEventTap to get a editable stream of kCGEventMouseMoved events and change them into kCGEventLeftMouseDragged events in the callback of the CGEventTap (obviously you need to synthesize the mouse down event first)

You would think that this is done automatically by sending a click event with no release and moving the mouse but that is not the case.

valexa