user32util

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2021 License: MIT Imports: 4 Imported by: 4

README

user32util

GoDoc

Package user32util provides helper functionality for working with Windows' user32 library.

APIs

The library offers several helper functions for working with user32.

Many of these functions require that you first load the user32 DLL:

user32, err := user32util.LoadUser32DLL()
if err != nil {
	// Error handling.
}
Input listeners
  • NewLowLevelMouseListener() - Starts a listener that reports on mouse input
  • NewLowLevelKeyboardListener() - Starts a listener that reports on keyboard input
Send input
  • SendKeydbInput() - Sends a single keyboard input
  • SendMouseInput() - Sends a single mouse input
  • SendInput() - Send input implements the SendInput() Windows system call
  • SendHardwareInput() - Sends a single hardware input

Examples

The following examples can be found in the examples/ directory:

  • moveandclickmouse - Moves the mouse and then left clicks on the new position. Takes inputs as command line arguments in x,y format. E.g., example 1221,244 460,892. Coordinates can be printed by running: example print
  • readkeyboard - Reads keyboard presses and prints them to stderr
  • readmouse - Reads mouse inputs and prints them to stderr
  • sendinput - Sends keyboard or mouse inputs to Windows

Special thanks

This library is influenced by jimmycliff obonyo's work in this GitHub gist: https://gist.github.com/obonyojimmy/52d836a1b31e2fc914d19a81bd2e0a1b

Thank you for documenting your work, jimmycliff.

Documentation

Overview

Package user32util provides helper functionality for working with Windows' user32 library.

Many of these functions require that you first load the user32 DLL:

user32, err := user32util.LoadUser32DLL()
if err != nil {
	// Error handling.
}

While this library provides some high-level documentation about the User32 API, the documentation purposely avoids repeating much from Microsoft's documentation. This is mainly to avoid a game of telephone (i.e., degrade the information provided by Microsoft).

Please refer to the Windows API documentation for more information: https://docs.microsoft.com/en-us/windows/win32/api/winuser/

Index

Constants

View Source
const (
	InputMouse = iota
	InputKeyboard
	InputHardware
)
View Source
const (
	MouseEventFAbsolute       uint32 = 0x8000
	MouseEventFHWheel         uint32 = 0x01000
	MouseEventFMove           uint32 = 0x0001
	MouseEventFMoveNoCoalesce uint32 = 0x2000
	MouseEventFLeftDown       uint32 = 0x0002
	MouseEventFLeftUp         uint32 = 0x0004
	MouseEventFRightDown      uint32 = 0x0008
	MouseEventFRightUp        uint32 = 0x0010
	MouseEventFMiddleDown     uint32 = 0x0020
	MouseEventFMiddleUp       uint32 = 0x0040
	MouseEventFVirtualDesk    uint32 = 0x4000
	MouseEventFWheel          uint32 = 0x0800
	MouseEventFXDown          uint32 = 0x0080
	MouseEventFXUp            uint32 = 0x0100
)

Various MouseInput dwFlags.

Refer to the following Windows API document for more information: https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-mouseinput

View Source
const (
	KeyEventFExtendedKey uint32 = 0x0001
	KeyEventFKeyUp       uint32 = 0x0002
	KeyEventFScanCode    uint32 = 0x0008
	KeyEventFUnicode     uint32 = 0x0004
)

Various KeybdInput dwFlags.

Refer to the following Windows API document for more information: https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-keybdinput

Variables

This section is empty.

Functions

func SendHardwareInput

func SendHardwareInput(input HardwareInput, user32 *User32DLL) error

No idea if this works. Untested.

func SendInput

func SendInput(numInputs uint, unsafePointerToVal unsafe.Pointer, inputStructSizeBytes uintptr, user32 *User32DLL) error

SendInput is a hacky implementation of SendInput that works around the lack of union support.

https://github.com/JamesHovious/w32/blob/master/user32.go works around this by using cgo. I have no desire to make cgo a dependency of the project.

From the Windows API documentation:

Synthesizes keystrokes, mouse motions, and button clicks.

Refer to the following Windows API document for more information: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput

func SendKeydbInput

func SendKeydbInput(input KeybdInput, user32 *User32DLL) error

Wrapper for SendInput() that sends a single KeybdInput.

