robotgo

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

README

Robotgo-Without-Bitmap

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

Cloned https://github.com/go-vgo/robotgo, and removed bitmap section. Because of need to use robotgo mouse control, but bitmap section's dependency "zlib" keep bother me.. So I removed it.

RobotGo supports Mac, Windows, and Linux(X11).

Chinese Simplified

Contents

Docs

Binding:

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 Mac OS X:
Xcode Command Line Tools
brew install libpng libxcb libxkbcommon
For Windows:
MinGW-w64 (Use recommended) or other GCC
For everything else:
GCC, libpng

X11 with the XTest extension (also known as the Xtst library)

Event:

xcb, xkb, libxkbcommon
Ubuntu:
sudo apt install gcc libc6-dev

sudo apt install libx11-dev xorg-dev libxtst-dev libpng++-dev

sudo apt install xcb libxcb-xkb-dev x11-xkb-utils libx11-xcb-dev libxkbcommon-x11-dev
sudo apt install libxkbcommon-dev

sudo apt install xsel xclip
Fedora:
sudo dnf install libxkbcommon-devel libXtst-devel libxkbcommon-x11-devel xorg-x11-xkb-utils-devel

sudo dnf install libpng-devel

sudo dnf install xsel xclip

Installation:

go get github.com/go-vgo/robotgo

It's that easy!

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

func main() {
  robotgo.ScrollMouse(10, "up")
  robotgo.MouseClick("left", true)
  robotgo.MoveMouseSmooth(100, 200, 1.0, 100.0)
}
Keyboard
package main

