在OS X中,从程序中模拟鼠标的操作是否可能?具体来说,我正在尝试使用两个网络摄像头模拟触摸屏。所以,假设我可以获得X、Y位置,我可以将信息发送到操作系统作为鼠标移动或单击吗?
编辑-或者,如果在另一个操作系统中特别容易,我愿意考虑。
在OS X中,从程序中模拟鼠标的操作是否可能?具体来说,我正在尝试使用两个网络摄像头模拟触摸屏。所以,假设我可以获得X、Y位置,我可以将信息发送到操作系统作为鼠标移动或单击吗?
编辑-或者,如果在另一个操作系统中特别容易,我愿意考虑。
可以,你可以使用Quartz事件服务来模拟输入事件。
假设是C语言,我写了这个快速示例:
#include <ApplicationServices/ApplicationServices.h>
#include <unistd.h>
int main() {
// Move to 200x200
CGEventRef move1 = CGEventCreateMouseEvent(
NULL, kCGEventMouseMoved,
CGPointMake(200, 200),
kCGMouseButtonLeft // ignored
);
// Move to 250x250
CGEventRef move2 = CGEventCreateMouseEvent(
NULL, kCGEventMouseMoved,
CGPointMake(250, 250),
kCGMouseButtonLeft // ignored
);
// Left button down at 250x250
CGEventRef click1_down = CGEventCreateMouseEvent(
NULL, kCGEventLeftMouseDown,
CGPointMake(250, 250),
kCGMouseButtonLeft
);
// Left button up at 250x250
CGEventRef click1_up = CGEventCreateMouseEvent(
NULL, kCGEventLeftMouseUp,
CGPointMake(250, 250),
kCGMouseButtonLeft
);
// Now, execute these events with an interval to make them noticeable
CGEventPost(kCGHIDEventTap, move1);
sleep(1);
CGEventPost(kCGHIDEventTap, move2);
sleep(1);
CGEventPost(kCGHIDEventTap, click1_down);
CGEventPost(kCGHIDEventTap, click1_up);
// Release the events
CFRelease(click1_up);
CFRelease(click1_down);
CFRelease(move2);
CFRelease(move1);
return 0;
}
假设使用GCC编译,请使用以下命令:
gcc -o program program.c -Wall -framework ApplicationServices
享受神奇的体验。
these functions
严格指的是在文件 CGRemoteOperation.h
中声明的旧版事件相关函数,但这里没有使用到。 - jweyrichSwift
鼠标移动和点击示例:
func mouseMoveAndClick(onPoint point: CGPoint) {
guard let moveEvent = CGEvent(mouseEventSource: nil, mouseType: .mouseMoved, mouseCursorPosition: point, mouseButton: .left) else {
return
}
guard let downEvent = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: point, mouseButton: .left) else {
return
}
guard let upEvent = CGEvent(mouseEventSource: nil, mouseType: .leftMouseUp, mouseCursorPosition: point, mouseButton: .left) else {
return
}
moveEvent.post(tap: CGEventTapLocation.cghidEventTap)
downEvent.post(tap: CGEventTapLocation.cghidEventTap)
upEvent.post(tap: CGEventTapLocation.cghidEventTap)
}
这是一个基于jweyrick答案的工作中的C程序:
// Compile instructions:
//
// gcc -o click click.c -Wall -framework ApplicationServices
#include <ApplicationServices/ApplicationServices.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int x = 0, y = 0, n = 1;
float duration = 0.1;
if (argc < 3) {
printf("USAGE: click X Y [N] [DURATION]\n");
exit(1);
}
x = atoi(argv[1]);
y = atoi(argv[2]);
if (argc >= 4) {
n = atoi(argv[3]);
}
if (argc >= 5) {
duration = atof(argv[4]);
}
CGEventRef click_down = CGEventCreateMouseEvent(
NULL, kCGEventLeftMouseDown,
CGPointMake(x, y),
kCGMouseButtonLeft
);
CGEventRef click_up = CGEventCreateMouseEvent(
NULL, kCGEventLeftMouseUp,
CGPointMake(x, y),
kCGMouseButtonLeft
);
// Now, execute these events with an interval to make them noticeable
for (int i = 0; i < n; i++) {
CGEventPost(kCGHIDEventTap, click_down);
sleep(duration);
CGEventPost(kCGHIDEventTap, click_up);
sleep(duration);
}
// Release the events
CFRelease(click_down);
CFRelease(click_up);
return 0;
}
usleep(duration*1000000)
。否则,你只能睡整数秒! - nneonneo