Documentation
¶
Overview ¶
Package win provides a pure-Go Windows implementation of the robotgo API for desktop automation: mouse, keyboard, screen capture, window management and process enumeration.
Unlike the upstream go-vgo/robotgo, this package uses NO Cgo. It is built entirely on top of the Win32 API bindings in github.com/tailscale/win and golang.org/x/sys/windows, so it cross-compiles with CGO_ENABLED=0.
The exported API mirrors the Wayland sibling package (github.com/go-vgo/robotgo/wayland) so callers can swap implementations per platform with a build tag.
Index ¶
- Constants
- Variables
- func ActiveName(name string) error
- func ActivePid(pid int, isHandle bool) error
- func Alert(title, msg, ok, cancel string) bool
- func Capture(args ...int) (*image.RGBA, error)
- func CaptureImg(args ...int) (image.Image, error)
- func CheckAccess(prompt bool) bool
- func Click(args ...interface{}) error
- func CloseHID() error
- func CloseWindow(args ...int) error
- func CmdCtrl() string
- func DisplaysNum() int
- func DragSmooth(x, y int, args ...interface{}) error
- func FindIds(name string) ([]int, error)
- func FindName(pid int) (string, error)
- func FindNames() ([]string, error)
- func FindPath(pid int) (string, error)
- func GetActiveApp() (string, string, int)
- func GetBounds(pid int, isHandle bool) (int, int, int, int)
- func GetClient(pid int, isHandle bool) (int, int, int, int)
- func GetHWNDByPid(pid int) int
- func GetMousePos() (int, int)
- func GetPid() int
- func GetPixelColor(x, y int, displayId ...int) string
- func GetScaleSize(displayId ...int) (int, int)
- func GetScreenSize() (int, int)
- func GetTitle(args ...int) string
- func GetVersion() string
- func Height(img image.Image) int
- func InitHID() error
- func InputClick(button string, double bool) error
- func InputKeyToggle(key string, up bool, mods ...string) error
- func InputMove(x, y int) error
- func InputPos() (int, int)
- func InputScroll(x, y int) error
- func InputToggle(button string, up bool) error
- func InputType(str string) error
- func KeyDown(key string, args ...interface{}) error
- func KeyPress(key string, args ...interface{}) error
- func KeyTap(key string, args ...interface{}) error
- func KeyToggle(key string, args ...interface{}) error
- func KeyUp(key string, args ...interface{}) error
- func Kill(pid int) error
- func Location() (int, int)
- func MaxWindow(pid int, args ...interface{}) error
- func MinWindow(pid int, args ...interface{}) error
- func MouseDown(key ...interface{}) error
- func MouseUp(key ...interface{}) error
- func Move(x, y int, displayId ...int) error
- func MoveClick(x, y int, args ...interface{}) error
- func MoveRelative(x, y int) error
- func MoveSmooth(x, y int, args ...interface{}) bool
- func PadHex(hex uint32) string
- func PidExists(pid int) (bool, error)
- func Pids() ([]int, error)
- func PostKeyTap(w WindowInfo, key string, mods ...string) error
- func PostKeyToggle(w WindowInfo, key, direction string, mods ...string) error
- func PostMouse(w WindowInfo, ev MouseEvent) error
- func PostType(w WindowInfo, text string) error
- func Run(path string) ([]byte, error)
- func Save(img image.Image, path string, quality ...int) error
- func SaveCapture(path string, args ...int) error
- func SaveJpeg(img image.Image, path string, quality ...int) error
- func SavePng(img image.Image, path string) error
- func ScaleF(displayId ...int) float64
- func Scroll(x, y int, args ...int) error
- func ScrollDir(x int, direction ...interface{}) error
- func ScrollSmooth(to int, args ...int) error
- func SetBackend(b Backend) error
- func SetHIDLibraryPath(path string)deprecated
- func SetMessageTarget(w WindowInfo)
- func Toggle(key ...interface{}) error
- func Type(str string, args ...int) int
- func TypeDelay(str string, delay int) error
- func TypeStr(str string, args ...int) error
- func Width(img image.Image) int
- type Backend
- type MouseEvent
- type MouseKind
- type Nps
- type Point
- type Rect
- type Size
- type WindowInfo
Constants ¶
const ( 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" Backspace = "backspace" Delete = "delete" Enter = "enter" Tab = "tab" Esc = "esc" Escape = "escape" Up = "up" Down = "down" Right = "right" Left = "left" 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" Shift = "shift" Ctrl = "ctrl" Alt = "alt" Cmd = "cmd" ShiftL = "shiftl" ShiftR = "shiftr" CtrlL = "ctrll" CtrlR = "ctrlr" AltL = "altl" AltR = "altr" Space = "space" Capslock = "capslock" Caps = "caps" Print = "print" Insert = "insert" Menu = "menu" )
Key constants matching robotgo's API.
const Version = "v0.1.0-windows"
Version is the robotgo-win version string.
Variables ¶
var ErrDriverNotInstalled = errors.New("robotgo: interception driver not installed or accessible")
ErrDriverNotInstalled is returned when the Interception driver is missing.
var ErrNotFound = errors.New("robotgo: window not found")
ErrNotFound is returned when no matching window is found.
var ErrNotSupported = errors.New("robotgo: operation not supported")
ErrNotSupported is returned when an operation is not supported.
var NotPid bool
NotPid, when true, makes the pid argument of the keyboard APIs be treated as a window handle (HWND) directly instead of a process id, mirroring the default Cgo backend's robotgo.NotPid behavior on Windows.
Functions ¶
func ActiveName ¶
ActiveName brings the first window whose title contains name (case insensitive) to the foreground.
func ActivePid ¶ added in v1.3.2
ActivePid restores (if minimized) and brings to the foreground the first visible window owned by pid, or the window pid itself when isHandle.
func Alert ¶ added in v1.3.4
Alert shows a modal MessageBox and reports whether OK was chosen; like the Cgo backend it uses the stock OK/Cancel buttons (OK only when cancel is ""), so the ok label is ignored.
func CaptureImg ¶
CaptureImg captures the screen and returns an image.Image. Optional args: x, y, w, h (region). With no args the full primary screen is captured.
func CheckAccess ¶ added in v1.2.0
CheckAccess reports whether input injection is permitted. Windows has no accessibility gate, so it always returns true; prompt is ignored.
func Click ¶
func Click(args ...interface{}) error
Click clicks a mouse button. Default is the left button. Use "left", "right", or "center". Pass true for a double-click.
func CloseHID ¶ added in v1.3.6
func CloseHID() error
CloseHID releases the Interception driver devices.
func CloseWindow ¶
CloseWindow closes a window by posting WM_CLOSE. With no args the foreground window is closed; otherwise the first arg is a pid, and pid <= 0 (e.g. a failed lookup) returns ErrNotFound. A nil error means the request was sent, not that the window has closed.
func CmdCtrl ¶
func CmdCtrl() string
CmdCtrl returns "cmd" on macOS, "ctrl" elsewhere. On Windows: "ctrl".
func DragSmooth ¶
DragSmooth moves the mouse smoothly while holding a button down.
func GetActiveApp ¶ added in v1.2.0
GetActiveApp returns the foreground app's executable name, full executable path and pid. Fields that can not be resolved are left empty.
func GetBounds ¶ added in v1.3.2
GetBounds returns the window rect (x, y, w, h) of pid's window (an HWND when isHandle); pid <= 0 selects the foreground window. It returns zeros when no window is found.
func GetClient ¶ added in v1.3.2
GetClient returns the client area (x, y, w, h) of pid's window in screen coordinates (pid is an HWND when isHandle); pid <= 0 selects the foreground window.
func GetHWNDByPid ¶ added in v1.3.4
GetHWNDByPid returns the first visible top-level window owned by pid, 0 when there is none.
func GetMousePos ¶
GetMousePos returns the current mouse position. Alias of Location.
func GetPixelColor ¶
GetPixelColor returns the pixel color at (x, y) as a 6-digit hex string.
func GetScaleSize ¶
GetScaleSize returns the screen size multiplied by the DPI scale factor, matching robotgo.GetScaleSize on the Cgo backend.
func GetScreenSize ¶
GetScreenSize returns the primary display's width and height in pixels.
func GetTitle ¶
GetTitle returns the title of the foreground window. Arguments are accepted for API parity but ignored.
func InitHID ¶ added in v1.3.6
func InitHID() error
InitHID opens the Interception driver devices. It is called by SetBackend(BackendHID) and lazily by HID input.
func InputClick ¶ added in v1.3.6
InputClick clicks button through the current Backend.
func InputKeyToggle ¶ added in v1.3.6
InputKeyToggle presses (up false) or releases key through the current Backend. Modifiers are pressed before the key and released after it; the modifiers the key itself implies (shift for "A" or "@") are added like KeyTap and KeyToggle do.
func InputMove ¶ added in v1.3.6
InputMove moves the pointer to screen point (x, y) through the current Backend.
func InputPos ¶ added in v1.3.6
InputPos returns the pointer position the current Backend moves from: the virtual pointer under BackendMessage, otherwise the real cursor.
func InputScroll ¶ added in v1.3.6
InputScroll scrolls by wheel notches through the current Backend.
func InputToggle ¶ added in v1.3.6
InputToggle presses or releases button through the current Backend.
func KeyDown ¶
KeyDown presses a key down. Extra args (e.g. modifiers) are forwarded to KeyToggle for API parity with the default robotgo backend.
func KeyTap ¶
KeyTap taps a key (press + release). Optional trailing modifiers (strings or a []string) and an int pid: when a pid is supplied the key (and its modifiers) is posted to that process's window via PostMessageW, mirroring the Windows path in key/keypress_c.h; otherwise it is injected into the focused window via SendInput. Set NotPid to pass an HWND instead of a pid.
KeyTap("a")
KeyTap("a", "ctrl")
KeyTap("a", "ctrl", "shift")
KeyTap("a", []string{"ctrl", "shift"})
KeyTap("a", pid)
KeyTap("a", pid, "ctrl")
func KeyToggle ¶
KeyToggle toggles a key. Default is "down"; pass "up" to release. Modifiers (strings or a []string) are pressed before the key and released in reverse order after it. An optional int pid posts the events to that process's window via PostMessageW, mirroring key/keypress_c.h; otherwise SendInput is used. Set NotPid to pass an HWND.
KeyToggle("a")
KeyToggle("a", "up")
KeyToggle("a", "down", []string{"ctrl", "shift"})
KeyToggle("a", "up", []string{"ctrl", "shift"})
KeyToggle("a", pid)
func KeyUp ¶
KeyUp releases a key. Extra args (e.g. modifiers) are forwarded to KeyToggle for API parity with the default robotgo backend.
func MaxWindow ¶
MaxWindow maximizes (or restores, if the bool arg is false) pid's window; pid <= 0 returns ErrNotFound instead of acting on the foreground window.
func MinWindow ¶
MinWindow minimizes (or restores, if the bool arg is false) pid's window; pid <= 0 returns ErrNotFound instead of acting on the foreground window.
func MouseDown ¶
func MouseDown(key ...interface{}) error
MouseDown sends a mouse button down event.
func Move ¶
Move moves the mouse to absolute position (x, y) through the current Backend. The optional displayId is accepted for API parity but ignored on Windows, where coordinates are in the unified virtual-desktop space.
func MoveRelative ¶
MoveRelative moves the mouse relative to its current position.
func MoveSmooth ¶
MoveSmooth moves the mouse smoothly to (x, y) with an ease-in-out curve. Optional args: steps (int), sleepMs (int). Returns true on success.
func PostKeyTap ¶ added in v1.3.6
func PostKeyTap(w WindowInfo, key string, mods ...string) error
PostKeyTap taps key (with optional modifiers) in window w via WM_KEYDOWN/WM_KEYUP messages.
func PostKeyToggle ¶ added in v1.3.6
func PostKeyToggle(w WindowInfo, key, direction string, mods ...string) error
PostKeyToggle presses ("down") or releases ("up") key in window w. Modifiers are pressed before the key and released after it. Alt and Ctrl stay held in w until their "up", whichever of its controls has the focus. While Alt is held without Ctrl, keys posted in between are Alt chords (WM_SYSKEYDOWN) and that Alt releases as WM_KEYUP, while Alt released with no key in between is a lone Alt (WM_SYSKEYUP). An "up" for keys that were not pressed in w is posted as if they had just been pressed together.
func PostMouse ¶ added in v1.3.6
func PostMouse(w WindowInfo, ev MouseEvent) error
PostMouse posts a mouse message to the deepest child of window w under the event point. The real cursor does not move and the window is not activated.
func PostType ¶ added in v1.3.6
func PostType(w WindowInfo, text string) error
PostType types text into window w as WM_CHAR messages.
func SaveCapture ¶
SaveCapture captures the screen and saves it to a file.
func ScaleF ¶
ScaleF returns the system DPI scale factor (LOGPIXELSX / 96), mirroring the Cgo backend's sys_scale on Windows.
func Scroll ¶
Scroll scrolls the mouse by wheel notches. Positive y scrolls up, negative scrolls down; positive x scrolls left, negative scrolls right (matching robotgo's Cgo backend convention). Optional arg: delay ms.
func ScrollSmooth ¶
ScrollSmooth scrolls the mouse smoothly by `to` steps, repeating `num` times (default 5) with `tm` ms between steps (default 100). An optional third arg sets the horizontal offset per step.
func SetBackend ¶ added in v1.3.6
SetBackend selects the input backend. BackendHID initializes the Interception driver immediately and fails if it is unavailable.
func SetHIDLibraryPath
deprecated
added in
v1.3.6
func SetHIDLibraryPath(path string)
SetHIDLibraryPath is a no-op kept for compatibility.
Deprecated: the HID backend talks to the Interception driver directly and no longer loads interception.dll.
func SetMessageTarget ¶ added in v1.3.6
func SetMessageTarget(w WindowInfo)
SetMessageTarget fixes the window BackendMessage posts to. A zero WindowInfo restores automatic targeting.
func Toggle ¶
func Toggle(key ...interface{}) error
Toggle toggles a mouse button down or up.
Toggle("left") // press down
Toggle("left", "up") // release
func Type ¶
Type types a string using Unicode key events, supporting any character (including those not present on the current keyboard layout). An optional first int argument is the target pid: when non-zero each character is posted to that process's window as a WM_CHAR message, mirroring unicodeType() in key/keypress_c.h; otherwise it is injected into the focused window via SendInput. Set NotPid to pass an HWND instead of a pid.
Type("hello")
Type("hello", pid)
It returns the number of characters (runes) typed, stopping at the first failed input event; 0 if the target window is not found.
Types ¶
type Backend ¶ added in v1.3.6
type Backend int
Backend selects how the global mouse and keyboard API injects input.
const ( // BackendSendInput injects input with SendInput/SetCursorPos (default). BackendSendInput Backend = iota // BackendHID injects hardware-level strokes through the Interception // driver. Characters missing from the keyboard layout fall back to // SendInput Unicode events in Type. BackendHID // BackendMessage posts window messages (PostMessageW) without moving the // real cursor or activating a window. Mouse events go to the message // target, or the window under the event point when no target is set, and // use a virtual pointer started at the real cursor position. Key events // go to the target, or the foreground window. BackendMessage )
func GetBackend ¶ added in v1.3.6
func GetBackend() Backend
GetBackend returns the current input backend.
type MouseEvent ¶ added in v1.3.6
type MouseEvent struct {
Kind MouseKind
Button string // "left" (default), "right", "center"
X, Y int
Clicks int // click state: 1 single, 2 posts a double-click message
// DX, DY are scroll notches; positive DY scrolls up, positive DX scrolls
// left, matching Scroll.
DX, DY int
}
MouseEvent is one standalone mouse event at screen point (X, Y).
type MouseKind ¶ added in v1.3.6
type MouseKind int
MouseKind is the type of a standalone mouse event.
type Rect ¶
Rect represents a rectangle.
func GetScreenRect ¶
GetScreenRect returns the primary display rectangle, or the whole virtual desktop when a non-zero displayId is given on a multi-monitor system (the same rule as the Cgo backend's getScreenRect).
type WindowInfo ¶ added in v1.3.6
type WindowInfo struct {
ID uint64 `json:"id"` // HWND
Pid int `json:"pid"` // owning process
Owner string `json:"owner"` // executable name without ".exe"
X int `json:"x"`
Y int `json:"y"`
W int `json:"width"`
H int `json:"height"`
}
WindowInfo describes an on-screen top-level app window in screen coordinates.
func ListWindows ¶ added in v1.3.6
func ListWindows() ([]WindowInfo, error)
ListWindows returns visible, non-minimized top-level windows in z-order, front-most first. Windows of the calling process are excluded.
func WindowAt ¶ added in v1.3.6
func WindowAt(x, y int) (WindowInfo, error)
WindowAt returns the front-most app window containing screen point (x, y).
func (WindowInfo) Contains ¶ added in v1.3.6
func (w WindowInfo) Contains(x, y int) bool
Contains reports whether screen point (x, y) lies inside the window.