macos 在 OS X 中以编程方式模拟鼠标输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2734117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simulating mouse input programmatically in OS X
提问by Rob Lourens
Is it possible to simulate the actions of a mouse from a program in OS X? Specifically, the short version is that I'm trying to simulate a touchscreen using two webcams. So assuming I can get X,Y positions, can I send information to the OS as a mouse movement or click?
是否可以从 OS X 中的程序模拟鼠标的动作?具体来说,简短的版本是我正在尝试使用两个网络摄像头模拟触摸屏。因此,假设我可以获得 X、Y 位置,我可以通过鼠标移动或点击向操作系统发送信息吗?
Edit- Or if it's particularly easy in another operating system I'd be willing to consider that.
编辑 - 或者如果它在另一个操作系统中特别容易,我愿意考虑。
回答by jweyrich
Yes, it is possible. You can use the Quartz Event Servicesto simulate input events.
对的,这是可能的。您可以使用Quartz 事件服务来模拟输入事件。
Assuming C, I wrote this quick example:
假设 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;
}
And assuming GCC, compile with:
并假设 GCC,编译:
gcc -o program program.c -Wall -framework ApplicationServices
gcc -o program program.c -Wall -framework ApplicationServices
Enjoy the magic.
享受魔法。
回答by Alexey Pichukov
Swift
mouse move and click example:
Swift
鼠标移动和点击示例:
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)
}
回答by ghoti
回答by Dorian
Here is a working C program based on jweyrick's answer:
这是一个基于 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;
}
Hosted at https://gist.github.com/Dorian/5ae010cd70f02adf2107