void testApp::addMouseForce(ofxVec2f& pt) { ofxVec2f worldLoc = pt + pos; ofxVec2f mouseLoc(mouseX, mouseY); ofxVec2f diff = mouseLoc - worldLoc; if (diff.length() < minMouseDist) { float pct = 1 - (diff.length() / minMouseDist ); diff *= pct; frc -= diff; torque -= pt.getPerpendicular().dot( (diff) ); } }
void processCommand(const char *cmd) { int tmpx, tmpy, btn; float tmpInterval; UInt32 tmpkc; char str[CMD_STRING_MAXLEN]; bzero(str, CMD_STRING_MAXLEN); if (IS_CMD(cmd, "mouselocation")) { CGPoint cgLoc = mouseLoc(); printf("%.f %.f\n", cgLoc.x, cgLoc.y); } else if (IS_CMD(cmd, "mousewarp ")) { print_msg("Warping mouse to location."); sscanf(cmd, "mousewarp %d %d", &tmpx, &tmpy); mouseMove(tmpx, tmpy); } else if (IS_CMD(cmd, "mousemove ")) { print_msg("Moving mouse."); sscanf(cmd, "mousemove %d %d", &tmpx, &tmpy); mouseMoveTo(tmpx, tmpy, 0.7); } else if (IS_CMD(cmd, "mousedown")) { print_msg("Pressing mouse button."); sscanf(cmd, "mousedown %d", &btn); mousePress(btn, SINGLE_CLICK); } else if (IS_CMD(cmd, "mouseup")) { print_msg("Releasing mouse button."); sscanf(cmd, "mouseup %d", &btn); mouseRelease(btn, SINGLE_CLICK); } else if (IS_CMD(cmd, "mouseclick")) { print_msg("Clicking mouse."); sscanf(cmd, "mouseclick %d", &btn); mouseClick(btn, SINGLE_CLICK); } else if (IS_CMD(cmd, "mousedoubleclick")) { print_msg("Double-clicking mouse."); sscanf(cmd, "mousedoubleclick %d", &btn); mouseClick(btn, DOUBLE_CLICK); } else if (IS_CMD(cmd, "mousetripleclick")) { print_msg("Triple-clicking mouse."); sscanf(cmd, "mousetripleclick %d", &btn); mouseClick(btn, TRIPLE_CLICK); } else if (IS_CMD(cmd, "mousedrag ")) { print_msg("Dragging mouse."); sscanf(cmd, "mousedrag %d %d", &tmpx, &tmpy); mouseDrag(LEFT_MOUSE, tmpx, tmpy); } else if (IS_CMD(cmd, "press ")) { print_msg("Pressing key."); sscanf(cmd, "press %x", &tmpkc); keyPress((CGKeyCode)tmpkc, NULL); } else if (IS_CMD(cmd, "release ")) { print_msg("Releasing key."); sscanf(cmd, "release %x", &tmpkc); keyRelease((CGKeyCode)tmpkc, NULL); } else if (IS_CMD(cmd, "hit ")) { print_msg("Hitting key."); sscanf(cmd, "hit %x", &tmpkc); keyHit((CGKeyCode)tmpkc, NULL); } else if (IS_CMD(cmd, "type ")) { print_msg("Typing."); strncpy(str, &cmd[5], CMD_STRING_MAXLEN); typeString(str); } else if (IS_CMD(cmd, "wait")) { print_msg("Waiting."); sscanf(cmd, "wait %f", &tmpInterval); usleep(1000000 * tmpInterval); } else { print_msg("I don't know what you want to do."); } }
CGPoint mouseLoc() { Point currLoc; GetGlobalMouse(&currLoc); CGPoint cgLoc = {.x = currLoc.h, .y = currLoc.v}; return cgLoc; } // btn: 0 = none, 1 = left, 2 = right, etc CGEventType mouseEventType(int btn, int btnState) { switch(btn) { case NO_MOUSE_BUTTON: return kCGEventMouseMoved; case LEFT_MOUSE: switch(btnState) { case MOUSE_UP: return kCGEventLeftMouseUp; case MOUSE_DRAGGED: return kCGEventLeftMouseDragged; default: return kCGEventLeftMouseDown; } case RIGHT_MOUSE: switch(btnState) { case MOUSE_UP: return kCGEventRightMouseUp; case MOUSE_DRAGGED: return kCGEventRightMouseDragged; default: return kCGEventRightMouseDown; } default: switch(btnState) { case MOUSE_UP: return kCGEventOtherMouseUp; case MOUSE_DRAGGED: return kCGEventOtherMouseDragged; default: return kCGEventOtherMouseDown; } } } void mouseEvent(int btn, int btnState, int clickType) { CGPoint currLoc; currLoc = mouseLoc(); CGEventType mouseType = mouseEventType(btn, btnState); CGMouseButton mb = (btn == LEFT_MOUSE) ? kCGMouseButtonLeft : (btn == RIGHT_MOUSE) ? kCGMouseButtonRight : kCGMouseButtonCenter; CGEventRef theEvent = CGEventCreateMouseEvent(NULL, mouseType, currLoc, mb); if (clickType) { CGEventSetIntegerValueField(theEvent, kCGMouseEventClickState, clickType); } CGEventPost(kCGHIDEventTap, theEvent); CFRelease(theEvent); }