반응형
OS X에서 프로그래밍 방식으로 마우스 입력 시뮬레이션
OS X의 프로그램에서 마우스 동작을 시뮬레이션 할 수 있습니까? 특히, 짧은 버전은 두 개의 웹캠을 사용하여 터치 스크린을 시뮬레이션하려는 것입니다. 그래서 내가 X, Y 위치를 얻을 수 있다고 가정하면 마우스 움직임이나 클릭으로 OS에 정보를 보낼 수 있습니까?
편집-또는 다른 운영 체제에서 특히 쉽다면 기꺼이 고려할 것입니다.
예, 가능합니다. 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
마법을 즐기십시오.
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)
}
컴파일을 원하지 않고 셸 기반 도구를 찾고 있다면 Cliclick 이 해결책이 될 수 있습니다.
다음은 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
참고URL : https://stackoverflow.com/questions/2734117/simulating-mouse-input-programmatically-in-os-x
반응형
'UFO ET IT' 카테고리의 다른 글
.NET에서 PEM RSA 개인 키를 읽는 방법 (0) | 2020.11.27 |
---|---|
한 인터페이스가 다른 인터페이스를 상속해야하는 경우 (0) | 2020.11.27 |
File.ReadAllLines ()와 File.ReadAllText ()의 차이점은 무엇입니까? (0) | 2020.11.27 |
Excel 워크 시트의 이름 길이에 제한이 있습니까? (0) | 2020.11.27 |
속성 선택기 식 (0) | 2020.11.27 |