input

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package input provides CGEvent-based keyboard and mouse input helpers.

It wraps CoreGraphics CGEvent functions via purego (cgo-free) for creating and posting keyboard and mouse events. Events can be posted to a specific process (e.g., a VM window) or to the system HID event tap.

Basic usage:

ev, err := input.CreateKeyboardEvent(0, input.KeyReturn, true)
if err != nil {
	log.Fatal(err)
}
input.PostToSelf(ev)

Index

Constants

View Source
const (
	EventNull           = 0
	EventLeftMouseDown  = 1
	EventLeftMouseUp    = 2
	EventRightMouseDown = 3
	EventRightMouseUp   = 4
	EventMouseMoved     = 5
	EventKeyDown        = 10
	EventKeyUp          = 11
)

CGEvent mouse and keyboard event types.

View Source
const (
	KeyReturn = 36
	KeyTab    = 48
	KeySpace  = 49
	KeyDelete = 51
	KeyEscape = 53
)

Common virtual key codes.

View Source
const HIDEventTap = 0

HIDEventTap is the location for posting events to the HID system.

Variables

This section is empty.

Functions

func CreateKeyboardEvent

func CreateKeyboardEvent(source uintptr, virtualKey uint16, keyDown bool) (uintptr, error)

CreateKeyboardEvent creates a keyboard event. virtualKey is the macOS virtual key code (e.g., KeyReturn=36, KeyTab=48).

func CreateMouseEvent

func CreateMouseEvent(source uintptr, mouseType uint32, position corefoundation.CGPoint, mouseButton uint32) (uintptr, error)

CreateMouseEvent creates a mouse event at the given position. The source parameter is typically 0 (no source). mouseType should be one of the Event* mouse constants. mouseButton is 0 for left, 1 for right.

func Post

func Post(tap uint32, event uintptr) error

Post posts an event to the given HID event tap location. Use HIDEventTap for the system HID tap.

func PostToSelf

func PostToSelf(event uintptr) error

PostToSelf posts an event to this process. This is useful for directing keystrokes to a VM window owned by the current process rather than whatever app the user has focused.

func SetFlags

func SetFlags(event uintptr, flags uint64) error

SetFlags sets the modifier flags (shift, control, etc.) on an event.

func SetUnicodeString

func SetUnicodeString(event uintptr, s string) error

SetUnicodeString sets the Unicode string on a keyboard event. This allows typing arbitrary characters that don't have a direct keycode.

func TypeCharacter

func TypeCharacter(char rune) error

TypeCharacter types a single character by creating key down/up events with the character set as a Unicode string. Events are posted to the current process.

Types

This section is empty.

Jump to

Keyboard shortcuts

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