func SendMouseInput

func SendMouseInput(input MouseInput, user32 *User32DLL) error

Wrapper for SendInput() that sends a single MouseInput.

func SetCursorPos added in v0.3.0

func SetCursorPos(x int32, y int32, user32 *User32DLL) (bool, error)

SetCursorPos sets the mouse cursor position.

From the Windows API documentation:

Moves the cursor to the specified screen coordinates. If the new
coordinates are not within the screen rectangle set by the most
recent ClipCursor function call, the system automatically adjusts
the coordinates so that the cursor stays within the rectangle.

Refer to the Windows API documentation for more information: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setcursorpos

Types

type HardwareInput

type HardwareInput struct {
	UMsg    uint32
	WParamL uint16
	WParamH uint16
}

From the Windows API documentation:

Contains information about a simulated message generated by
an input device other than a keyboard or mouse.

Refer to the following Windows API document for more information: https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-hardwareinput

type KbdllHookStruct

type KbdllHookStruct struct {
	VkCode      uint32
	ScanCode    uint32
	Flags       uint32
	Time        uint32
	DwExtraInfo uintptr
}

From the Windows API documentation:

Contains information about a low-level keyboard input event.

Refer to the following Windows API document for more information: https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-kbdllhookstruct

func (KbdllHookStruct) VirtualKeyCode

func (o KbdllHookStruct) VirtualKeyCode() byte

type KeybdInput

type KeybdInput struct {
	WVK         uint16
	WScan       uint16
	DwFlags     uint32
	Time        uint32
	DwExtraInfo uintptr
}

From the Windows API documentation:

Contains information about a simulated keyboard event.

Refer to the following Windows API document for more information: https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-keybdinput

type KeyboardButtonAction

type KeyboardButtonAction uintptr

KeyboardButtonAction is an alias for the values contained in the wParam field fo LowLevelKeyboardEvent.

const (
	WMKeyDown       KeyboardButtonAction = 256
	WMKeyUp         KeyboardButtonAction = 257
	WHSystemKeyDown KeyboardButtonAction = 260
	WMSystemKeyUp   KeyboardButtonAction = 261
)

LowLevelKeyboardEvent wParam flags.

type LowLevelKeyboardEvent

type LowLevelKeyboardEvent struct {
	WParam uintptr
	LParam uintptr
	Struct *KbdllHookStruct
}

LowLevelKeyboardEvent represents a single keyboard event.

func (LowLevelKeyboardEvent) KeyboardButtonAction

func (o LowLevelKeyboardEvent) KeyboardButtonAction() KeyboardButtonAction

type LowLevelKeyboardEventListener

type LowLevelKeyboardEventListener struct {
	// contains filtered or unexported fields
}

LowLevelKeyboardEventListener represents an instance of the LowLevelKeyboardProc Windows hook.

From the Windows API documentation:

An application-defined or library-defined callback function used
with the SetWindowsHookEx function. The system calls this function
every time a new keyboard input event is about to be posted into
a thread input queue.

Refer to the following Windows API document for more information: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644985%28v=vs.85%29

func NewLowLevelKeyboardListener

func NewLowLevelKeyboardListener(fn OnLowLevelKeyboardEventFunc, user32 *User32DLL) (*LowLevelKeyboardEventListener, error)

NewLowLevelKeyboardListener instantiates a new keyboard input listener using the LowLevelKeyboardProc Windows hook.

Refer to LowLevelKeyboardEventListener for more information.

func (*LowLevelKeyboardEventListener) OnDone

func (o *LowLevelKeyboardEventListener) OnDone() <-chan error

OnDone returns a channel that is written to when the event listener exits. A non-nil error is written if an error caused the listener to exit.

func (*LowLevelKeyboardEventListener) Release

func (o *LowLevelKeyboardEventListener) Release() error

Release releases the underlying hook handle and stops the listener from receiving any additional events.

type LowLevelMouseEvent

type LowLevelMouseEvent struct {
	WParam uintptr
	LParam uintptr
	Struct *MsllHookStruct
}

func (LowLevelMouseEvent) MouseButtonAction

func (o LowLevelMouseEvent) MouseButtonAction() MouseButtonAction

type LowLevelMouseEventListener

