-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse_click_macos.go
48 lines (40 loc) · 1.74 KB
/
mouse_click_macos.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//go:build darwin
// +build darwin
// This file contains functions that use Core Graphics (via cgo) to simulate mouse events.
package main
/*
#cgo LDFLAGS: -framework ApplicationServices
#include <ApplicationServices/ApplicationServices.h>
#include <stdlib.h>
// postMouseClick simulates moving the mouse to (x,y) and then performing a left-click.
void postMouseClick(int x, int y) {
// Move the mouse pointer to the coordinate.
CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, CGPointMake(x, y), kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, move);
CFRelease(move);
// Simulate mouse button press.
CGEventRef clickDown = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, CGPointMake(x, y), kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, clickDown);
CFRelease(clickDown);
// Simulate mouse button release.
CGEventRef clickUp = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseUp, CGPointMake(x, y), kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, clickUp);
CFRelease(clickUp);
}
// moveMouse moves the mouse pointer to the specified coordinates without clicking.
void moveMouse(int x, int y) {
CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, CGPointMake(x, y), kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, move);
CFRelease(move);
}
*/
import "C"
import "image"
// clickAtAbsolutePointCG moves the mouse to the absolute coordinate and performs a click using Core Graphics.
func clickAtAbsolutePointCG(pt image.Point) {
C.postMouseClick(C.int(pt.X), C.int(pt.Y))
}
// moveMouseToCoordinateCG moves the mouse pointer (without clicking) to the specified absolute coordinate.
func moveMouseToCoordinateCG(pt image.Point) {
C.moveMouse(C.int(pt.X), C.int(pt.Y))
}