keyboard

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package keyboard provides keyboard input injection and global event monitoring.

This package wraps the axidev-io keyboard C API, providing a safe and idiomatic Go interface for simulating keyboard input and monitoring global keyboard events.

Sender - Keyboard Input Injection

Create a Sender to inject keyboard input:

sender, err := keyboard.NewSender()
if err != nil {
    log.Fatal(err)
}
defer sender.Close()

// Type some text
sender.TypeText("Hello, World!")

// Press a key combination (Ctrl+S)
sender.Combo(keyboard.ModCtrl, keyboard.StringToKey("S"))

// Tap a single key
sender.Tap(keyboard.StringToKey("Enter"))

Listener - Global Keyboard Event Monitoring

Create a Listener to monitor keyboard events:

listener, err := keyboard.NewListener()
if err != nil {
    log.Fatal(err)
}
defer listener.Close()

err = listener.Start(func(event keyboard.KeyEvent) {
    if event.Pressed {
        fmt.Printf("Key pressed: %s\n", keyboard.KeyToString(event.Key))
    }
})

Thread Safety

All Sender and Listener methods are thread-safe. However, the listener callback is invoked from a background thread, so callbacks must be thread-safe and avoid blocking operations.

Permissions

On some platforms, accessibility or input monitoring permissions may be required. Check the Capabilities struct returned by sender.Capabilities() to determine what permissions are needed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func KeyToString

func KeyToString(key Key) string

KeyToString converts a Key to its canonical string name.

Types

type Capabilities

type Capabilities struct {
	// CanInjectKeys is true if the backend can send physical key events.
	CanInjectKeys bool

	// CanInjectText is true if the backend can inject arbitrary Unicode text.
	CanInjectText bool

	// CanSimulateHID is true if the backend simulates low-level HID events (e.g., uinput).
	CanSimulateHID bool

	// SupportsKeyRepeat is true if key repeat is supported by the backend.
	SupportsKeyRepeat bool

	// NeedsAccessibilityPerm is true if accessibility permission is required (platform-dependent).
	NeedsAccessibilityPerm bool

	// NeedsInputMonitoringPerm is true if input monitoring permission is required (platform-dependent).
	NeedsInputMonitoringPerm bool

	// NeedsUinputAccess is true if uinput or similar device access is required.
	NeedsUinputAccess bool
}

Capabilities describes the features supported by the keyboard backend.

type Key

type Key uint16

Key represents a logical keyboard key identifier.

func StringToKey

func StringToKey(name string) Key

StringToKey parses a key name string to a Key value. Returns 0 (Key unknown) for unrecognized inputs. The parsing is case-insensitive and accepts common aliases like "esc", "space".

Common key names:

  • Letters: "A"-"Z"
  • Digits: "0"-"9"
  • Function keys: "F1"-"F24"
  • Navigation: "Up", "Down", "Left", "Right", "Home", "End", "PageUp", "PageDown"
  • Editing: "Backspace", "Delete", "Insert", "Tab", "Return", "Enter", "Space"
  • Modifiers: "Shift", "Control", "Alt", "Super", "Meta", "CapsLock", "NumLock"
  • Special: "Escape", "Esc", "PrintScreen", "ScrollLock", "Pause"

type KeyEvent

type KeyEvent struct {
	// Codepoint is the Unicode codepoint produced by the event (0 if none).
	Codepoint uint32

	// Key is the logical key ID (0 if unknown).
	Key Key

	// Modifiers is the current modifier bitmask.
	Modifiers Modifier

	// Pressed is true for key press, false for key release.
	Pressed bool
}

KeyEvent represents a keyboard event received by the listener.

func (KeyEvent) IsPress

func (e KeyEvent) IsPress() bool

IsPress returns true if this is a key press event.

func (KeyEvent) IsRelease

func (e KeyEvent) IsRelease() bool

IsRelease returns true if this is a key release event.

func (KeyEvent) KeyName

func (e KeyEvent) KeyName() string

KeyName returns the canonical name of the key.

func (KeyEvent) Rune

func (e KeyEvent) Rune() rune

Rune returns the Unicode rune for this event, or 0 if none.

type Listener

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

Listener provides global keyboard event monitoring.

func NewListener

func NewListener() (*Listener, error)

NewListener creates a new keyboard Listener instance. Returns an error if allocation fails.

func (*Listener) Close

func (l *Listener) Close()