type LowLevelMouseEventListener struct {
	// contains filtered or unexported fields
}

LowLevelMouseEventListener represents an instance of the LowLevelMouseProc Windows hook.

From the Windows API documentation:

An application-defined or library-defined callback function
used with the SetWindowsHookEx function. The system calls
this function every time a new mouse input event is about to
be posted into a thread input queue.

Refer to the following Windows API document for more information: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644986%28v=vs.85%29

func NewLowLevelMouseListener

func NewLowLevelMouseListener(fn OnLowLevelMouseEventFunc, user32 *User32DLL) (*LowLevelMouseEventListener, error)

NewLowLevelMouseListener instantiates a new mouse input listener using the LowLevelMouseProc Windows hook.

Refer to LowLevelMouseEventListener for more information.

func (*LowLevelMouseEventListener) OnDone

func (o *LowLevelMouseEventListener) OnDone() <-chan error

OnDone returns a channel that is written to when the event listener exits. A non-nil error is written if an error caused the listener to exit.

func (*LowLevelMouseEventListener) Release

func (o *LowLevelMouseEventListener) Release() error

Release releases the underlying hook handle and stops the listener from receiving any additional events.

type MouseButtonAction

type MouseButtonAction uintptr

MouseButtonAction is an alias for the values contained in the wParam field fo LowLevelKeyboardEvent.

const (
	WMLButtonDown MouseButtonAction = 0x0201
	WMLButtonUp   MouseButtonAction = 0x0202
	WMMouseMove   MouseButtonAction = 0x0200
	WMMouseWheel  MouseButtonAction = 0x020A
	WMMouseHWheel MouseButtonAction = 0x020E
	WMRButtonDown MouseButtonAction = 0x0204
	WMRButtonUp   MouseButtonAction = 0x0205
)

LowLevelMouseEvent wParam flags.

const (
	WMXButtonDown     MouseButtonAction = 0x020B
	WMXButtonUp       MouseButtonAction = 0x020C
	WMXButtonDblClk   MouseButtonAction = 0x020D
	WMNCXButtonDown   MouseButtonAction = 0x00AB
	WMNCXButtonUp     MouseButtonAction = 0x00AC
	WMNCXButtonDblClk MouseButtonAction = 0x00AD
)

Other mouse related message types (unsure where they are used, but they appear in the 'mouseData' field documentation).

type MouseInput

type MouseInput struct {
	Dx          int32
	Dy          int32
	MouseData   uint32
	DwFlags     uint32
	Time        uint32
	DwExtraInfo uintptr
}

From the Windows API documentation:

Contains information about a simulated mouse event.

Refer to the following Windows API document for more information: https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-mouseinput

type Msg

type Msg struct {
	Hwnd     unsafe.Pointer
	Message  uint
	WParam   uintptr
	LParam   uintptr
	Time     uint32
	Pt       Point
	LPrivate uint32
}

From the Windows API documentation:

Contains message information from a thread's message queue.

Refer to the following Windows API document for more information: https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-msg

type MsllHookStruct

type MsllHookStruct struct {
	Point       Point
	MouseData   uint32
	Flags       uint32
	Time        uint32
	DwExtraInfo uintptr
}

From the Windows API documentation:

Contains information about a low-level mouse input event.

Refer to the following Windows API document for more information: https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-msllhookstruct

type OnLowLevelKeyboardEventFunc

type OnLowLevelKeyboardEventFunc func(event LowLevelKeyboardEvent)

type OnLowLevelMouseEventFunc

type OnLowLevelMouseEventFunc func(event LowLevelMouseEvent)

type Point

type Point struct {
	X int32
	Y int32
}

From the Windows API documentation:

The POINT structure defines the x- and y- coordinates of a point.

Refer to the following Windows API document for more information: https://docs.microsoft.com/en-us/previous-versions/dd162805%28v=vs.85%29

type User32DLL

type User32DLL struct {
	// contains filtered or unexported fields
}

User32DLL represents the user32 DLL, mapping several of its procedures to this struct's fields.

func LoadUser32DLL

func LoadUser32DLL() (*User32DLL, error)

LoadUser32DLL loads the user32 DLL into memory.

func (*User32DLL) Release

func (o *User32DLL) Release() error

Release releases the underlying DLL.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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