Package winput provides a high-level interface for Windows background input automation.
- Variables
- Constants
- func EnablePerMonitorDPI
- func GetCursorPos
- func SetBackend
- func SetHIDLibraryPath
- func MoveMouseTo
- func ClickMouseAt
- func KeyDown
- func KeyUp
- func Press
- func PressHotkey
- func Type
- func CaptureVirtualDesktop
- func CaptureRegion
- type Backend
- type Key
- type Window
- func FindByClass
- func FindByPID
- func FindByProcessName
- func FindByTitle
- func (*Window) Click
- func (*Window) ClickMiddle
- func (*Window) ClickRight
- func (*Window) ClientRect
- func (*Window) ClientToScreen
- func (*Window) DPI
- func (*Window) DoubleClick
- func (*Window) KeyDown
- func (*Window) KeyUp
- func (*Window) Move
- func (*Window) MoveRel
- func (*Window) Press
- func (*Window) PressHotkey
- func (*Window) ScreenToClient
- func (*Window) Scroll
- func (*Window) Type
var (
// ErrWindowNotFound implies the target window could not be located by Title, Class, or PID.
ErrWindowNotFound = errors.New("window not found")
// ErrWindowGone implies the window handle is no longer valid.
ErrWindowGone = errors.New("window is gone or invalid")
// ErrWindowNotVisible implies the window is hidden or minimized.
ErrWindowNotVisible = errors.New("window is not visible")
// ErrUnsupportedKey implies the character cannot be mapped to a key.
ErrUnsupportedKey = errors.New("unsupported key or character")
// ErrBackendUnavailable implies the selected backend (e.g. HID) failed to initialize.
ErrBackendUnavailable = errors.New("input backend unavailable")
// ErrDriverNotInstalled specific to BackendHID, implies the Interception driver is missing or not accessible.
ErrDriverNotInstalled = errors.New("interception driver not installed or accessible")
// ErrDLLLoadFailed implies interception.dll could not be loaded.
ErrDLLLoadFailed = errors.New("failed to load interception library")
// ErrPermissionDenied implies the operation failed due to system privilege restrictions (e.g. UIPI).
ErrPermissionDenied = errors.New("permission denied")
// ErrPostMessageFailed implies the PostMessageW call returned 0 (e.g., queue full or invalid handle).
ErrPostMessageFailed = errors.New("PostMessageW failed")
)func ImageToVirtual(imageX, imageY int32) (int32, int32)ImageToVirtual converts coordinates from a "Full Virtual Desktop Screenshot" (OpenCV match) to actual Windows Virtual Desktop coordinates usable by MoveMouseTo.
Requires the image to be a capture of the entire virtual desktop (origin 0,0 of the image corresponds to the top-left of the virtual canvas).
func VirtualBounds() RectVirtualBounds returns the bounding rectangle of the entire virtual desktop.
func Monitors() ([]Monitor, error)Monitors returns a list of all active monitors and their geometries.
const (
// BackendMessage uses standard Windows Messages (PostMessage) for input.
BackendMessage Backend = iota
// BackendHID uses the Interception driver to simulate hardware input.
BackendHID
)Common keyboard scan codes.
const (
KeyEsc, KeyEnter, KeySpace, KeyTab, KeyBkSp Key = ...
KeyShift, KeyCtrl, KeyAlt, KeyCaps Key = ...
KeyF1 .. KeyF12 Key = ...
KeyA .. KeyZ Key = ...
Key0 .. Key9 Key = ...
KeyArrowUp, KeyArrowDown, KeyLeft, KeyRight Key = ...
KeyHome, KeyEnd, KeyPageUp, KeyPageDown Key = ...
KeyInsert, KeyDelete Key = ...
)func EnablePerMonitorDPI() errorEnablePerMonitorDPI sets the current process to be Per-Monitor (v2) DPI aware.
func GetCursorPos() (int32, int32, error)GetCursorPos returns the current absolute screen coordinates of the mouse cursor.
func SetBackend(b Backend) errorSetBackend sets the input simulation backend. If BackendHID is selected, it attempts to initialize the driver immediately and returns an error if it fails (e.g. driver not installed).
func SetHIDLibraryPath(path string)SetHIDLibraryPath sets the custom path for interception.dll.
func MoveMouseTo(x, y int32) errorMoveMouseTo moves the mouse cursor to the absolute screen coordinates (Virtual Desktop).
func ClickMouseAt(x, y int32) errorClickMouseAt moves the mouse to the specified screen coordinates and performs a left click.
func ClickRightMouseAt(x, y int32) errorClickRightMouseAt moves the mouse to the specified screen coordinates and performs a right click.
func ClickMiddleMouseAt(x, y int32) errorClickMiddleMouseAt moves the mouse to the specified screen coordinates and performs a middle click.
func DoubleClickMouseAt(x, y int32) errorDoubleClickMouseAt moves the mouse to the specified screen coordinates and performs a left double-click.
func KeyDown(k Key) errorKeyDown simulates a global key down event.
func KeyUp(k Key) errorKeyUp simulates a global key up event.
func Press(k Key) errorPress simulates a global key press (down then up) with a short delay.
func PressHotkey(keys ...Key) errorPressHotkey simulates a key combination (e.g., Ctrl+C). It presses keys in order, waits 50ms, then releases them in reverse order.
func Type(text string) errorType simulates global text input by simulating keystrokes for each character.
func CaptureVirtualDesktop() (*image.RGBA, error)CaptureVirtualDesktop (in package screen) captures the entire virtual desktop (all monitors) using GDI. It requires the process to be Per-Monitor DPI Aware. Returns an *image.RGBA.
func CaptureRegion(x, y, w, h int32) (*image.RGBA, error)CaptureRegion (in package screen) captures a specific region of the virtual desktop.
x, y are Virtual Desktop coordinates (allowed to be negative).
w, h are pixel dimensions.
It internally calls CaptureVirtualDesktop, converts coordinates, and performs a safe crop.
func FindByTitle(title string) (*Window, error)FindByTitle searches for a top-level window matching the exact title.
func FindByClass(class string) (*Window, error)FindByClass searches for a top-level window matching the class name.
func FindByPID(pid uint32) ([]*Window, error)FindByPID returns all top-level windows belonging to the specified Process ID.
func FindByProcessName(name string) ([]*Window, error)FindByProcessName returns all top-level windows belonging to the process with the given executable name.
func (w *Window) FindChildByClass(class string) (*Window, error)FindChildByClass searches for a child window with the specified class name (e.g. "Edit" inside Notepad).
func (w *Window) Move(x, y int32) errorMove moves the mouse cursor to the specified coordinates relative to the window's client area.
func (w *Window) MoveRel(dx, dy int32) errorMoveRel moves the mouse cursor relative to its current position.
func (w *Window) Click(x, y int32) errorClick performs a left mouse button click at the specified client coordinates.
func (w *Window) ClickRight(x, y int32) errorClickRight performs a right mouse button click at the specified client coordinates.
func (w *Window) ClickMiddle(x, y int32) errorClickMiddle performs a middle mouse button click at the specified client coordinates.
func (w *Window) DoubleClick(x, y int32) errorDoubleClick performs a left mouse button double-click.
func (w *Window) Scroll(x, y int32, delta int32) errorScroll performs a vertical mouse wheel scroll at the specified coordinates.
func (w *Window) KeyDown(key Key) errorKeyDown sends a key down event to the window.
func (w *Window) KeyUp(key Key) errorKeyUp sends a key up event to the window.
func (w *Window) Press(key Key) errorPress simulates a full keystroke (KeyDown followed by KeyUp).
func (w *Window) PressHotkey(keys ...Key) errorPressHotkey presses a combination of keys in order and releases them in reverse order.
func (w *Window) Type(text string) errorTypes a string, automatically handling Shift modifiers.
func (w *Window) DPI() (uint32, uint32, error)DPI returns the horizontal and vertical DPI for the window.
func (w *Window) ClientRect() (width, height int32, err error)ClientRect returns the width and height of the window's client area.
func (w *Window) ScreenToClient(x, y int32) (cx, cy int32, err error)ScreenToClient converts screen-relative coordinates to window-client-relative coordinates.
func (w *Window) ClientToScreen(x, y int32) (sx, sy int32, err error)ClientToScreen converts window-client-relative coordinates to screen-relative coordinates.