import (
  "fmt"

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

func main() {
  robotgo.TypeStr("Hello World")
  robotgo.TypeStr("だんしゃり", 1.0)
  // robotgo.TypeString("テストする")

  robotgo.TypeStr("Hi galaxy. こんにちは世界.")
  robotgo.Sleep(1)

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

  robotgo.KeyTap("enter")
  // robotgo.TypeString("en")
  robotgo.KeyTap("i", "alt", "command")

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

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

import (
	"fmt"

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

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

  color := robotgo.GetPixelColor(100, 200)
  fmt.Println("color---- ", color)
}
Bitmap
/*package main

import (
	"fmt"

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

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

  fmt.Println("...", bitmap)

  fx, fy := robotgo.FindBitmap(bitmap)
  fmt.Println("FindBitmap------ ", fx, fy)

  robotgo.SaveBitmap(bitmap, "test.png")
}*/
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 ---")
  robotgo.EventHook(hook.KeyDown, []string{"q", "ctrl", "shift"}, func(e hook.Event) {
    fmt.Println("ctrl-shift-q")
    robotgo.EventEnd()
  })

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

  s := robotgo.EventStart()
  <-robotgo.EventProcess(s)
}

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

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

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

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

  mleft := robotgo.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.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.ShowAlert("test", "robotgo")
  if abool {
 	  fmt.Println("ok@@@ ", "ok")
  }

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

CrossCompiling

Windows64 to win32
SET CGO_ENABLED=1
SET GOARCH=386
go build main.go
Other to windows
GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ go build -x ./
// CC=mingw-w64\x86_64-7.2.0-win32-seh-rt_v5-rev1\mingw64\bin\gcc.exe
// CXX=mingw-w64\x86_64-7.2.0-win32-seh-rt_v5-rev1\mingw64\bin\g++.exe

Some discussions and questions, please see issues/228, issues/143.

Authors

Plans

  • Update Find an image on screen, read pixels from an image
  • Update Window Handle
  • Try support Android, maybe support IOS

Contributors

License

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

See LICENSE-APACHE, LICENSE-MIT.

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:

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

Index

Constants

View Source
const (
	// Version get the robotgo version
	Version = "v0.91.0.1089, MT. Rainier!"
)

Variables

View Source
var Keycode = uMap{
	"`": 41,
	"1": 2,
	"2": 3,
	"3": 4,
	"4": 5,
	"5": 6,
	"6": 7,
	"7": 8,
	"8": 9,
	"9": 10,
	"0": 11,
	"-": 12,
	"+": 13,

	"q":  16,
	"w":  17,
	"e":  18,
	"r":  19,
	"t":  20,
	"y":  21,
	"u":  22,
	"i":  23,
	"o":  24,
	"p":  25,
	"[":  26,
	"]":  27,
	"\\": 43,

	"a": 30,
	"s": 31,
	"d": 32,
	"f": 33,
	"g": 34,
	"h": 35,
	"j": 36,
	"k": 37,
	"l": 38,
	";": 39,
	"'": 40,

	"z": 44,
	"x": 45,
	"c": 46,
	"v": 47,
	"b": 48,
	"n": 49,
	"m": 50,
	",": 51,
	".": 52,
	"/": 53,

	"f1":  59,
	"f2":  60,
	"f3":  61,
	"f4":  62,
	"f5":  63,
	"f6":  64,
	"f7":  65,
	"f8":  66,
	"f9":  67,
	"f10": 68,
	"f11": 69,
	"f12": 70,

	"esc":     1,
	"delete":  14,
	"tab":     15,
	"ctrl":    29,
	"control": 29,
	"alt":     56,
	"space":   57,
	"shift":   42,
	"rshift":  54,
	"enter":   28,

	"cmd":     3675,
	"command": 3675,
	"rcmd":    3676,
	"ralt":    3640,

	"up":    57416,
	"down":  57424,
	"left":  57419,
	"right": 57421,
}

Keycode robotgo hook key's code map

View Source
var MouseMap = uMap{
	"left":       1,
	"right":      2,
	"center":     3,
	"wheelDown":  4,
	"wheelUp":    5,
	"wheelLeft":  6,
	"wheelRight": 7,
}

MouseMap robotgo hook mouse's code map

Functions

func ActiveName

func ActiveName(name string) error

ActiveName active window by name

func ActivePID

func ActivePID(pid int32, 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

func ActivePIDC(pid int32, args ...int) error

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

func AddEvent

func AddEvent(key string) bool

AddEvent add event listener,

parameters for the string type, the keyboard corresponding key parameters,

mouse arguments: mleft, center, mright, wheelDown, wheelUp, wheelLeft, wheelRight.

Use "robotgo.AddEvents()" or "gohook" add asynchronous event listener

func AddEvents

func AddEvents(key string, arr ...string) bool

AddEvents add global event hook

robotgo.AddEvents("q") robotgo.AddEvents("q", "ctrl") robotgo.AddEvents("q", "ctrl", "shift")

func AddMouse

func AddMouse(btn string, x ...int16) bool

AddMouse add mouse event hook

mouse arguments: left, center, right, wheelDown, wheelUp, wheelLeft, wheelRight.

robotgo.AddMouse("left") robotgo.AddMouse("left", 100, 100)

func AddMousePos

func AddMousePos(x, y int16) bool

AddMousePos add listen mouse event pos hook

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

robotgo.Click(button string, double bool)

func CloseWindow

func CloseWindow(args ...int32)

CloseWindow close the window

func Drag

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

Drag drag the mouse

func DragMouse

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

DragMouse drag the mouse

func DragSmooth

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

DragSmooth drag the mouse smooth

func End

func End()

End removes global event hook

func EventEnd

func EventEnd()

EventEnd removes global event hook

func EventHook

func EventHook(when uint8, keysPressed []string, callback func(hook.Event))

EventHook register gohook event

func EventProcess

func EventProcess(events chan hook.Event) chan bool

EventProcess return go hook process

func EventStart

func EventStart() chan hook.Event

EventStart start global event hook return event channel

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 GetActive

func GetActive() C.MData

GetActive get the active window

func GetBHandle

func GetBHandle() int

GetBHandle get the window handle, Wno-deprecated

func GetBounds

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

GetBounds get the window bounds

func GetHandPid

func GetHandPid(pid int32, args ...int32) C.MData

GetHandPid get handle mdata by pid

func GetHandle

func GetHandle() int

GetHandle get the window handle

func GetMousePos

func GetMousePos() (int, int)

GetMousePos get mouse's portion

func GetPID

func GetPID() int32

GetPID get the process id

func GetTitle

func GetTitle(args ...int32) string

GetTitle get the window title

func GetVersion

func GetVersion() string

GetVersion get the robotgo version

func GetXId

func GetXId(xu *xgbutil.XUtil, pid int32) (xproto.Window, error)

GetXId get the xid return window and error

func GetXidFromPid

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

GetXidFromPid get the xide from pid

func GoString

func GoString(char *C.char) string

GoString teans C.char to string

func Is64Bit

func Is64Bit() bool

Is64Bit determine whether the sys is 64bit

func IsValid

func IsValid() bool

IsValid valid the window

func KeyTap

func KeyTap(tapKey string, args ...interface{}) string

KeyTap tap the keyboard code;

See keys:

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

func KeyToggle

func KeyToggle(key string, args ...string) string

KeyToggle toggle the keyboard

See keys:

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

func Kill

func Kill(pid int) error

Kill kill the process by PID

func MaxWindow

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

MaxWindow set the window max

func MicroSleep

func MicroSleep(tm float64)

MicroSleep time C.microsleep(tm)

func MilliSleep

func MilliSleep(tm int)

MilliSleep sleep tm milli second

func MinWindow

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

MinWindow set the window min

func MouseClick

func MouseClick(args ...interface{})

MouseClick click the mouse

robotgo.MouseClick(button string, double bool)

func MouseToggle

func MouseToggle(togKey string, args ...interface{}) int

MouseToggle toggle the mouse

func Move

func Move(x, y int)

Move move the mouse

func MoveArgs

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

MoveArgs move mose 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)

func MoveMouse

func MoveMouse(x, y int)

MoveMouse move the mouse

func MoveMouseSmooth

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

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

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)