Close destroys the listener and releases resources. Safe to call multiple times.

func (*Listener) IsListening

func (l *Listener) IsListening() bool

IsListening returns true if the listener is currently active.

func (*Listener) Start

func (l *Listener) Start(callback ListenerCallback) error

Start begins listening for keyboard events. The callback may be invoked from an internal thread. The callback must be thread-safe and avoid long-blocking work.

func (*Listener) Stop

func (l *Listener) Stop()

Stop stops listening for keyboard events. Safe to call from any thread; no-op if not running.

type ListenerCallback

type ListenerCallback func(event KeyEvent)

ListenerCallback is the function signature for keyboard event callbacks. The callback may be invoked from an internal thread and must be thread-safe.

type Modifier

type Modifier uint8

Modifier represents a bitmask of keyboard modifier keys.

Modifier constants matching the C API. These can be combined using bitwise OR:

mods := keyboard.ModCtrl | keyboard.ModShift
sender.Combo(mods, keyboard.StringToKey("S"))  // Ctrl+Shift+S

func (Modifier) HasAlt

func (m Modifier) HasAlt() bool

HasAlt returns true if the Alt modifier is set.

func (Modifier) HasCapsLock

func (m Modifier) HasCapsLock() bool

HasCapsLock returns true if CapsLock is active.

func (Modifier) HasCtrl

func (m Modifier) HasCtrl() bool

HasCtrl returns true if the Ctrl modifier is set.

func (Modifier) HasNumLock

func (m Modifier) HasNumLock() bool

HasNumLock returns true if NumLock is active.

func (Modifier) HasShift

func (m Modifier) HasShift() bool

HasShift returns true if the Shift modifier is set.

func (Modifier) HasSuper

func (m Modifier) HasSuper() bool

HasSuper returns true if the Super (Windows/Command) modifier is set.

type Sender

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

Sender provides keyboard input injection capabilities.

func NewSender

func NewSender() (*Sender, error)

NewSender creates a new keyboard Sender instance. Returns an error if allocation fails.

func (*Sender) ActiveModifiers

func (s *Sender) ActiveModifiers() Modifier

ActiveModifiers returns the currently active modifiers.

func (*Sender) BackendType

func (s *Sender) BackendType() uint8

BackendType returns the active backend type as an integer.

func (*Sender) Capabilities

func (s *Sender) Capabilities() Capabilities

Capabilities returns the backend capabilities.

func (*Sender) Close

func (s *Sender) Close()

Close destroys the sender and releases resources. Safe to call multiple times.

func (*Sender) Combo

func (s *Sender) Combo(mods Modifier, key Key) error

Combo executes a key combo: press modifiers, tap key, release modifiers.

func (*Sender) Flush

func (s *Sender) Flush()

Flush forces delivery of pending keyboard events.

func (*Sender) HoldModifier

func (s *Sender) HoldModifier(mods Modifier) error

HoldModifier presses the specified modifier keys.

func (*Sender) IsReady

func (s *Sender) IsReady() bool

IsReady returns true if the backend is ready to inject keyboard events.

func (*Sender) KeyDown

func (s *Sender) KeyDown(key Key) error

KeyDown simulates a physical key press.

func (*Sender) KeyUp

func (s *Sender) KeyUp(key Key) error

KeyUp simulates a physical key release.

func (*Sender) ReleaseAllModifiers

func (s *Sender) ReleaseAllModifiers() error

ReleaseAllModifiers releases all currently held modifiers.

func (*Sender) ReleaseModifier

func (s *Sender) ReleaseModifier(mods Modifier) error

ReleaseModifier releases the specified modifier keys.

func (*Sender) RequestPermissions

func (s *Sender) RequestPermissions() bool

RequestPermissions requests runtime permissions required by the backend. Returns true if the backend is ready after requesting permissions.

func (*Sender) SetKeyDelay

func (s *Sender) SetKeyDelay(delayMicroseconds uint32)

SetKeyDelay sets the delay (in microseconds) used by tap/combo operations.

func (*Sender) Tap

func (s *Sender) Tap(key Key) error

Tap simulates a key tap (press then release).

func (*Sender) TypeCharacter

func (s *Sender) TypeCharacter(codepoint rune) error

TypeCharacter injects a single Unicode codepoint.

func (*Sender) TypeText

func (s *Sender) TypeText(text string) error

TypeText injects UTF-8 text directly (layout-independent on supporting backends).