170 lines
4.3 KiB
Go
170 lines
4.3 KiB
Go
// Package input injects mouse and keyboard events on the host via the
|
|||
|
|
// Win32 SendInput API — the same mechanism any accessibility tool or
|
||
|
|
// remote-desktop product uses; it does not touch drivers or the kernel.
|
||
|
|
package input
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"unsafe"
|
||
|
|
|
||
|
|
"golang.org/x/sys/windows"
|
||
|
|
)
|
||
|
|
|
||
|
|
var (
|
||
|
|
user32 = windows.NewLazySystemDLL("user32.dll")
|
||
|
|
procSendInput = user32.NewProc("SendInput")
|
||
|
|
)
|
||
|
|
|
||
|
|
const (
|
||
|
|
inputMouse = 0
|
||
|
|
inputKeyboard = 1
|
||
|
|
|
||
|
|
mouseEventFMove = 0x0001
|
||
|
|
mouseEventFLeftDown = 0x0002
|
||
|
|
mouseEventFLeftUp = 0x0004
|
||
|
|
mouseEventFRightDown = 0x0008
|
||
|
|
mouseEventFRightUp = 0x0010
|
||
|
|
mouseEventFMiddleDown = 0x0020
|
||
|
|
mouseEventFMiddleUp = 0x0040
|
||
|
|
mouseEventFWheel = 0x0800
|
||
|
|
mouseEventFVirtualDesk = 0x4000
|
||
|
|
mouseEventFAbsolute = 0x8000
|
||
|
|
|
||
|
|
keyEventFKeyUp = 0x0002
|
||
|
|
)
|
||
|
|
|
||
|
|
// mouseInputBody mirrors Win32 MOUSEINPUT exactly (32 bytes on amd64).
|
||
|
|
type mouseInputBody struct {
|
||
|
|
Dx, Dy int32
|
||
|
|
MouseData uint32
|
||
|
|
DwFlags uint32
|
||
|
|
Time uint32
|
||
|
|
DwExtraInfo uintptr
|
||
|
|
}
|
||
|
|
|
||
|
|
// keybdInputBody mirrors Win32 KEYBDINPUT exactly (24 bytes on amd64).
|
||
|
|
type keybdInputBody struct {
|
||
|
|
Vk uint16
|
||
|
|
Scan uint16
|
||
|
|
DwFlags uint32
|
||
|
|
Time uint32
|
||
|
|
DwExtraInfo uintptr
|
||
|
|
}
|
||
|
|
|
||
|
|
// mouseInputEvent and keybdInputEvent each mirror the 40-byte Win32 INPUT
|
||
|
|
// struct on amd64 (4-byte type + 4-byte alignment pad + 32-byte union).
|
||
|
|
// We build one full INPUT-sized value per call rather than a generic union
|
||
|
|
// type so the memory layout SendInput expects is explicit and easy to check
|
||
|
|
// against MSDN's struct definitions.
|
||
|
|
type mouseInputEvent struct {
|
||
|
|
Type uint32
|
||
|
|
_ uint32
|
||
|
|
Body mouseInputBody
|
||
|
|
}
|
||
|
|
|
||
|
|
type keybdInputEvent struct {
|
||
|
|
Type uint32
|
||
|
|
_ uint32
|
||
|
|
Body keybdInputBody
|
||
|
|
_ [8]byte
|
||
|
|
}
|
||
|
|
|
||
|
|
func sendInput(ptr unsafe.Pointer, size uintptr) error {
|
||
|
|
ret, _, err := procSendInput.Call(1, uintptr(ptr), size)
|
||
|
|
if ret == 0 {
|
||
|
|
return fmt.Errorf("input: SendInput failed: %w", err)
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// MouseMove moves the pointer to an absolute position given as a fraction
|
||
|
|
// of the entire virtual desktop on each axis (0..65535), matching
|
||
|
|
// MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_VIRTUALDESK's normalized coordinate
|
||
|
|
// space. VIRTUALDESK specifically is what makes this map across *all*
|
||
|
|
// monitors combined (internal/capture.VirtualScreenBounds) rather than
|
||
|
|
// just the primary one — without it, MOUSEEVENTF_ABSOLUTE alone only
|
||
|
|
// covers the primary display, so clicks on a secondary monitor would land
|
||
|
|
// in the wrong place (or not at all) despite that monitor being visible
|
||
|
|
// and clickable in the viewer.
|
||
|
|
func MouseMove(normX, normY uint16) error {
|
||
|
|
ev := mouseInputEvent{
|
||
|
|
Type: inputMouse,
|
||
|
|
Body: mouseInputBody{
|
||
|
|
Dx: int32(normX),
|
||
|
|
Dy: int32(normY),
|
||
|
|
DwFlags: mouseEventFMove | mouseEventFAbsolute | mouseEventFVirtualDesk,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
return sendInput(unsafe.Pointer(&ev), unsafe.Sizeof(ev))
|
||
|
|
}
|
||
|
|
|
||
|
|
type MouseButton int
|
||
|
|
|
||
|
|
const (
|
||
|
|
ButtonLeft MouseButton = iota
|
||
|
|
ButtonRight
|
||
|
|
ButtonMiddle
|
||
|
|
)
|
||
|
|
|
||
|
|
func mouseButtonFlag(b MouseButton, down bool) (uint32, error) {
|
||
|
|
switch b {
|
||
|
|
case ButtonLeft:
|
||
|
|
if down {
|
||
|
|
return mouseEventFLeftDown, nil
|
||
|
|
}
|
||
|
|
return mouseEventFLeftUp, nil
|
||
|
|
case ButtonRight:
|
||
|
|
if down {
|
||
|
|
return mouseEventFRightDown, nil
|
||
|
|
}
|
||
|
|
return mouseEventFRightUp, nil
|
||
|
|
case ButtonMiddle:
|
||
|
|
if down {
|
||
|
|
return mouseEventFMiddleDown, nil
|
||
|
|
}
|
||
|
|
return mouseEventFMiddleUp, nil
|
||
|
|
default:
|
||
|
|
return 0, fmt.Errorf("input: unknown mouse button %d", b)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func MouseButtonEvent(b MouseButton, down bool) error {
|
||
|
|
flag, err := mouseButtonFlag(b, down)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
ev := mouseInputEvent{
|
||
|
|
Type: inputMouse,
|
||
|
|
Body: mouseInputBody{DwFlags: flag},
|
||
|
|
}
|
||
|
|
return sendInput(unsafe.Pointer(&ev), unsafe.Sizeof(ev))
|
||
|
|
}
|
||
|
|
|
||
|
|
// MouseWheel scrolls vertically. delta follows Win32 convention: positive
|
||
|
|
// scrolls up/forward in units of WHEEL_DELTA (120).
|
||
|
|
func MouseWheel(delta int16) error {
|
||
|
|
ev := mouseInputEvent{
|
||
|
|
Type: inputMouse,
|
||
|
|
Body: mouseInputBody{
|
||
|
|
MouseData: uint32(uint16(delta)),
|
||
|
|
DwFlags: mouseEventFWheel,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
return sendInput(unsafe.Pointer(&ev), unsafe.Sizeof(ev))
|
||
|
|
}
|
||
|
|
|
||
|
|
// KeyEvent injects a virtual-key press or release.
|
||
|
|
func KeyEvent(vk uint16, down bool) error {
|
||
|
|
var flags uint32
|
||
|
|
if !down {
|
||
|
|
flags = keyEventFKeyUp
|
||
|
|
}
|
||
|
|
ev := keybdInputEvent{
|
||
|
|
Type: inputKeyboard,
|
||
|
|
Body: keybdInputBody{
|
||
|
|
Vk: vk,
|
||
|
|
DwFlags: flags,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
return sendInput(unsafe.Pointer(&ev), unsafe.Sizeof(ev))
|
||
|
|
}
|