Documentation
¶
Overview ¶
Package winput provides a Windows input automation library focused on background operation. It abstracts input injection to support multiple backends while maintaining a consistent, object-centric API.
Key Features:
1. Pure Go & No CGO: This library uses dynamic DLL loading (syscall.LoadLibrary) and does not require a CGO compiler environment (GCC) for building.
2. Dual Input Backends:
- BackendMessage (Default): Uses PostMessage for background input. It does not require focus and is ideal for non-intrusive automation.
- BackendHID: Uses the Interception driver for kernel-level simulation (requires driver installation). This mode simulates hardware-level input, complete with human-like mouse movement trajectories and jitter. Supports custom DLL path via SetHIDLibraryPath.
3. Coordinate System & Screen Management:
- Window-Centric: Operations on *Window use client coordinates.
- Global Input: MoveMouseTo/ClickMouseAt/Type for absolute virtual desktop coordinates (useful for Electron/Games).
- Child Windows: FindChildByClass for targeting specific controls (e.g. "Edit" in Notepad).
- Screen Package: winput/screen helpers for querying monitor bounds and virtual desktop geometry.
- DPI Awareness: Per-Monitor v2 support for accurate mapping.
4. Intelligent Keyboard Input:
- Type(string) automatically handles Shift modifiers for uppercase letters and symbols.
- Uses Scan Codes (Set 1) for maximum compatibility with low-level hooks and games.
5. Robust Error Handling: Defines standard errors like ErrWindowNotFound, ErrDriverNotInstalled, ErrPostMessageFailed. It follows an "explicit failure" principle, where backend initialization errors are reported on the first attempted action.
6. Thread Safety: All public input methods are thread-safe and serialized using an internal mutex. This prevents state pollution (e.g., mixing Shift states from concurrent operations) and race conditions when switching backends.
Example:
// For complete examples, see cmd/example/
// - basic_message: Background automation for standard apps
// - global_vision: Electron/Game automation using screen coordinates
// - advanced_hid: Hardware level simulation
// 1. Find the window
w, err := winput.FindByTitle("Untitled - Notepad")
if err != nil {
log.Fatal(winput.ErrWindowNotFound)
}
// 2. Setup DPI awareness (optional but recommended)
winput.EnablePerMonitorDPI()
// 3. Perform actions (using default Message backend)
w.Click(100, 100) // Left click
w.ClickRight(100, 100) // Right click
w.Scroll(100, 100, 120) // Vertical scroll
w.Type("Hello World!") // Automatically handles Shift for 'H', 'W', and '!'
w.Press(winput.KeyEnter)
// 4. Global Input (Visual Automation / Electron)
// winput.MoveMouseTo(1920, 500)
// winput.ClickMouseAt(1920, 500)
// 5. Switch to HID backend for hardware-level simulation
// winput.SetHIDLibraryPath("libs/interception.dll")
// winput.SetBackend(winput.BackendHID)
Index ¶
- Constants
- Variables
- func ClickMiddleMouseAt(x, y int32) error
- func ClickMouseAt(x, y int32) error
- func ClickRightMouseAt(x, y int32) error
- func DoubleClickMouseAt(x, y int32) error
- func EnablePerMonitorDPI() error
- func GetCursorPos() (int32, int32, error)
- func KeyDown(k Key) error
- func KeyUp(k Key) error
- func MoveMouseTo(x, y int32) error
- func Press(k Key) error
- func PressHotkey(keys ...Key) error
- func SetBackend(b Backend) error
- func SetHIDLibraryPath(path string)
- func Type(text string) error
- type Backend
- type Key
- type Window
- func (w *Window) Click(x, y int32) error
- func (w *Window) ClickMiddle(x, y int32) error
- func (w *Window) ClickRight(x, y int32) error
- func (w *Window) ClientRect() (width, height int32, err error)
- func (w *Window) ClientToScreen(x, y int32) (sx, sy int32, err error)
- func (w *Window) DPI() (uint32, uint32, error)
- func (w *Window) DoubleClick(x, y int32) error
- func (w *Window) FindChildByClass(class string) (*Window, error)
- func (w *Window) IsValid() bool
- func (w *Window) IsVisible() bool
- func (w *Window) KeyDown(key Key) error
- func (w *Window) KeyUp(key Key) error
- func (w *Window) Move(x, y int32) error
- func (w *Window) MoveRel(dx, dy int32) error
- func (w *Window) Press(key Key) error
- func (w *Window) PressHotkey(keys ...Key) error
- func (w *Window) ScreenToClient(x, y int32) (cx, cy int32, err error)
- func (w *Window) Scroll(x, y int32, delta int32) error
- func (w *Window) Type(text string) error
Constants ¶
const ( KeyEsc = keyboard.KeyEsc Key1 = keyboard.Key1 Key2 = keyboard.Key2 Key3 = keyboard.Key3 Key4 = keyboard.Key4 Key5 = keyboard.Key5 Key6 = keyboard.Key6 Key7 = keyboard.Key7 Key8 = keyboard.Key8 Key9 = keyboard.Key9 Key0 = keyboard.Key0 KeyMinus = keyboard.KeyMinus KeyEqual = keyboard.KeyEqual KeyBkSp = keyboard.KeyBkSp KeyTab = keyboard.KeyTab KeyQ = keyboard.KeyQ KeyW = keyboard.KeyW KeyE = keyboard.KeyE KeyR = keyboard.KeyR KeyT = keyboard.KeyT KeyY = keyboard.KeyY KeyU = keyboard.KeyU KeyI = keyboard.KeyI KeyO = keyboard.KeyO KeyP = keyboard.KeyP KeyLBr = keyboard.KeyLBr KeyRBr = keyboard.KeyRBr KeyEnter = keyboard.KeyEnter KeyCtrl = keyboard.KeyCtrl KeyA = keyboard.KeyA KeyS = keyboard.KeyS KeyD = keyboard.KeyD KeyF = keyboard.KeyF KeyG = keyboard.KeyG KeyH = keyboard.KeyH KeyJ = keyboard.KeyJ KeyK = keyboard.KeyK KeyL = keyboard.KeyL KeySemi = keyboard.KeySemi KeyQuot = keyboard.KeyQuot KeyTick = keyboard.KeyTick KeyShift = keyboard.KeyShift KeyBackslash = keyboard.KeyBackslash KeyZ = keyboard.KeyZ KeyX = keyboard.KeyX KeyC = keyboard.KeyC KeyV = keyboard.KeyV KeyB = keyboard.KeyB KeyN = keyboard.KeyN KeyM = keyboard.KeyM KeyComma = keyboard.KeyComma KeyDot = keyboard.KeyDot KeySlash = keyboard.KeySlash KeyAlt = keyboard.KeyAlt KeySpace = keyboard.KeySpace KeyCaps = keyboard.KeyCaps KeyF1 = keyboard.KeyF1 KeyF2 = keyboard.KeyF2 KeyF3 = keyboard.KeyF3 KeyF4 = keyboard.KeyF4 KeyF5 = keyboard.KeyF5 KeyF6 = keyboard.KeyF6 KeyF7 = keyboard.KeyF7 KeyF8 = keyboard.KeyF8 KeyF9 = keyboard.KeyF9 KeyF10 = keyboard.KeyF10 KeyF11 = keyboard.KeyF11 KeyF12 = keyboard.KeyF12 KeyNumLock = keyboard.KeyNumLock KeyScroll = keyboard.KeyScroll KeyHome = keyboard.KeyHome KeyArrowUp = keyboard.KeyArrowUp KeyPageUp = keyboard.KeyPageUp KeyLeft = keyboard.KeyLeft KeyRight = keyboard.KeyRight KeyEnd = keyboard.KeyEnd KeyArrowDown = keyboard.KeyArrowDown KeyPageDown = keyboard.KeyPageDown KeyInsert = keyboard.KeyInsert KeyDelete = keyboard.KeyDelete )
const ( INPUT_KEYBOARD = 1 KEYEVENTF_UNICODE = 0x0004 KEYEVENTF_KEYUP = 0x0002 )
Variables ¶
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 = 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. ErrPostMessageFailed = window.ErrPostMessageFailed )
Functions ¶
func ClickMiddleMouseAt ¶
ClickMiddleMouseAt moves to the specified screen coordinates and performs a middle click.
func ClickMouseAt ¶
ClickMouseAt moves to the specified screen coordinates and performs a left click.
func ClickRightMouseAt ¶
ClickRightMouseAt moves to the specified screen coordinates and performs a right click.
func DoubleClickMouseAt ¶
DoubleClickMouseAt moves to the specified screen coordinates and performs a left double-click.
func EnablePerMonitorDPI ¶
func EnablePerMonitorDPI() error
EnablePerMonitorDPI sets the process to be Per-Monitor DPI aware.
func GetCursorPos ¶
GetCursorPos returns the current cursor position in screen coordinates.
func MoveMouseTo ¶
MoveMouseTo moves the mouse cursor to the specified absolute screen coordinates (Virtual Desktop).
func PressHotkey ¶
PressHotkey simulates a global combination of keys.
func SetBackend ¶
SetBackend sets the input simulation backend. If BackendHID is selected, it attempts to initialize the Interception driver immediately. Returns an error if the driver or DLL cannot be loaded.
func SetHIDLibraryPath ¶
func SetHIDLibraryPath(path string)
SetHIDLibraryPath sets the path to the interception.dll library.
Types ¶
type Key ¶
func KeyFromRune ¶
KeyFromRune attempts to map a unicode character to a Key.
type Window ¶
type Window struct {
HWND uintptr
}
Window represents a handle to a window.
func FindByClass ¶
FindByClass searches for a top-level window matching the specified class name.
func FindByProcessName ¶
FindByProcessName searches for all top-level windows belonging to a process with the given executable name.
func FindByTitle ¶
FindByTitle searches for a top-level window matching the exact title.
func (*Window) Click ¶
Click simulates a left mouse button click at the specified client coordinates.
func (*Window) ClickMiddle ¶
ClickMiddle simulates a middle mouse button click at the specified client coordinates.
func (*Window) ClickRight ¶
ClickRight simulates a right mouse button click at the specified client coordinates.
func (*Window) ClientRect ¶
ClientRect returns the client area dimensions of the window.
func (*Window) ClientToScreen ¶
ClientToScreen converts client coordinates to screen coordinates.
func (*Window) DoubleClick ¶
DoubleClick simulates a left mouse button double-click at the specified client coordinates.
func (*Window) FindChildByClass ¶
FindChildByClass searches for a child window with the specified class name.
func (*Window) MoveRel ¶
MoveRel simulates relative mouse movement from the current cursor position.
func (*Window) PressHotkey ¶
PressHotkey presses a combination of keys (e.g., Ctrl+A).
func (*Window) ScreenToClient ¶
ScreenToClient converts screen coordinates to client coordinates.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
example/advanced_hid
command
|
|
|
example/basic_message
command
|
|
|
example/global_vision
command
|
|