robotgo

repository·master·Indexed 27 days ago

https://github.com/go-vgo/robotgo

A Golang library for desktop automation, auto-testing, and AI computer use. It provides APIs to control the mouse and keyboard, read the screen, handle windows, process images/bitmaps, and listen to global events. Supports macOS, Windows, and Linux (X11, Wayland, and libei), with experimental Cgo-free (pure-Go) backends available for cross-compilation.

Tokens
11.5K
Snippets
21
Records
105
Agent score
95%

What's inside robotgo

  1. System Requirements for Ubuntu

    master

    Run the following commands to install all necessary dependencies for Robotgo on Ubuntu:

    # Golang
    sudo snap install go  --classic
    
    # GCC
    sudo apt install gcc libc6-dev
    
    # X11
    sudo apt install libx11-dev xorg-dev libxtst-dev
    
    # Clipboard
    sudo apt install xsel xclip
    
    # Bitmap
    sudo apt install libpng++-dev
    
    # GoHook
    sudo apt install xcb libxcb-xkb-dev x11-xkb-utils libx11-xcb-dev libxkbcommon-x11-dev libxkbcommon-dev
    sudo snap install go  --classic
    sudo apt install gcc libc6-dev
    sudo apt install libx11-dev xorg-dev libxtst-dev
    sudo apt install xsel xclip
    sudo apt install libpng++-dev
    sudo apt install xcb libxcb-xkb-dev x11-xkb-utils libx11-xcb-dev libxkbcommon-x11-dev libxkbcommon-dev
  2. System Requirements for Windows

    master

    To install the necessary toolchains on Windows:

    • Go: winget install Golang.go
    • GCC (LLVM-MinGW): winget install MartinStorsjo.LLVM-MinGW.UCRT
    • OR GCC (WinLibs): winget install BrechtSanders.WinLibs.POSIX.UCRT

    Alternatively, you can download Mingw-w64 and add the bin directory (e.g., C:\mingw64\bin) to your system Path environment variable.

  3. Install Robotgo

    master

    To use Robotgo in your Go project, import the package directly. If you are not using Go modules, use the go get command.

    Using Go modules (Go 1.11+):

    import "github.com/go-vgo/robotgo"

    Manual installation:

    go get github.com/go-vgo/robotgo
    import "github.com/go-vgo/robotgo"
    
    // or
    
    go get github.com/go-vgo/robotgo
  4. Cross-compile to Windows from Ubuntu

    master
    To build a Windows binary from an Ubuntu system, you must install the necessary GCC multilib and MinGW-w64 toolchains. If you are using the bitmap functionality, you must also install the libz-mingw-w64-dev package to avoid zlib.h: No such file or directory errors.
  5. System Requirements for Robotgo

    master

    Robotgo requires Golang and GCC to be installed correctly. Depending on your operating system, additional dependencies are required for specific features like clipboard, bitmap, and event hooking.

    macOS

    • Install Go via Homebrew: brew install go
    • Install Xcode Command Line Tools: xcode-select --install
    • Privacy Settings: You must manually add Screen Recording and Accessibility permissions under System Settings > Privacy & Security > Accessibility, Screen & System Audio Recording for the automation to work.
  6. Cross-compile from Windows 64-bit to Windows 32-bit

    master

    To cross-compile a Go application from a 64-bit Windows environment to a 32-bit Windows target, enable CGO and set the architecture to 386.

    SET CGO_ENABLED=1
    SET GOARCH=386
    go build main.go
  7. Use Cgo-free (Pure-Go) Builds

    master

    Robotgo provides experimental pure-Go (Cgo-free) backends. These allow you to build without GCC, MinGW, Xcode, or X11 headers by setting CGO_ENABLED=0 and using specific build tags. This is useful for cross-compilation.

    Available Backends and Tags

    BackendBuild tagGo package
    Windows (Cgo-free)wingithub.com/go-vgo/robotgo/win
    macOS (Quartz via purego)macgithub.com/go-vgo/robotgo/darwin
    X11 (Linux, pure-Go X protocol)x11github.com/go-vgo/robotgo/x11
    Wayland (Linux, wlroots)waylandgithub.com/go-vgo/robotgo/wayland
    libei (Linux, GNOME/KDE portal)libeigithub.com/go-vgo/robotgo/libei
    Pure-Go default (all platforms)purego(selects mac/win/wayland above)

    Build Examples

    Cross-compile for Linux (X11) without Cgo:

    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags "purego,x11" .

    Build for Windows without Cgo/MinGW:

    CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -tags win .

    Build for macOS (Quartz via purego) without Xcode:

    CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -tags mac .

    Build for Wayland (wlroots-based compositor):

    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags wayland .

    Build for GNOME/KDE (via libei):

    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags libei .

    The purego shortcut: Using -tags purego automatically selects the best pure-Go backend for the target OS: mac on macOS, win on Windows, and wayland on Linux.

    go build -tags purego .
    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags "purego,x11" .
    CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -tags win .
    CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -tags mac .
    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags wayland .
    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags libei .
  8. System Requirements for Fedora

    master

    Run the following commands to install necessary dependencies on Fedora:

    # X11
    sudo dnf install libXtst-devel
    
    # Clipboard
    sudo dnf install xsel xclip
    
    # Bitmap
    sudo dnf install libpng-devel
    
    # GoHook
    sudo dnf install libxkbcommon-devel libxkbcommon-x11-devel xkbcomp-devel
    xorg-x11-xkb-utils-devel
    sudo dnf install libXtst-devel
    sudo dnf install xsel xclip
    sudo dnf install libpng-devel
    sudo dnf install libxkbcommon-devel libxkbcommon-x11-devel xkbcomp-devel
    xorg-x11-xkb-utils-devel
  9. Install the clipboard package

    master

    To use the clipboard functionality in your Go projects, install the package using go get:

    $ go get github.com/atotto/clipboard

    Platform Support and Requirements

    • OSX: Supported.
    • Windows: Supported (tested on Windows 7, likely works on others).
    • Linux/Unix: Requires either the xclip or xsel command to be installed on the system.
  10. Build robotgo with Wayland support

    master

    To use the Wayland input emulation backend on Linux, you must use the wayland build tag. Alternatively, you can use the cross-platform pure-Go default backends by using the purego tag.

    To build with Wayland support:

    go build -tags wayland ./...

    To build using the pure-Go default backends:

    go build -tags purego ./...
    go build -tags wayland ./...
    go build -tags purego ./...
  11. Build robotgo with the pure-Go macOS backend

    master

    To use the Cgo-free (pure-Go) macOS implementation which uses Quartz/CoreGraphics via purego, use the mac build tag. This excludes the default Cgo backend.

    Alternatively, you can use the cross-platform pure-Go default backends (mac on macOS, win on Windows, wayland on Linux) by using the purego tag.

  12. Enable the libei backend for Linux input emulation

    master

    To use the libei backend for Linux input emulation (which works on GNOME and KDE via the xdg-desktop-portal RemoteDesktop interface), you must build your project with the libei build tag. This backend excludes the Cgo/X11 and wlroots Wayland backends.

    Note that this backend does not support screen capture or window management; these operations will return ErrNotSupported.

    go build -tags libei ./...