Documentation
¶
Overview ¶
Package darwin provides a pure-Go macOS implementation of the robotgo API for desktop automation: mouse, keyboard and screen capture.
Unlike the upstream go-vgo/robotgo, this package uses NO Cgo. It is built entirely on top of the Quartz/CoreGraphics and CoreFoundation system frameworks, dynamically loaded at runtime through github.com/ebitengine/purego, so it cross-compiles with CGO_ENABLED=0.
The exported API mirrors the Windows and Wayland sibling packages (github.com/go-vgo/robotgo/win, github.com/go-vgo/robotgo/wayland) so callers can swap implementations per platform with a build tag.
Most input and screen-capture APIs silently fail (or return empty results) unless the host process has been granted Accessibility and Screen Recording permissions in System Settings → Privacy & Security. Window management (GetTitle/GetBounds/ActivePid/ActiveName/MinWindow/MaxWindow/CloseWindow) uses the Accessibility (AXUIElement) API and needs the Accessibility permission; MaxWindow toggles native full screen. Write operations never fall back to the frontmost window for a pid <= 0, except CloseWindow() called with no argument.
Index ¶
- Constants
- Variables
- func ActiveName(name string) error
- func ActivePid(pid int, args ...int) error
- func Alert(title, msg, ok, cancel string) bool
- func Capture(args ...int) (*image.RGBA, error)
- func CaptureImg(args ...int) (image.Image, error)
- func CheckAccess(prompt bool) bool
- func Click(args ...interface{}) error
- func CloseWindow(args ...int) error
- func CmdCtrl() string
- func DisplaysNum() int
- func DragSmooth(x, y int, args ...interface{}) error
- func GetActiveApp() (name, bundleID string, pid int)
- func GetBounds(pid int, args ...int) (x, y, w, h int)
- func GetMousePos() (int, int)
- func GetPid() int
- func GetPixelColor(x, y int, displayId ...int) string
- func GetScaleSize(displayId ...int) (int, int)
- func GetScreenSize() (int, int)
- func GetTitle(args ...int) string
- func GetVersion() string
- func Height(img image.Image) int
- func KeyDown(key string, args ...interface{}) error
- func KeyPress(key string, args ...interface{}) error
- func KeyTap(key string, args ...interface{}) error
- func KeyToggle(key string, args ...interface{}) error
- func KeyUp(key string, args ...interface{}) error
- func Kill(pid int) error
- func Location() (int, int)
- func MainDisplayID() int
- func MaxWindow(pid int, args ...interface{}) error
- func MinWindow(pid int, args ...interface{}) error
- func MouseDown(key ...interface{}) error
- func MouseUp(key ...interface{}) error
- func Move(x, y int, displayId ...int) error
- func MoveClick(x, y int, args ...interface{}) error
- func MoveRelative(x, y int) error
- func MoveSmooth(x, y int, args ...interface{}) bool
- func MultiClick(button string, count int) error
- func PadHex(hex uint32) string
- func Run(path string) ([]byte, error)
- func Save(img image.Image, path string, quality ...int) error
- func SaveCapture(path string, args ...int) error
- func SaveJpeg(img image.Image, path string, quality ...int) error
- func SavePng(img image.Image, path string) error
- func ScaleF(displayId ...int) float64
- func Scroll(x, y int, args ...int) error
- func ScrollDir(x int, direction ...interface{}) error
- func ScrollSmooth(to int, args ...int) error
- func Toggle(key ...interface{}) error
- func Type(str string, args ...int) int
- func TypeDelay(str string, delay int) error
- func TypeStr(str string, args ...int) error
- func Width(img image.Image) int
- type CGFloat
- type CGPoint
- type CGRect
- type CGSize
- type Nps
- type Point
- type Rect
- type Size
Constants ¶
const Version = "v0.1.0-darwin"
Version is the robotgo-darwin version string.
Variables ¶
var ErrNotFound = errors.New("robotgo: not found")
ErrNotFound is returned when no matching window or element is found.
var ErrNotSupported = errors.New("robotgo: operation not supported")
ErrNotSupported is returned when an operation is not supported by this pure-Go backend.
Functions ¶
func ActiveName ¶
ActiveName activates the app whose name best matches name (exact, then prefix, then substring; case insensitive) among apps with a window.
func ActivePid ¶ added in v1.3.2
ActivePid brings the application with pid to the foreground, restoring and raising its window. args are accepted for API parity: as in Cgo, pid is always an app pid.
func Alert ¶ added in v1.3.4
Alert shows a modal alert with the ok and cancel buttons and reports whether ok was chosen; false on failure.
func CaptureImg ¶
CaptureImg captures the screen and returns an image.Image. Optional args: x, y, w, h (region) and a trailing display index. With no args the full main display is captured.
func CheckAccess ¶ added in v1.2.0
CheckAccess reports whether the process is trusted for Accessibility (needed to post input events); prompt shows the system dialog if not.
func Click ¶
func Click(args ...interface{}) error
Click clicks a mouse button. Default is the left button. Use "left", "right", or "center". Pass true for a double-click.
func CloseWindow ¶
CloseWindow closes the frontmost app's window (no argument), or the window of the pid given as the first argument, by pressing its close button.
func DragSmooth ¶
DragSmooth moves the mouse smoothly while holding the left button down.
func GetActiveApp ¶ added in v1.2.0
GetActiveApp returns the frontmost app's localized name, bundle id and pid. The pid comes from the live AX / window server state (NSWorkspace's frontmostApplication is cached without a main run loop).
func GetBounds ¶ added in v1.3.2
GetBounds returns the window bounds (x, y, w, h) of pid's window; pid <= 0 selects the frontmost app. It returns zeros when not reachable. args are accepted for API parity: as in Cgo, pid is always an app pid.
func GetMousePos ¶
GetMousePos returns the current mouse position. Alias of Location.
func GetPixelColor ¶
GetPixelColor returns the pixel color at (x, y) as a 6-digit hex string.
func GetScaleSize ¶
GetScaleSize returns the screen size in physical pixels (point size times the backing scale), matching robotgo.GetScaleSize on the Cgo backend.
func GetScreenSize ¶
GetScreenSize returns the main display's width and height in pixels.
func GetTitle ¶
GetTitle returns the title of the frontmost app's window, or of the window of the pid given as the first argument. It returns "" when the window is not reachable (e.g. without the Accessibility permission).
func KeyTap ¶
KeyTap taps a key (press + release). Trailing arguments may be modifier names (or a []string of them) and an int pid: when a pid is supplied the event is delivered to that process via CGEventPostToPid, mirroring the C SendTo() helper; otherwise it is posted to the global HID event tap.
KeyTap("a")
KeyTap("a", "cmd")
KeyTap("a", "cmd", "shift")
KeyTap("a", pid)
KeyTap("a", pid, "cmd")
func KeyToggle ¶
KeyToggle toggles a key. Default is "down"; pass "up" to release. Trailing arguments may be modifier names (or a []string of them) and an int pid: when a pid is supplied the event is delivered to that process via CGEventPostToPid, mirroring the C SendTo() helper.
KeyToggle("a")
KeyToggle("a", "up")
KeyToggle("a", "up", "cmd")
KeyToggle("a", pid)
func MainDisplayID ¶
func MainDisplayID() int
MainDisplayID returns the main display's CGDirectDisplayID, mirroring the Cgo backend's GetMainId. Returns 0 when the frameworks are unavailable.
func MaxWindow ¶
MaxWindow enters (or exits, if the bool arg is false) native full screen for pid's window.
func MouseDown ¶
func MouseDown(key ...interface{}) error
MouseDown sends a mouse button down event.
func Move ¶
Move moves the mouse to absolute position (x, y). The optional displayId is accepted for API parity but ignored.
func MoveRelative ¶
MoveRelative moves the mouse relative to its current position.
func MoveSmooth ¶
MoveSmooth moves the mouse smoothly to (x, y) with an ease-in-out curve. Optional args: steps (int), sleepMs (int). Returns true on success.
func MultiClick ¶ added in v1.3.2
MultiClick clicks button count times at the current location; the n-th click carries click state n, so macOS sees a double/triple... click.
func SaveCapture ¶
SaveCapture captures the screen and saves it to a file.
func ScaleF ¶
ScaleF returns the backing scale factor (pixel width / point width) of the selected display, mirroring the Cgo backend's sys_scale. Returns 1 when the frameworks are unavailable or the mode cannot be read.
func Scroll ¶
Scroll scrolls the mouse by wheel notches. Positive y scrolls up, negative scrolls down; positive x scrolls left, negative scrolls right (matching robotgo's Cgo backend convention). Optional arg: delay ms.
func ScrollSmooth ¶
ScrollSmooth scrolls the mouse smoothly by `to` steps, repeating `num` times (default 5) with `tm` ms between steps (default 100). An optional third arg sets the horizontal offset per step.
func Toggle ¶
func Toggle(key ...interface{}) error
Toggle toggles a mouse button down or up.
Toggle("left") // press down
Toggle("left", "up") // release
func Type ¶
Type types a string using Unicode key events, supporting any character (including those not on the active keyboard layout). An optional first int argument is the target pid: when non-zero the events are delivered to that process via CGEventPostToPid, mirroring the C SendTo() helper.
Type("hello")
Type("hello", pid)
It returns the number of characters (runes) typed and stops at the first event that can not be posted.
Types ¶
type CGFloat ¶
type CGFloat = float64
CGFloat is a 64-bit C double on every 64-bit Apple platform (amd64/arm64).
type CGPoint ¶
type CGPoint struct {
X, Y CGFloat
}
CGPoint mirrors the C struct CGPoint { CGFloat x, y; }.
type CGSize ¶
type CGSize struct {
Width, Height CGFloat
}
CGSize mirrors the C struct CGSize { CGFloat width, height; }.
type Rect ¶
Rect represents a rectangle.
func GetScreenRect ¶
GetScreenRect returns the selected display's bounds (origin in global display space, size in points), defaulting to the main display.