func MoveSmoothRelative

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

MoveSmoothRelative move mose smooth relative

func MovesClick

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

MovesClick move smooth and click the mouse

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 Scroll

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

Scroll scroll the mouse with x, y

robotgo.Scroll(x, y, msDelay int)

func ScrollMouse

func ScrollMouse(x int, direction string)

ScrollMouse scroll the mouse

func SetActive

func SetActive(win C.MData)

SetActive set the window active

func SetDelay

func SetDelay(d ...int)

SetDelay set the key and mouse delay

func SetHandle

func SetHandle(hwnd int)

SetHandle set the window handle

func SetHandlePid

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

SetHandlePid set the window handle by pid

func SetKeyDelay

func SetKeyDelay(delay int)

SetKeyDelay set keyboard delay

func SetKeyboardDelay

func SetKeyboardDelay(delay int)

SetKeyboardDelay set keyboard delay, Wno-deprecated, this function will be removed in version v1.0.0

func SetMouseDelay

func SetMouseDelay(delay int)

SetMouseDelay set mouse delay

func ShowAlert

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

ShowAlert show a alert window

func Sleep

func Sleep(tm int)

Sleep time.Sleep tm second

func Start

func Start() chan hook.Event

Start start global event hook return event channel

func StopEvent

func StopEvent()

StopEvent stop event listener

func Try

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

Try handler(err)

func TypeStr

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

TypeStr send a string, support UTF-8

robotgo.TypeStr(string: The string to send, float64: microsleep time, x11)

func TypeStrDelay

func TypeStrDelay(str string, delay int)

TypeStrDelay type string delayed

func TypeString

func TypeString(str string, delay ...int)

TypeString send a string, support unicode TypeStr(string: The string to send), Wno-deprecated

func TypeStringDelayed

func TypeStringDelayed(str string, delay int)

TypeStringDelayed type string delayed, Wno-deprecated

func UnicodeType

func UnicodeType(str uint32)

UnicodeType tap uint32 unicode

Types

type CHex

type CHex C.MMRGBHex

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

type MPoint

type MPoint struct {
	X int
	Y int
}

MPoint is MPoint struct

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

Directories

Path Synopsis
key

Jump to

Keyboard shortcuts

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