Skip to content

Instantly share code, notes, and snippets.

@annidy
Created August 18, 2014 02:51
Show Gist options
  • Save annidy/3e456034102a3fb9741f to your computer and use it in GitHub Desktop.
Save annidy/3e456034102a3fb9741f to your computer and use it in GitHub Desktop.
鼠标模拟点击
tell application "Finder"
set my_folder_path to container of (path to me) as text
set my_folder_path to POSIX path of my_folder_path
set my_path to my_folder_path & "smv"
end tell
tell application "Google Chrome"
activate
open location "http://www.baidu.com"
end tell
delay 5
tell application "System Events"
tell process "Chrome"
set frontmost to true
keystroke "Test"
do shell script (my_path & " 650 350 1") -- 1 click, 2 double click
display dialog "刚刚点击了一下哈!"
end tell
end tell
//
// main.c
// smv
//
// Created by FengXing on 13-12-25.
// Copyright (c) 2013年 FengXing. All rights reserved.
//
#include <CoreFoundation/CoreFoundation.h>
#include <CoreGraphics/CoreGraphics.h>
void d2(CGPoint pt)
{
CGEventRef mouseEvent = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, CGPointMake(pt.x, pt.y), kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, mouseEvent);
// Left button up
CGEventSetType(mouseEvent, kCGEventLeftMouseUp);
CGEventPost(kCGHIDEventTap, mouseEvent);
// 2nd click
CGEventSetIntegerValueField(mouseEvent, kCGMouseEventClickState, 2);
CGEventSetType(mouseEvent, kCGEventLeftMouseDown);
CGEventPost(kCGHIDEventTap, mouseEvent);
CGEventSetType(mouseEvent, kCGEventLeftMouseUp);
CGEventPost(kCGHIDEventTap, mouseEvent);
CFRelease(mouseEvent);
}
void d1(CGPoint pt)
{
// perform a click
CGEventRef leftDown = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, CGPointMake(pt.x, pt.y), kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, leftDown);
// Left button up
CGEventRef leftUp = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseUp, CGPointMake(pt.x, pt.y), kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, leftUp);
}
void help()
{
printf("Usage: smv x y [1|2] -- 1 click, 2 double click\n");
exit(0);
}
int main(int argc, char *argv[]) {
if (argc < 3) help();
CGError err = CGAssociateMouseAndMouseCursorPosition(1);
if (err) { printf("Opps Associate"); }
CGPoint point = {atof(argv[1]), atof(argv[2])};
CGWarpMouseCursorPosition(point);
// get current mouse pos
if (argc > 3) {
switch (atoi(argv[3])) {
case 1:
d1(point);
break;
case 2:
d2(point);
break;
}
}
CGAssociateMouseAndMouseCursorPosition(0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment