robotgo

package module
v1.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 5, 2026 License: Apache-2.0 Imports: 27 Imported by: 703

README

Robotgo

Build Status CircleCI Status codecov golangci-lint GoDoc GitHub release

English | 简体中文 | 繁體中文 | 日本語 | 한국어 | Français | Deutsch | Español | Русский | Português

Golang Desktop Automation, auto test and AI Computer Use.
Control the mouse, keyboard, read the screen, process, Window Handle, image and bitmap and global event listener.

RobotGo supports Mac, Windows, and Linux; and robotgo supports arm64 and x86-amd64.

I build Codg now, Easy code and work AI agent system: auto, asynchronous, concurrency, efficiently and High accuracy

Codg Demo

RobotGo-Pro get the JavaScript, Python, Lua and others version, tech supports, new features and newest robotgo version ("no open-source version now").

Contents

Docs

Binding

ADB, packaging android adb API.

Requirements

Go 1.26+ (see go.mod) and one backend:

  • Default (Cgo): CGO_ENABLED=1, a C compiler and the platform libraries below.
  • Pure Go (experimental): no C toolchain; see Cgo-free Builds.

Both need the feature-specific dependencies for the features you use.

Default Cgo setup
macOS

Go and the Xcode Command Line Tools (Clang):

brew install go
xcode-select --install

Permissions (Cgo and pure Go): in System Settings > Privacy & Security, grant the app or terminal Accessibility (input) and Screen Recording (capture).

Windows
winget install GoLang.Go

Plus one C toolchain. LLVM-MinGW:

winget install MartinStorsjo.LLVM-MinGW.UCRT
$env:CC = "clang"

Or MinGW-w64 (WinLibs):

winget install BrechtSanders.WinLibs.POSIX.UCRT
$env:CC = "gcc"

The toolchain's bin (e.g. C:\mingw64\bin) must be on PATH and match GOARCH. Any other Cgo-compatible compiler, such as a manual MinGW-w64 install, also works.

Bitmap ships libpng for MinGW-w64 only; with another toolchain, build libpng yourself.

Linux (X11)

Build: GCC, libc headers, X11/XTest headers. Runtime: an X server with XTEST and DISPLAY set. For native Wayland, see Cgo-free Builds.

Ubuntu / Debian:

# Go (Snap if the distro package is too old)
sudo snap install go --classic
# or: sudo apt install golang

# Core (Cgo): compiler, libc, X11, XTest
sudo apt install gcc libc6-dev libx11-dev xorg-dev libxtst-dev

# Clipboard (X11)
sudo apt install xsel xclip

# Bitmap: libpng
sudo apt install libpng++-dev

# GoHook: XCB, XKB
sudo apt install xcb libxcb-xkb-dev x11-xkb-utils libx11-xcb-dev libxkbcommon-x11-dev libxkbcommon-dev

Fedora:

# Core (Cgo): compiler, libc, X11, XTest
sudo dnf install gcc glibc-devel libX11-devel libXtst-devel

# Clipboard (X11)
sudo dnf install xsel xclip

# Bitmap: libpng
sudo dnf install libpng-devel

# GoHook: XKB (Fedora < 34: xorg-x11-xkb-utils-devel instead of xkbcomp-devel)
sudo dnf install libxkbcommon-devel libxkbcommon-x11-devel xkbcomp-devel
Feature-specific dependencies

Needed on top of the core setup, only for the features you use:

  • Linux clipboard (Cgo and pure Go): xclip or xsel on X11; wl-clipboard on Wayland:

    sudo apt install wl-clipboard   # Ubuntu / Debian
    sudo dnf install wl-clipboard   # Fedora
    
  • Bitmap: libpng headers.

  • GoHook: XCB/XKB headers on Linux.

Bitmap and GoHook are separate Cgo packages; a pure-Go RobotGo backend does not remove their C requirements.

Cgo-free Builds

Experimental pure-Go backends build and cross-compile with CGO_ENABLED=0 (no GCC, MinGW, Xcode or X11 headers). Same import path and API; a build tag selects the backend — CGO_ENABLED=0 alone selects nothing.

Target GOOS Build tag Package Runtime requirements
windows win win Win32 only, no extra runtime
darwin mac darwin System frameworks and privacy permissions
linux x11 x11 X server with XTEST, DISPLAY set
linux wayland wayland wlroots compositor, see Wayland
linux libei libei RemoteDesktop portal (GNOME/KDE), see libei

purego is a shortcut: mac on macOS, win on Windows, wayland on Linux. Add x11 or libei to override on Linux (-tags "purego,x11").

Use one backend tag matching GOOS; combinations such as x11,libei fail to compile (purego plus one Linux override is the only exception).

Build commands
# Current OS
CGO_ENABLED=0 go build -tags purego .

# Cross-compile
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 x11 .
CGO_ENABLED=0 GOOS=linux   GOARCH=amd64 go build -tags wayland .
CGO_ENABLED=0 GOOS=linux   GOARCH=amd64 go build -tags libei .

PowerShell: $env:CGO_ENABLED = "0"; go build -tags purego .

Build the module root (.) or your own package, not ./...: examples/ and some subpackages need Cgo-only APIs.

Limitations

The runtime requirements above still apply. Cgo-only APIs such as CaptureScreen / FreeBitmap do not exist; use CaptureImg. Unsupported operations return ErrNotSupported (or an empty result).

  • mac: window management (GetTitle, GetBounds, ActivePid, ActiveName, MinWindow, MaxWindow, CloseWindow) uses Accessibility and needs the Accessibility permission; MaxWindow toggles native full screen and GetClient equals GetBounds.
  • wayland, libei: Location() returns the last position injected by RobotGo, not the physical cursor. Clipboard needs wl-clipboard.
Wayland

A wlroots compositor (Sway, Hyprland, Wayfire, ...) with WAYLAND_DISPLAY and XDG_RUNTIME_DIR set, exposing:

Feature Protocol global
Mouse control zwlr_virtual_pointer_manager_v1
Keyboard control zwp_virtual_keyboard_manager_v1
Screen capture zwlr_screencopy_manager_v1
Window management zwlr_foreign_toplevel_manager_v1

GNOME and KDE do not provide these protocols; use libei there.

libei (GNOME / KDE)

Input goes through the xdg-desktop-portal RemoteDesktop D-Bus interface: needs a session bus, xdg-desktop-portal and a backend implementing it (xdg-desktop-portal-gnome, -kde or -wlr). The first run shows a consent dialog; the restore token is cached under $XDG_STATE_HOME/robotgo.

Absolute motion uses a linked ScreenCast stream (default); set libei.LinkScreenCast = false for relative motion only. Screen capture and window management report ErrNotSupported.

Installation

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

png.h: No such file or directory with Bitmap: see its libpng requirements and issues/47.

Update

go get -u github.com/go-vgo/robotgo

Note go1.10.x C file compilation cache problem, golang #24355. go mod vendor problem, golang #26366.

Examples

Mouse
package main

import (
  "fmt"
  "github.com/go-vgo/robotgo"
)

func main() {
  robotgo.MouseSleep = 300

  robotgo.Move(100, 100)
  fmt.Println(robotgo.Location())
  robotgo.Move(100, -200) // multi screen supported
  robotgo.MoveSmooth(120, -150)
  fmt.Println(robotgo.Location())

  robotgo.ScrollDir(10, "up")
  robotgo.ScrollDir(20, "right")

  robotgo.Scroll(0, -10)
  robotgo.Scroll(100, 0)

  robotgo.MilliSleep(100)
  robotgo.ScrollSmooth(-10, 6)
  // robotgo.ScrollRelative(10, -100)

  robotgo.Move(10, 20)
  robotgo.MoveRelative(0, -10)
  robotgo.DragSmooth(10, 10)

  robotgo.Click("wheelRight")
  robotgo.Click("left", true)
  robotgo.MoveSmooth(100, 200, 1.0, 10.0)

  robotgo.Toggle("left")
  robotgo.Toggle("left", "up")
}
Keyboard
package main

import (
  "fmt"

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

func main() {
  robotgo.Type("Hello World")
  robotgo.Type("だんしゃり", 0, 1)
  // robotgo.Type("テストする")

  robotgo.Type("Hi, Seattle space needle, Golden gate bridge, One world trade center.")
  robotgo.Type("Hi galaxy, hi stars, hi MT.Rainier, hi sea. こんにちは世界.")
  robotgo.Sleep(1)

  // ustr := uint32(robotgo.CharCodeAt("Test", 0))
  // robotgo.UnicodeType(ustr)

  robotgo.KeySleep = 100
  robotgo.KeyTap("enter")
  // robotgo.Type("en")
  robotgo.KeyTap("i", "alt", "cmd")

  arr := []string{"alt", "cmd"}
  robotgo.KeyTap("i", arr)

  robotgo.MilliSleep(100)
  robotgo.KeyToggle("a")
  robotgo.KeyToggle("a", "up")

  robotgo.WriteAll("Test")
  text, err := robotgo.ReadAll()
  if err == nil {
    fmt.Println(text)
  }
}
Screen
package main

import (
  "fmt"
  "strconv"

  "github.com/go-vgo/robotgo"
  "github.com/vcaesar/imgo"
)

func main() {
  x, y := robotgo.Location()
  fmt.Println("pos: ", x, y)

  color := robotgo.GetPixelColor(100, 200)
  fmt.Println("color---- ", color)

  sx, sy := robotgo.GetScreenSize()
  fmt.Println("get screen size: ", sx, sy)

  bit := robotgo.CaptureScreen(10, 10, 30, 30)
  defer robotgo.FreeBitmap(bit)

  img := robotgo.ToImage(bit)
  imgo.Save("test.png", img)

  num := robotgo.DisplaysNum()
  for i := 0; i < num; i++ {
    robotgo.DisplayID = i
    img1, _ := robotgo.CaptureImg()
    path1 := "save_" + strconv.Itoa(i)
    robotgo.Save(img1, path1+".png")
    robotgo.SaveJpeg(img1, path1+".jpeg", 50)

    img2, _ := robotgo.CaptureImg(10, 10, 20, 20)
    robotgo.Save(img2, "test_"+strconv.Itoa(i)+".png")

    x, y, w, h := robotgo.GetDisplayBounds(i)
    img3, err := robotgo.CaptureImg(x, y, w, h)
    fmt.Println("Capture error: ", err)
    robotgo.Save(img3, path1+"_1.png")
  }
}
Bitmap
package main

import (
  "fmt"

  "github.com/go-vgo/robotgo"
  "github.com/vcaesar/bitmap"
)

func main() {
  bit := robotgo.CaptureScreen(10, 20, 30, 40)
  // use `defer robotgo.FreeBitmap(bit)` to free the bitmap
  defer robotgo.FreeBitmap(bit)

  fmt.Println("bitmap...", bit)
  img := robotgo.ToImage(bit)
  // robotgo.SavePng(img, "test_1.png")
  robotgo.Save(img, "test_1.png")

  bit2 := robotgo.ToCBitmap(robotgo.ImgToBitmap(img))
  fx, fy := bitmap.Find(bit2)
  fmt.Println("FindBitmap------ ", fx, fy)
  robotgo.Move(fx, fy)

  arr := bitmap.FindAll(bit2)
  fmt.Println("Find all bitmap: ", arr)

  fx, fy = bitmap.Find(bit)
  fmt.Println("FindBitmap------ ", fx, fy)

  bitmap.Save(bit, "test.png")
}
OpenCV
package main

import (
  "fmt"
  "math/rand"

  "github.com/go-vgo/robotgo"
  "github.com/vcaesar/gcv"
  "github.com/vcaesar/bitmap"
)

func main() {
  opencv()
}

func opencv() {
  name := "test.png"
  name1 := "test_001.png"
  robotgo.SaveCapture(name1, 10, 10, 30, 30)
  robotgo.SaveCapture(name)

  fmt.Print("gcv find image: ")
  fmt.Println(gcv.FindImgFile(name1, name))
  fmt.Println(gcv.FindAllImgFile(name1, name))

  bit := bitmap.Open(name1)
  defer robotgo.FreeBitmap(bit)
  fmt.Print("find bitmap: ")
  fmt.Println(bitmap.Find(bit))

  // bit0 := robotgo.CaptureScreen()
  // img := robotgo.ToImage(bit0)
  // bit1 := robotgo.CaptureScreen(10, 10, 30, 30)
  // img1 := robotgo.ToImage(bit1)
  // defer robotgo.FreeBitmapArr(bit0, bit1)
  img, _ := robotgo.CaptureImg()
  img1, _ := robotgo.CaptureImg(10, 10, 30, 30)

  fmt.Print("gcv find image: ")
  fmt.Println(gcv.FindImg(img1, img))
  fmt.Println()

  res := gcv.FindAllImg(img1, img)
  fmt.Println(res[0].TopLeft.Y, res[0].Rects.TopLeft.X, res)
  x, y := res[0].TopLeft.X, res[0].TopLeft.Y
  robotgo.Move(x, y-rand.Intn(5))
  robotgo.MilliSleep(100)
  robotgo.Click()

  res = gcv.FindAll(img1, img) // use find template and sift
  fmt.Println("find all: ", res)
  res1 := gcv.Find(img1, img)
  fmt.Println("find: ", res1)

  img2, _, _ := robotgo.DecodeImg("test_001.png")
  x, y = gcv.FindX(img2, img)
  fmt.Println(x, y)
}
Event
package main

import (
  "fmt"

  // "github.com/go-vgo/robotgo"
  hook "github.com/robotn/gohook"
)

func main() {
  add()
  low()
  event()
}

func add() {
  fmt.Println("--- Please press ctrl + shift + q to stop hook ---")
  hook.Register(hook.KeyDown, []string{"q", "ctrl", "shift"}, func(e hook.Event) {
    fmt.Println("ctrl-shift-q")
    hook.End()
  })

  fmt.Println("--- Please press w---")
  hook.Register(hook.KeyDown, []string{"w"}, func(e hook.Event) {
    fmt.Println("w")
  })

  s := hook.Start()
  <-hook.Process(s)
}

func low() {
	evChan := hook.Start()
	defer hook.End()

	for ev := range evChan {
		fmt.Println("hook: ", ev)
	}
}

func event() {
  ok := hook.AddEvents("q", "ctrl", "shift")
  if ok {
    fmt.Println("add events...")
  }

  keve := hook.AddEvent("k")
  if keve {
    fmt.Println("you press... ", "k")
  }

  mleft := hook.AddEvent("mleft")
  if mleft {
    fmt.Println("you press... ", "mouse left button")
  }
}
Window
package main

import (
  "fmt"

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

func main() {
  fpid, err := robotgo.FindIds("Google")
  if err == nil {
    fmt.Println("pids... ", fpid)

    if len(fpid) > 0 {
      robotgo.Type("Hi galaxy!", fpid[0])
      robotgo.KeyTap("a", fpid[0], "cmd")

      robotgo.KeyToggle("a", fpid[0])
      robotgo.KeyToggle("a", fpid[0], "up")

      robotgo.ActivePid(fpid[0])

      robotgo.Kill(fpid[0])
    }
  }

  robotgo.ActiveName("chrome")

  isExist, err := robotgo.PidExists(100)
  if err == nil && isExist {
    fmt.Println("pid exists is", isExist)

    robotgo.Kill(100)
  }

  abool := robotgo.Alert("test", "robotgo")
  if abool {
 	  fmt.Println("ok@@@ ", "ok")
  }

  title := robotgo.GetTitle()
  fmt.Println("title@@@ ", title)
}

Authors

Plans

  • Better multiscreen support
  • Update Window Handle
  • Try to support Android and IOS

Contributors

License

Robotgo is primarily distributed under the terms of "the Apache License (Version 2.0)", with portions covered by various BSD-like licenses.

See LICENSE-APACHE, LICENSE.

Documentation

Overview

Package robotgo Go native cross-platform system automation.

Please make sure Golang, GCC is installed correctly before installing RobotGo;

See Requirements:

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

Installation:

With Go module support (Go 1.11+), just import:

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

Otherwise, to install the robotgo package, run the command:

go get -u github.com/go-vgo/robotgo

Index

Constants

View Source
const (
	// KeyA define key "a"
	KeyA = "a"
	KeyB = "b"
	KeyC = "c"
	KeyD = "d"
	KeyE = "e"
	KeyF = "f"
	KeyG = "g"
	KeyH = "h"
	KeyI = "i"
	KeyJ = "j"
	KeyK = "k"
	KeyL = "l"
	KeyM = "m"
	KeyN = "n"
	KeyO = "o"
	KeyP = "p"
	KeyQ = "q"
	KeyR = "r"
	KeyS = "s"
	KeyT = "t"
	KeyU = "u"
	KeyV = "v"
	KeyW = "w"
	KeyX = "x"
	KeyY = "y"
	KeyZ = "z"
	//
	CapA = "A"
	CapB = "B"
	CapC = "C"
	CapD = "D"
	CapE = "E"
	CapF = "F"
	CapG = "G"
	CapH = "H"
	CapI = "I"
	CapJ = "J"
	CapK = "K"
	CapL = "L"
	CapM = "M"
	CapN = "N"
	CapO = "O"
	CapP = "P"
	CapQ = "Q"
	CapR = "R"
	CapS = "S"
	CapT = "T"
	CapU = "U"
	CapV = "V"
	CapW = "W"
	CapX = "X"
	CapY = "Y"
	CapZ = "Z"
	//
	Key0      = "0"
	Key1      = "1"
	Key2      = "2"
	Key3      = "3"
	Key4      = "4"
	Key5      = "5"
	Key6      = "6"
	Key7      = "7"
	Key8      = "8"
	Key9      = "9"
	KeyGrave  = "`"
	KeyQuoter = '"'
	KeyQuote  = "'"

	// Backspace backspace key string
	Backspace = "backspace"
	Delete    = "delete"
	Enter     = "enter"
	Tab       = "tab"
	Esc       = "esc"
	Escape    = "escape"
	Up        = "up"    // Up arrow key
	Down      = "down"  // Down arrow key
	Right     = "right" // Right arrow key
	Left      = "left"  // Left arrow key
	Home      = "home"
	End       = "end"
	Pageup    = "pageup"
	Pagedown  = "pagedown"

	Fn  = "fn"
	F1  = "f1"
	F2  = "f2"
	F3  = "f3"
	F4  = "f4"
	F5  = "f5"
	F6  = "f6"
	F7  = "f7"
	F8  = "f8"
	F9  = "f9"
	F10 = "f10"
	F11 = "f11"
	F12 = "f12"
	F13 = "f13"
	F14 = "f14"
	F15 = "f15"
	F16 = "f16"
	F17 = "f17"
	F18 = "f18"
	F19 = "f19"
	F20 = "f20"
	F21 = "f21"
	F22 = "f22"
	F23 = "f23"
	F24 = "f24"

	Cmd  = "cmd"  // is the "win" key for windows
	CmdL = "cmdl" // left command
	CmdR = "cmdr" // right command
	// "command"
	Alt     = "alt"
	AltL    = "altl" // left alt
	AltR    = "altr" // right alt
	Ctrl    = "ctrl"
	CtrlL   = "ctrll" // left ctrl
	CtrlR   = "ctrlr" // right ctrl
	Control = "control"
	Shift   = "shift"
	ShiftL  = "shiftl" // left shift
	ShiftR  = "shiftr" // right shift
	// "right_shift", capslock
	Caps        = "caps"
	Capslock    = "capslock"
	Space       = "space"
	Print       = "print"
	Printscreen = "printscreen" // No Mac support
	Insert      = "insert"
	Menu        = "menu" // Windows only
	ScrollLock  = "scroll_lock"
	PauseBreak  = "pause_break"

	AudioMute    = "audio_mute"     // Mute the volume
	AudioVolDown = "audio_vol_down" // Lower the volume
	AudioVolUp   = "audio_vol_up"   // Increase the volume
	AudioPlay    = "audio_play"
	AudioStop    = "audio_stop"
	AudioPause   = "audio_pause"
	AudioPrev    = "audio_prev"    // Previous Track
	AudioNext    = "audio_next"    // Next Track
	AudioRewind  = "audio_rewind"  // Linux only
	AudioForward = "audio_forward" // Linux only
	AudioRepeat  = "audio_repeat"  //  Linux only
	AudioRandom  = "audio_random"  //  Linux only

	Num0    = "num0" // numpad 0
	Num1    = "num1"
	Num2    = "num2"
	Num3    = "num3"
	Num4    = "num4"
	Num5    = "num5"
	Num6    = "num6"
	Num7    = "num7"
	Num8    = "num8"
	Num9    = "num9"
	NumLock = "num_lock"

	NumDecimal = "num."
	NumPlus    = "num+"
	NumMinus   = "num-"
	NumMul     = "num*"
	NumDiv     = "num/"
	NumClear   = "num_clear"
	NumEnter   = "num_enter"
	NumEqual   = "num_equal"

	LightsMonUp     = "lights_mon_up"     // Turn up monitor brightness			No Windows support
	LightsMonDown   = "lights_mon_down"   // Turn down monitor brightness		No Windows support
	LightsKbdToggle = "lights_kbd_toggle" // Toggle keyboard backlight on/off		No Windows support
	LightsKbdUp     = "lights_kbd_up"     // Turn up keyboard backlight brightness	No Windows support
	LightsKbdDown   = "lights_kbd_down"
)

Defining a bunch of constants.

View Source
const (
	// Mleft mouse left button
	Mleft      = "left"
	Mright     = "right"
	Center     = "center"
	WheelDown  = "wheelDown"
	WheelUp    = "wheelUp"
	WheelLeft  = "wheelLeft"
	WheelRight = "wheelRight"
)
View Source
const (
	// Version get the robotgo version
	Version = "v2.00.0.1658, MT. Baker!"
)

Variables

View Source
var (
	// MouseSleep set the mouse default millisecond sleep time
	MouseSleep = 0
	// KeySleep set the key default millisecond sleep time
	KeySleep = 10

	// DisplayID set the screen display id
	DisplayID = -1

	// NotPid used the hwnd not pid in windows
	NotPid bool
	// Scale option the os screen scale
	Scale bool
)
View Source
var ErrSmoothMove = errors.New("robotgo: smooth move failed")

ErrSmoothMove is returned when MoveSmooth reports a failure

View Source
var Keycode = keycode.Keycode

Keycode robotgo hook key's code map

View Source
var MouseMap = keycode.MouseMap

MouseMap robotgo hook mouse's code map

View Source
var Special = keycode.Special

Special is the special key map

Functions

func ActiveName

func ActiveName(name string) error

ActiveName active the window by name

Examples:

robotgo.ActiveName("chrome")

func ActivePid added in v0.110.0

func ActivePid(pid int, args ...int) error

ActivePid active the window by Pid,

If args[0] > 0 on the Windows platform via a window handle to active, If args[0] > 0 on the unix platform via a xid to active

func ActivePidC added in v0.110.0

func ActivePidC(pid int, args ...int) error

ActivePidC active the window by Pid, If args[0] > 0 on the unix platform via a xid to active

func Alert added in v0.110.0

func Alert(title, msg string, args ...string) bool

Alert show a alert window Displays alert with the attributes. If cancel button is not given, only the default button is displayed

Examples:

robotgo.Alert("hi", "window", "ok", "cancel")

func ByteToImg added in v0.93.2

func ByteToImg(b []byte) (image.Image, error)

ByteToImg convert []byte to image.Image

func Capture added in v0.110.0

func Capture(args ...int) (*image.RGBA, error)

Capture capture the screenshot, use the CaptureImg default

func CaptureImg added in v0.100.1

func CaptureImg(args ...int) (image.Image, error)

CaptureImg capture the screen and return image.Image, error

func CharCodeAt

func CharCodeAt(s string, n int) rune

CharCodeAt char code at utf-8

func CheckAccess added in v1.2.0

func CheckAccess(p bool) bool

CheckAccess check the os accessibility (macOS Accessibility; always true on other platforms), p shows the system prompt when access is not granted

func CheckMouse

func CheckMouse(btn string) C.MMMouseButton

CheckMouse check the mouse button

func Click

func Click(args ...interface{}) error

Click click the mouse button and return error

robotgo.Click(button string, double bool)

Examples:

err := robotgo.Click() // default is left button
err := robotgo.Click("right")

func ClickV1 added in v1.0.1

func ClickV1(args ...interface{})

ClickV1 click the mouse button

robotgo.Click(button string, double bool)

Examples:

robotgo.Click() // default is left button
robotgo.Click("right")
robotgo.Click("wheelLeft")

func CloseMainDisplay added in v0.110.3

func CloseMainDisplay()

CloseMainDisplay close the main X11 display

func CloseWindow

func CloseWindow(args ...int)

CloseWindow close the window

func CmdCtrl added in v0.110.0

func CmdCtrl() string

CmdCtrl If the operating system is macOS, return the key string "cmd", otherwise return the key string "ctrl"

func CmdV added in v1.0.0

func CmdV(pid ...int) error

CmdV tap key command + v or control + v

func DecodeImg

func DecodeImg(path string) (image.Image, string, error)

DecodeImg decode the image to image.Image and return

func DisplaysNum added in v0.110.0

func DisplaysNum() int

DisplaysNum get the count of displays

func Drag deprecated

func Drag(x, y int, args ...string) error

Deprecated: use the DragSmooth(),

Drag drag the mouse to (x, y), It's not valid now, use the DragSmooth()

func DragMouse deprecated

func DragMouse(x, y int, args ...interface{})

Deprecated: use the DragSmooth(),

DragMouse drag the mouse to (x, y), It's same with the DragSmooth() now

func DragSmooth

func DragSmooth(x, y int, args ...interface{}) error

DragSmooth drag the mouse like smooth to (x, y)

Examples:

robotgo.DragSmooth(10, 10)

func FindIds

func FindIds(name string) ([]int, error)

FindIds finds the all processes named with a subset of "name" (case insensitive), return matched IDs.

func FindName

func FindName(pid int) (string, error)

FindName find the process name by the process id

func FindNames

func FindNames() ([]string, error)

FindNames find the all process name

func FindPath

func FindPath(pid int) (string, error)

FindPath find the process path by the process pid

func FreeBitmap

func FreeBitmap(bitmap CBitmap)

FreeBitmap free and dealloc the C bitmap

func FreeBitmapArr added in v0.100.1

func FreeBitmapArr(bit ...CBitmap)

FreeBitmapArr free and dealloc the C bitmap array

func GetActiveApp added in v1.2.0

func GetActiveApp() (string, string, int)

GetActiveApp get the active app info return name, bundle id (macOS) or executable path, and pid

func GetActiveC added in v0.110.3

func GetActiveC() C.MData

GetActiveC get the active window

func GetBHandle deprecated

func GetBHandle() int

Deprecated: use the GetHandle(),

GetBHandle get the window handle, Wno-deprecated

This function will be removed in version v1.0.0

func GetBounds

func GetBounds(pid int, args ...int) (int, int, int, int)

GetBounds get the window bounds

func GetClient added in v0.110.0

func GetClient(pid int, args ...int) (int, int, int, int)

GetClient get the window client bounds

func GetDisplayBounds added in v0.110.0

func GetDisplayBounds(i int) (x, y, w, h int)

GetDisplayBounds gets the display screen bounds

func GetHWNDByPid added in v0.110.0

func GetHWNDByPid(pid int) int

GetHWNDByPid get the hwnd by pid

func GetHandByPidC added in v0.110.3

func GetHandByPidC(pid int, args ...int) C.MData

GetHandByPidC get handle mdata by pid

func GetHandle

func GetHandle() int

GetHandle get the window handle

func GetLocationColor added in v0.110.0

func GetLocationColor(displayId ...int) string

GetLocationColor get the location pos's color

func GetMainId added in v0.110.0

func GetMainId() int

GetMainId get the main display id

func GetMousePos deprecated

func GetMousePos() (int, int)

Deprecated: use the function Location()

GetMousePos get the mouse's position return x, y

func GetPid added in v0.110.0

func GetPid() int

GetPid get the process id return int32

func GetPixelColor

func GetPixelColor(x, y int, displayId ...int) string

GetPixelColor get the pixel color return string

func GetPxColor

func GetPxColor(x, y int, displayId ...int) C.MMRGBHex

GetPxColor get the pixel color return C.MMRGBHex

func GetScaleSize

func GetScaleSize(displayId ...int) (int, int)

GetScaleSize get the screen scale size

func GetScreenSize

func GetScreenSize() (int, int)

GetScreenSize get the screen size

func GetText

func GetText(imgPath string, args ...string) (string, error)

GetText get the image text by tesseract ocr

robotgo.GetText(imgPath, lang string)

func GetTitle

func GetTitle(args ...int) string

GetTitle get the window title return string

Examples:

fmt.Println(robotgo.GetTitle())

ids, _ := robotgo.FindIds()
robotgo.GetTitle(ids[0])

func GetVersion

func GetVersion() string

GetVersion get the robotgo version

func GetXDisplayName

func GetXDisplayName() string

GetXDisplayName get XDisplay name (Linux)

func GetXid added in v0.110.0

func GetXid(xu *xgbutil.XUtil, pid int) (xproto.Window, error)

GetXid get the xid return window and error

func GetXidByPid added in v1.0.0

func GetXidByPid(xu *xgbutil.XUtil, pid int) (xproto.Window, error)

GetXidByPid get the xid from pid

func GetXidFromPid deprecated

func GetXidFromPid(xu *xgbutil.XUtil, pid int) (xproto.Window, error)

Deprecated: use the GetXidByPid(),

GetXidFromPid get the xid from pid

func GoString

func GoString(char *C.char) string

GoString trans C.char to string

func Height added in v0.100.2

func Height(img image.Image) int

Height return the image.Image height

func HexToRgb

func HexToRgb(hex uint32) *C.uint8_t

HexToRgb trans hex to rgb

func ImgSize added in v0.100.2

func ImgSize(path string) (int, int, error)

ImgSize get the file image size

func Is64Bit

func Is64Bit() bool

Is64Bit determine whether the sys is 64bit

func IsMain added in v0.110.0

func IsMain(displayId int) bool

IsMain is main display

func IsValid

func IsValid() bool

IsValid valid the window

func KeyDown added in v0.100.0

func KeyDown(key string, args ...interface{}) error

KeyDown press down a key

func KeyPress added in v0.100.0

func KeyPress(key string, args ...interface{}) error

KeyPress press key string

func KeyTap

func KeyTap(key string, args ...interface{}) error

KeyTap taps the keyboard code;

See keys supported:

https://github.com/go-vgo/robotgo/blob/master/docs/keys.md#keys

Examples:

robotgo.KeySleep = 100 // 100 millisecond
robotgo.KeyTap("a")
robotgo.KeyTap("i", "alt", "command")

arr := []string{"alt", "command"}
robotgo.KeyTap("i", arr)

robotgo.KeyTap("k", pid int)

func KeyToggle

func KeyToggle(key string, args ...interface{}) error

KeyToggle toggles the keyboard, if there not have args default is "down"

See keys:

https://github.com/go-vgo/robotgo/blob/master/docs/keys.md#keys

Examples:

robotgo.KeyToggle("a")
robotgo.KeyToggle("a", "up")

robotgo.KeyToggle("a", "up", "alt", "cmd")
robotgo.KeyToggle("a", "up", []string{"alt", "cmd"})
robotgo.KeyToggle("k", pid int)

func KeyUp added in v0.100.0

func KeyUp(key string, args ...interface{}) error

KeyUp press up a key

func Kill

func Kill(pid int) error

Kill kill the process by PID

func Location added in v0.110.0

func Location() (int, int)

Location get the mouse location position return x, y

func MaxWindow

func MaxWindow(pid int, args ...interface{})

MaxWindow set the window max

func MicroSleep deprecated

func MicroSleep(tm float64)

Deprecated: use the MilliSleep(),

MicroSleep time C.microsleep(tm)

func MilliSleep

func MilliSleep(tm int)

MilliSleep sleep tm milli second

func MinWindow

func MinWindow(pid int, args ...interface{})

MinWindow set the window min

func MouseButtonString added in v1.0.1

func MouseButtonString(btn C.MMMouseButton) string

MouseButtonString converts a C.MMMouseButton to a readable name.

func MouseClick deprecated

func MouseClick(args ...interface{})

Deprecated: use the Click(),

MouseClick click the mouse

robotgo.MouseClick(button string, double bool)

func MouseDown added in v0.110.0

func MouseDown(key ...interface{}) error

MouseDown send mouse down event

func MouseUp added in v0.110.0

func MouseUp(key ...interface{}) error

MouseUp send mouse up event

func Move

func Move(x, y int, displayId ...int) error

Move move the mouse to (x, y)

Examples:

robotgo.MouseSleep = 100  // 100 millisecond
robotgo.Move(10, 10)

func MoveArgs

func MoveArgs(x, y int) (int, int)

MoveArgs get the mouse relative args

func MoveClick

func MoveClick(x, y int, args ...interface{}) error

MoveClick move and click the mouse

robotgo.MoveClick(x, y int, button string, double bool)

Examples:

robotgo.MouseSleep = 100
robotgo.MoveClick(10, 10)

func MoveMouse deprecated

func MoveMouse(x, y int)

Deprecated: use the Move(),

MoveMouse move the mouse

func MoveMouseSmooth deprecated

func MoveMouseSmooth(x, y int, args ...interface{}) bool

Deprecated: use the MoveSmooth(),

MoveMouseSmooth move the mouse smooth, moves mouse to x, y human like, with the mouse button up.

func MoveRelative

func MoveRelative(x, y int) error

MoveRelative move mouse with relative

func MoveScale added in v0.110.0

func MoveScale(x, y int, displayId ...int) (int, int)

MoveScale calculate the os scale factor x, y

func MoveSmooth

func MoveSmooth(x, y int, args ...interface{}) bool

MoveSmooth move the mouse smooth, moves mouse to x, y human like, with the mouse button up.

robotgo.MoveSmooth(x, y int, low, high float64, mouseDelay int)

Examples:

robotgo.MoveSmooth(10, 10)
robotgo.MoveSmooth(10, 10, 1.0, 2.0)

func MoveSmoothRelative

func MoveSmoothRelative(x, y int, args ...interface{}) error

MoveSmoothRelative move mouse smooth with relative

func MovesClick

func MovesClick(x, y int, args ...interface{}) error

MovesClick move smooth and click the mouse

use the `robotgo.MouseSleep = 100`

func Mul deprecated

func Mul(x int) int

Deprecated: use the ScaledF(),

Mul mul the scale, drop

func MultiClick added in v1.0.1

func MultiClick(button string, count int, click ...bool) error

MultiClick performs multiple clicks and returns error

robotgo.MultiClick(button string, count int)

func OpenImg

func OpenImg(path string) ([]byte, error)

OpenImg open the image return []byte

func PadHex

func PadHex(hex C.MMRGBHex) string

PadHex trans C.MMRGBHex to string

func PadHexs added in v0.110.0

func PadHexs(hex CHex) string

PadHexs trans CHex to string

func Paste added in v1.0.0

func Paste(str string, pid ...int) (int, error)

Paste paste a string (supported UTF-8), write the string to clipboard and tap `cmd + v`, return the pasted character (rune) count

func PasteStr deprecated

func PasteStr(str string) error

PasteStr paste a string

Deprecated: use the Paste()

func Pastes added in v1.2.0

func Pastes(str string, pid ...int) int

Pastes paste a string and return the pasted character count, 0 on failure

func PidExists

func PidExists(pid int) (bool, error)

PidExists determine whether the process exists

func Pids

func Pids() ([]int, error)

Pids get the all process id

func Read added in v0.100.0

func Read(path string) (image.Image, error)

Read read the file return image.Image

func ReadAll

func ReadAll() (string, error)

ReadAll read string from clipboard

func RgbToHex

func RgbToHex(r, g, b uint8) C.uint32_t

RgbToHex trans rgb to hex

func Run added in v0.100.9

func Run(path string) ([]byte, error)

Run run a cmd shell

func Save added in v0.100.0

func Save(img image.Image, path string, quality ...int) error

Save create a image file with the image.Image

func SaveCapture

func SaveCapture(path string, args ...int) error

SaveCapture capture screen and save the screenshot to image

func SaveCaptureGo added in v1.1.0

func SaveCaptureGo(path string, args ...int) error

SaveCaptureGo capture screen and save the screenshot to image

func SaveImg

func SaveImg(b []byte, path string) error

SaveImg save the image by []byte

func SaveJpeg added in v0.100.0

func SaveJpeg(img image.Image, path string, quality ...int) error

SaveJpeg save the image by image.Image

func SavePng added in v0.92.0

func SavePng(img image.Image, path string) error

SavePng save the image by image.Image

func Scale0 deprecated added in v0.100.1

func Scale0() int

Deprecated: use the ScaledF(),

Scale0 return ScaleX() / 0.96, drop

func Scale1 deprecated added in v0.110.0

func Scale1() int

Deprecated: use the ScaledF(),

Scale1 get the screen scale (only windows old), drop

func ScaleF added in v0.100.8

func ScaleF(displayId ...int) float64

ScaleF get the system scale val

func ScaleX deprecated

func ScaleX() int

Deprecated: use the ScaledF(),

ScaleX get the primary display horizontal DPI scale factor, drop

func Scaled

func Scaled(x int, displayId ...int) int

Scaled get the screen scaled return scale size

func Scaled0 added in v0.100.8

func Scaled0(x int, f float64) int

Scaled0 return int(x * f)

func Scaled1 added in v0.110.0

func Scaled1(x int, f float64) int

Scaled1 return int(x / f)

func Scroll

func Scroll(x, y int, args ...int) error

Scroll scroll the mouse to (x, y)

robotgo.Scroll(x, y, msDelay int)

Examples:

robotgo.Scroll(10, 10)

func ScrollDir added in v0.110.0

func ScrollDir(x int, direction ...interface{}) error

ScrollDir scroll the mouse with direction to (x, "up") supported: "up", "down", "left", "right"

Examples:

robotgo.ScrollDir(10, "down")
robotgo.ScrollDir(10, "up")

func ScrollRelative added in v0.100.1

func ScrollRelative(x, y int, args ...int) error

ScrollRelative scroll mouse with relative

Examples:

robotgo.ScrollRelative(10, 10)

func ScrollSmooth added in v0.100.7

func ScrollSmooth(to int, args ...int) error

ScrollSmooth scroll the mouse smooth, default scroll 5 times and sleep 100 millisecond

robotgo.ScrollSmooth(toy, num, sleep, tox)

Examples:

robotgo.ScrollSmooth(-10)
robotgo.ScrollSmooth(-10, 6, 200, -10)

func SetActive

func SetActive(win Handle)

SetActive set the window active

func SetActiveC added in v0.110.3

func SetActiveC(win C.MData)

SetActiveC set the window active

func SetDelay

func SetDelay(d ...int)

SetDelay sets the key and mouse delay robotgo.SetDelay(100) option the robotgo.KeySleep and robotgo.MouseSleep = d

func SetHandle

func SetHandle(hwnd int)

SetHandle set the window handle

func SetHandlePid

func SetHandlePid(pid int, args ...int)

SetHandlePid set the window handle by pid

func SetXDisplayName

func SetXDisplayName(name string) error

SetXDisplayName set XDisplay name (Linux)

func Sleep

func Sleep(tm int)

Sleep time.Sleep tm second

func StrToImg added in v0.94.0

func StrToImg(data string) (image.Image, error)

StrToImg convert base64 string to image.Image

func SysScale

func SysScale(displayId ...int) float64

SysScale get the sys scale

func ToByteImg added in v0.94.0

func ToByteImg(img image.Image, fm ...string) []byte

ToByteImg convert image.Image to base64-encoded []byte in format fm (default "jpeg"), nil on failure

func ToImage added in v0.92.0

func ToImage(bit CBitmap) image.Image

ToImage convert C.MMBitmapRef to standard image.Image

func ToInterfaces added in v0.110.0

func ToInterfaces(fields []string) []interface{}

ToInterfaces convert []string to []interface{}

func ToMMBitmapRef

func ToMMBitmapRef(bit CBitmap) C.MMBitmapRef

ToMMBitmapRef trans CBitmap to C.MMBitmapRef

func ToMMRGBHex

func ToMMRGBHex(hex CHex) C.MMRGBHex

ToMMRGBHex trans CHex to C.MMRGBHex

func ToRGBA added in v0.99.9

func ToRGBA(bit CBitmap) *image.RGBA

ToRGBA convert C.MMBitmapRef to standard image.RGBA

func ToRGBAGo added in v0.99.9

func ToRGBAGo(bmp1 Bitmap) *image.RGBA

ToRGBAGo convert Bitmap to standard image.RGBA

func ToStringImg added in v0.94.0

func ToStringImg(img image.Image, fm ...string) string

ToStringImg convert image.Image to string

func ToStrings added in v0.110.0

func ToStrings(fields []interface{}) []string

ToStrings convert []interface{} to []string

func ToUC added in v0.99.1

func ToUC(text string) []string

ToUC trans string to unicode []string

Runes outside ASCII become the Xlib keysym name ("U4e16", "U1F600").

func ToUint8p added in v0.99.9

func ToUint8p(dst []uint8) *uint8

ToUint8p convert the []uint8 to uint8 pointer

func Toggle added in v0.100.7

func Toggle(key ...interface{}) error

Toggle toggle the mouse, support button:

	"left", "center", "right",
 "wheelDown", "wheelUp", "wheelLeft", "wheelRight"

Examples:

robotgo.Toggle("left") // default is down
robotgo.Toggle("left", "up")

func Try

func Try(fun func(), handler func(interface{}))

Try handler(err)

func Type added in v1.0.0

func Type(str string, args ...int) int

Type type a string (supported UTF-8)

robotgo.Type(string: "The string to send", int: pid, "milli_sleep time", "x11 option")

Examples:

robotgo.Type("abc@123, Hi galaxy, こんにちは")
robotgo.Type("To be or not to be, this is questions.", pid int)

It returns the count of characters typed, stops at the first failure.

func TypeDelay added in v1.0.0

func TypeDelay(str string, delay int) error

TypeDelay type string with delayed And you can use robotgo.KeySleep = 100 to delayed not this function

func TypeStr

func TypeStr(str string, args ...int) error

TypeStr tap a string and return error

func TypeStrDelay deprecated

func TypeStrDelay(str string, delay int) error

TypeStrDelay type string width delay

Deprecated: use the TypeDelay()

func TypeStringDelayed deprecated

func TypeStringDelayed(str string, delay int)

Deprecated: use the TypeStr(),

TypeStringDelayed type string delayed, Wno-deprecated

This function will be removed in version v1.0.0

func U8ToHex

func U8ToHex(hex *C.uint8_t) C.MMRGBHex

U8ToHex trans *C.uint8_t to C.MMRGBHex

func U32ToHex

func U32ToHex(hex C.uint32_t) C.MMRGBHex

U32ToHex trans C.uint32_t to C.MMRGBHex

func UnicodeType

func UnicodeType(str uint32, args ...int) error

UnicodeType tap the uint32 unicode

func Width added in v0.100.2

func Width(img image.Image) int

Width return the image.Image width

func WriteAll

func WriteAll(text string) error

WriteAll write string to clipboard

Types

type Bitmap

type Bitmap struct {
	ImgBuf        *uint8
	Width, Height int

	Bytewidth     int
	BitsPixel     uint8
	BytesPerPixel uint8
}

Bitmap define the go Bitmap struct

The common type conversion of bitmap:

https://github.com/go-vgo/robotgo/blob/master/docs/keys.md#type-conversion

func CaptureGo added in v0.110.0

func CaptureGo(args ...int) Bitmap

CaptureGo capture the screen and return bitmap(go struct)

func ImgToBitmap added in v0.99.9

func ImgToBitmap(m image.Image) (bit Bitmap)

ImgToBitmap convert the standard image.Image to Bitmap

func RGBAToBitmap added in v0.99.9

func RGBAToBitmap(r1 *image.RGBA) (bit Bitmap)

RGBAToBitmap convert the standard image.RGBA to Bitmap

func ToBitmap

func ToBitmap(bit CBitmap) Bitmap

ToBitmap trans C.MMBitmapRef to Bitmap; a nil ref (failed capture, e.g. macOS without the Screen Recording permission) yields an empty Bitmap.

type CBitmap

type CBitmap C.MMBitmapRef

CBitmap define CBitmap as C.MMBitmapRef type

func ByteToCBitmap added in v0.110.0

func ByteToCBitmap(by []byte) CBitmap

ByteToCBitmap trans []byte to CBitmap

func CaptureScreen

func CaptureScreen(args ...int) CBitmap

CaptureScreen capture the screen return bitmap(c struct), use `defer robotgo.FreeBitmap(bitmap)` to free the bitmap

robotgo.CaptureScreen(x, y, w, h int)

func ImgToCBitmap added in v0.110.0

func ImgToCBitmap(img image.Image) CBitmap

ImgToCBitmap trans image.Image to CBitmap

func ToCBitmap

func ToCBitmap(bit Bitmap) CBitmap

ToCBitmap trans Bitmap to C.MMBitmapRef

type CHex

type CHex C.MMRGBHex

CHex define CHex as c rgb Hex type (C.MMRGBHex)

func UintToHex

func UintToHex(u uint32) CHex

UintToHex trans uint32 to robotgo.CHex

type Handle added in v0.110.3

type Handle C.MData

Handle define window Handle as C.MData type

func GetActive

func GetActive() Handle

GetActive get the active window

func GetHandById added in v0.110.3

func GetHandById(id int, args ...int) Handle

GetHandById get handle mdata by id

func GetHandByPid added in v0.110.3

func GetHandByPid(pid int, args ...int) Handle

GetHandByPid get handle mdata by pid

func GetHandPid deprecated

func GetHandPid(pid int, args ...int) Handle

Deprecated: use the GetHandByPid(),

GetHandPid get handle mdata by pid

type Map

type Map map[string]interface{}

Map a map[string]interface{}

type Nps

type Nps struct {
	Pid  int
	Name string
}

Nps process struct

func Process

func Process() ([]Nps, error)

Process get the all process struct

type Point added in v0.100.3

type Point struct {
	X int
	Y int
}

Point is point struct

type Rect added in v0.100.3

type Rect struct {
	Point
	Size
}

Rect is rect structure

func GetDisplayRect added in v0.110.0

func GetDisplayRect(i int) Rect

GetDisplayRect gets the display rect

func GetScreenRect added in v0.100.3

func GetScreenRect(displayId ...int) Rect

GetScreenRect get the screen rect (x, y, w, h)

type Size added in v0.100.3

type Size struct {
	W, H int
}

Size is size structure

Directories

Path Synopsis
https://github.com/golang/go/issues/26366
https://github.com/golang/go/issues/26366
Package clipboard read/write on clipboard
Package clipboard read/write on clipboard
cmd/gocopy command
cmd/gopaste command
example command
Command cmd is a placeholder for the robotgo CLI.
Command cmd is a placeholder for the robotgo CLI.
cuse
agent
Package agent is a placeholder for the computer-use agent.
Package agent is a placeholder for the computer-use agent.
Package darwin provides a pure-Go macOS implementation of the robotgo API for desktop automation: mouse, keyboard and screen capture.
Package darwin provides a pure-Go macOS implementation of the robotgo API for desktop automation: mouse, keyboard and screen capture.
key command
mouse command
scale command
screen command
window command
internal
xharness
Package xharness is a test-only helper: it owns a focused X11 window and reads back the text that window receives, through XLookupString, so a test sees exactly what an application would be given for injected key events (layout, Shift and AltGr levels applied by the server).
Package xharness is a test-only helper: it owns a focused X11 window and reads back the text that window receives, through XLookupString, so a test sees exactly what an application would be given for injected key events (layout, Shift and AltGr levels applied by the server).
Package libei provides a pure-Go libei/portal implementation of the robotgo API for desktop automation on Wayland compositors that do NOT expose the wlroots virtual-input protocols — most importantly GNOME (Mutter) and KDE Plasma (KWin).
Package libei provides a pure-Go libei/portal implementation of the robotgo API for desktop automation on Wayland compositors that do NOT expose the wlroots virtual-input protocols — most importantly GNOME (Mutter) and KDE Plasma (KWin).
Package wayland provides a pure-Go Wayland implementation of the robotgo API for desktop automation on wlroots-based compositors (Sway, Hyprland, Wayfire, etc.).
Package wayland provides a pure-Go Wayland implementation of the robotgo API for desktop automation on wlroots-based compositors (Sway, Hyprland, Wayfire, etc.).
Package win provides a pure-Go Windows implementation of the robotgo API for desktop automation: mouse, keyboard, screen capture, window management and process enumeration.
Package win provides a pure-Go Windows implementation of the robotgo API for desktop automation: mouse, keyboard, screen capture, window management and process enumeration.
Package x11 is a pure-Go (Cgo-free) X11 backend for robotgo.
Package x11 is a pure-Go (Cgo-free) X11 backend for robotgo.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL