robotgo

package module
v0.0.0-...-aa0f17c Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: Apache-2.0 Imports: 28 Imported by: 0

README

Robotgo

Build Status CircleCI Status Build Status Appveyor Go Report Card GoDoc GitHub release

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

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

Contents

Docs

Binding:

ADB, packaging android adb API.

Robotn, binding JavaScript and other, support more language.

Requirements:

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

ALL:
Golang

GCC
For MacOS:
brew install go

Xcode Command Line Tools (And Privacy setting: #277)

xcode-select --install
For Windows:
winget install Golang.go
For Linux:

Wayland screen capture uses wlr-screencopy first (DMA-BUF or wl_shm). DMA-BUF requires the compositor to expose zwp_linux_dmabuf_v1 version 4+. If Wayland screencopy is unavailable/fails, RobotGo falls back to the freedesktop portal screenshot path.

Portal fallback requires xdg-desktop-portal and a desktop backend (for example xdg-desktop-portal-gnome or xdg-desktop-portal-kde). You can force portal capture with ROBOTGO_FORCE_PORTAL=1. You can also override backend selection with ROBOTGO_WAYLAND_BACKEND: auto, dmabuf, wl_shm, or portal. Set ROBOTGO_CAPTURE_DEBUG=1 to log capture backend decisions.

See docs/wayland-tasks.md for the current Wayland support status and remaining tasks.

bit, err := robotgo.CaptureScreen(0, 0, 100, 100)
if err != nil {
  log.Fatal(err)
}
defer robotgo.FreeBitmap(bit)
winget install MartinStorsjo.LLVM-MinGW.UCRT

Or MinGW-w64 (Use recommended) or others Mingw llvm-mingw;

Download the Mingw, then set system environment variables C:\mingw64\bin to the Path. Set environment variables to run GCC from command line.

Or the other GCC (But you should compile the "libpng" with yourself when use the bitmap.)

For everything else:
GCC

X11 with the XTest extension (the Xtst library)

"Clipboard": xsel xclip


"Bitmap": libpng (Just used by the "bitmap".)

"Event-Gohook": xcb, xkb, libxkbcommon (Just used by the "hook".)

Ubuntu:
# sudo apt install 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

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 xorg-x11-xkb-utils-devel

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 github.com/go-vgo/robotgo

png.h: No such file or directory? Please see 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.TypeStr("Hello World")
  robotgo.TypeStr("だんしゃり", 0, 1)
  // robotgo.TypeStr("テストする")

  robotgo.TypeStr("Hi, Seattle space needle, Golden gate bridge, One world trade center.")
  robotgo.TypeStr("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.TypeStr("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.TypeStr("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

  • Refactor some C code to Go (such as x11, windows)
  • Better multiscreen support
  • Wayland support including DMA-BUF screencopy
  • 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/marang/robotgo#requirements

Installation:

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

import "github.com/marang/robotgo"

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

go get -u github.com/marang/robotgo

+bulid windows,next

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"

	// 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"

	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
	Lcmd = "lcmd" // left command
	Rcmd = "rcmd" // right command
	// "command"
	Alt     = "alt"
	Lalt    = "lalt" // left alt
	Ralt    = "ralt" // right alt
	Ctrl    = "ctrl"
	Lctrl   = "lctrl" // left ctrl
	Rctrl   = "rctrl" // right ctrl
	Control = "control"
	Shift   = "shift"
	Lshift  = "lshift" // left shift
	Rshift  = "rshift" // right shift
	// "right_shift"
	Capslock    = "capslock"
	Space       = "space"
	Print       = "print"
	Printscreen = "printscreen" // No Mac support
	Insert      = "insert"
	Menu        = "menu" // Windows only

	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 = "v1.00.0.1189, 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 (
	ErrWaylandDisplay  = errors.New("wayland connect failed")
	ErrNoScreencopy    = errors.New("screencopy manager not available")
	ErrNoOutputs       = errors.New("no outputs")
	ErrDmabufDevice    = errors.New("screencopy dmabuf device unsupported")
	ErrDmabufModifiers = errors.New("screencopy dmabuf modifiers unsupported")
	ErrDmabufImport    = errors.New("screencopy dmabuf import failed")
	ErrDmabufMap       = errors.New("screencopy dmabuf map failed")
	ErrWaylandFailed   = errors.New("wayland capture failed")
	ErrPortalFailed    = errors.New("portal capture failed")
	ErrNotSupported    = errors.New("operation not supported on current platform/backend")
)
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

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

ActivePid activates the window by PID via X11.

func ActivePidC

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

ActivePidC activates the window by PID via X11.

func Alert

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

Alert shows a simple alert dialog with optional custom button labels. On Linux it prefers native Wayland-friendly tools when available. Order of backends: zenity, kdialog, xmessage, notify-send (fire-and-forget). If cancel button is not given, only the default button is displayed.

Examples:

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

func ByteToImg

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

ByteToImg convert []byte to image.Image

func Capture

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

Capture capture the screenshot, use the CaptureImg default

func CaptureImg

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 CheckMouse

func CheckMouse(btn string) C.MMMouseButton

CheckMouse check the mouse button

func Click

func Click(args ...interface{})

Click 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

func CloseMainDisplay()

CloseMainDisplay close the main X11 display

func CloseWindow

func CloseWindow(args ...int)

CloseWindow close the window

func CloseWindowE

func CloseWindowE(args ...int) error

CloseWindowE closes the target window and returns an explicit unsupported error on Wayland sessions.

func CloseWindowKill

func CloseWindowKill(args ...int) error

CloseWindowKill closes the target window and ensures the owning process terminates. If no arguments are provided, it targets the currently selected window (same as CloseWindow()). If a PID (or handle when NotPid is set) is provided, it targets that window. After issuing a normal close, it waits a short time for graceful shutdown and, if the process is still alive, it will force-kill it.

Usage:

CloseWindowKill()           // close current window and kill if needed
CloseWindowKill(pid)        // close by pid and kill if needed
CloseWindowKill(pid, 1)     // on Windows, treat first arg as handle

func CmdCtrl

func CmdCtrl() string

CmdCtrl returns "cmd" on macOS and "ctrl" on other platforms.

func CurrentSpecialTable

func CurrentSpecialTable() map[string]string

CurrentSpecialTable returns the special key map for the active display server. DetectDisplayServer determines whether the Wayland or X11 table should be returned.

func DecodeImg

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

DecodeImg decode the image to image.Image and return

func DisplaysNum

func DisplaysNum() int

DisplaysNum returns the count of displays using Xinerama.

func Drag deprecated

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

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{})

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

func FreeBitmapArr(bit ...CBitmap)

FreeBitmapArr free and dealloc the C bitmap array

func GetActiveC

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 returns the window bounds using X11.

func GetClient

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

GetClient returns the client bounds using X11.

func GetDisplayBounds

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

GetDisplayBounds gets the display screen bounds

func GetHWNDByPid

func GetHWNDByPid(pid int) int

GetHWNDByPid get the hwnd by pid

func GetHandByPidC

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

func GetLocationColor(displayId ...int) (string, error)

GetLocationColor gets the color of the current mouse location.

func GetMainId

func GetMainId() int

GetMainId returns the primary 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

func GetPid() int

GetPid get the process id return int32

func GetPixelColor

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

GetPixelColor returns the pixel color as a hex string.

func GetPxColor

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

GetPxColor returns the pixel color at (x,y). On Wayland this function falls back to capturing a 1x1 region using the Wayland backend. An error is returned if capturing fails or no suitable backend is available.

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 GetTitleE

func GetTitleE(args ...int) (string, error)

GetTitleE gets the window title and returns an explicit unsupported error on Wayland sessions.

func GetVersion

func GetVersion() string

GetVersion get the robotgo version

func GetXDisplayName

func GetXDisplayName() string

GetXDisplayName get XDisplay name (Linux)

func GetXid

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

GetXid gets the XID for a given PID.

func GetXidFromPid

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

GetXidFromPid returns the XID for the given PID.

func GoString

func GoString(char *C.char) string

GoString trans C.char to string

func Height

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

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

func IsMain(displayId int) bool

IsMain is main display

func IsValid

func IsValid() bool

IsValid valid the window

func KeyDown

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

KeyDown press down a key

func KeyPress

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/marang/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/marang/robotgo/blob/master/docs/keys.md#keys

Examples:

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

robotgo.KeyToggle("a", "up", "alt", "cmd")
robotgo.KeyToggle("k", pid int)

func KeyUp

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

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 MaxWindowE

func MaxWindowE(pid int, args ...interface{}) error

MaxWindowE sets the window max state and returns an explicit unsupported error on Wayland sessions.

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 MinWindowE

func MinWindowE(pid int, args ...interface{}) error

MinWindowE sets the window min state and returns an explicit unsupported error on Wayland sessions.

func MouseClick deprecated

func MouseClick(args ...interface{})

Deprecated: use the Click(),

MouseClick click the mouse

robotgo.MouseClick(button string, double bool)

func MouseDown

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

MouseDown send mouse down event

func MouseUp

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

MouseUp send mouse up event

func Move

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

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{})

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)

MoveRelative move mouse with relative

func MoveScale

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{})

MoveSmoothRelative move mouse smooth with relative

func MovesClick

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

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 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

func PadHexs(hex CHex) string

PadHexs trans CHex to string

func PasteStr

func PasteStr(str string) error

PasteStr paste a string (support UTF-8), write the string to clipboard and tap `cmd + v`

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

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

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

Run run a cmd shell

func Save

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 SaveImg

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

SaveImg save the image by []byte

func SaveJpeg

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

SaveJpeg save the image by image.Image

func SavePng

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

SavePng save the image by image.Image

func Scale0 deprecated

func Scale0() int

Deprecated: use the ScaledF(),

Scale0 return ScaleX() / 0.96, drop

func Scale1 deprecated

func Scale1() int

Deprecated: use the ScaledF(),

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

func ScaleF

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

func Scaled0(x int, f float64) int

Scaled0 return int(x * f)

func Scaled1

func Scaled1(x int, f float64) int

Scaled1 return int(x / f)

func Scroll

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

Scroll scroll the mouse to (x, y)

robotgo.Scroll(x, y, msDelay int)

Examples:

robotgo.Scroll(10, 10)

func ScrollDir

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

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

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

ScrollRelative scroll mouse with relative

Examples:

robotgo.ScrollRelative(10, 10)

func ScrollSmooth

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

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

func SetActiveC(win C.MData)

SetActiveC set the window active

func SetActiveE

func SetActiveE(win Handle) error

SetActiveE sets the active window and returns an explicit unsupported error for Wayland sessions where global window activation is not available.

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 SetWaylandBackend

func SetWaylandBackend(b WaylandBackend)

SetWaylandBackend allows tests and callers to force a specific Wayland capture backend.

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

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

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

ToByteImg convert image.Image to []byte

func ToImage

func ToImage(bit CBitmap) image.Image

ToImage convert C.MMBitmapRef to standard image.Image

func ToInterfaces

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

func ToRGBA(bit CBitmap) *image.RGBA

ToRGBA convert C.MMBitmapRef to standard image.RGBA

func ToRGBAGo

func ToRGBAGo(bmp1 Bitmap) *image.RGBA

ToRGBAGo convert Bitmap to standard image.RGBA

func ToStringImg

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

ToStringImg convert image.Image to string

func ToStrings

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

ToStrings convert []interface{} to []string

func ToUC

func ToUC(text string) []string

ToUC trans string to unicode []string

func ToUint8p

func ToUint8p(dst []uint8) ([]uint8, *uint8)

ToUint8p convert the []uint8 to a uint8 pointer and backing slice

func Toggle

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 TypeStr

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

TypeStr send a string (supported UTF-8)

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

Examples:

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

func TypeStrDelay

func TypeStrDelay(str string, delay int)

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

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)

UnicodeType tap the uint32 unicode

func Width

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
	// contains filtered or unexported fields
}

Bitmap define the go Bitmap struct

The common type conversion of bitmap:

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

func CaptureGo

func CaptureGo(args ...int) (Bitmap, error)

CaptureGo capture the screen and return a Go bitmap.

func ImgToBitmap

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

ImgToBitmap convert the standard image.Image to Bitmap

func RGBAToBitmap

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

type CBitmap

type CBitmap = C.MMBitmapRef

CBitmap define CBitmap as C.MMBitmapRef type

func ByteToCBitmap

func ByteToCBitmap(by []byte) CBitmap

ByteToCBitmap trans []byte to CBitmap

func CaptureScreen

func CaptureScreen(args ...int) (CBitmap, error)

CaptureScreen capture the screen and return a bitmap (C struct). Use `defer robotgo.FreeBitmap(bitmap)` to free the bitmap.

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

func ImgToCBitmap

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 CaptureBackend

type CaptureBackend string

CaptureBackend reports which backend handled the most recent screen capture.

const (
	BackendNone       CaptureBackend = ""
	BackendScreencopy CaptureBackend = "screencopy"
	BackendPortal     CaptureBackend = "portal"
	BackendX11        CaptureBackend = "x11"
)

func LastBackend

func LastBackend() CaptureBackend

LastBackend returns the backend used for the last CaptureScreen call.

type DisplayServer

type DisplayServer string

DisplayServer identifies the active Linux display server.

const (
	// DisplayServerX11 represents an X11 display server.
	DisplayServerX11 DisplayServer = "x11"
	// DisplayServerWayland represents a Wayland display server.
	DisplayServerWayland DisplayServer = "wayland"
	// DisplayServerUnknown indicates no known display server was detected.
	DisplayServerUnknown DisplayServer = "unknown"
)

func DetectDisplayServer

func DetectDisplayServer() DisplayServer

DetectDisplayServer inspects the environment and reports the active display server. It checks the standard DISPLAY and WAYLAND_DISPLAY variables. If neither is present, DisplayServerUnknown is returned.

type Handle

type Handle C.MData

Handle define window Handle as C.MData type

func GetActive

func GetActive() Handle

GetActive get the active window

func GetHandById

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

GetHandById get handle mdata by id

func GetHandByPid

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

type Point struct {
	X int
	Y int
}

Point is point struct

type Rect

type Rect struct {
	Point
	Size
}

Rect is rect structure

func GetDisplayRect

func GetDisplayRect(i int) Rect

GetDisplayRect gets the display rect

func GetScreenRect

func GetScreenRect(displayId ...int) Rect

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

type Size

type Size struct {
	W, H int
}

Size is size structure

type WaylandBackend

type WaylandBackend int

WaylandBackend selects which Wayland backend to use at runtime.

const (
	WaylandBackendAuto WaylandBackend = iota
	WaylandBackendDmabuf
	WaylandBackendWlShm
)

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
key command
mouse command
scale command
screen command
screen_full command
window command

Jump to

Keyboard shortcuts

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