Skip to content

Instantly share code, notes, and snippets.

Created March 2, 2016 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/a046f16fbf82eb7d4b53 to your computer and use it in GitHub Desktop.
Save anonymous/a046f16fbf82eb7d4b53 to your computer and use it in GitHub Desktop.
static void DoPostLLEvent(CGEventRef event, const base::Closure& task) {
CGEventPost(kCGHIDEventTap, event);
// "My guess is that the events are getting posted too quickly for the system
// to handle (since it really only has to handle no more than,
// say, 20 key events a second)."
// http://stackoverflow.com/questions/6239140/cgkeyevent-pasting-working-with-breakpoints-but-not-without
usleep(100000); // wait 0.1 sec
if (!task.is_null()) {
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(&EventQueueWatcher, task));
}
}
bool SendMouseEventsLL(MouseButton type, int state) {
CHECK(g_ui_controls_enabled);
return SendMouseEventsNotifyWhenDoneLL(type, state, base::Closure());
}
bool SendMouseMoveNotifyWhenDoneLL(int x, int y, const base::Closure& task) {
g_mouse_location = NSMakePoint(x, y); // no flip in core graphics events!
CGPoint click_point = CGPointMake(x, y);
base::ScopedCFTypeRef<CGEventRef> move_event(
CGEventCreateMouseEvent(NULL,
kCGEventMouseMoved,
click_point,
kCGMouseButtonLeft)); // ignored
DoPostLLEvent(move_event, task);
return true;
}
bool SendMouseEventsNotifyWhenDoneLL(MouseButton type, int state,
const base::Closure& task) {
// On windows it appears state can be (UP|DOWN). It is unclear if
// that'll happen here but prepare for it just in case.
if (state == (UP|DOWN)) {
return (SendMouseEventsNotifyWhenDoneLL(
type, DOWN, base::Closure()) &&
SendMouseEventsNotifyWhenDoneLL(
type, UP, task));
}
CGEventType etype = 0;
CGMouseButton mouse_button = 0;
if (type == LEFT) {
mouse_button = kCGMouseButtonLeft;
if (state == UP) {
etype = kCGEventLeftMouseUp;
} else {
etype = kCGEventLeftMouseDown;
}
} else if (type == MIDDLE) {
mouse_button = kCGMouseButtonRight;
if (state == UP) {
etype = kCGEventOtherMouseUp;
} else {
etype = kCGEventOtherMouseDown;
}
} else if (type == RIGHT) {
mouse_button = kCGMouseButtonCenter;
if (state == UP) {
etype = kCGEventRightMouseUp;
} else {
etype = kCGEventRightMouseDown;
}
} else {
return false;
}
base::ScopedCFTypeRef<CGEventRef> event(
CGEventCreateMouseEvent(NULL,
etype,
NSPointToCGPoint(g_mouse_location),
mouse_button));
DoPostLLEvent(event, task);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment