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 ¶
- func KeyToString(key Key) string
- type Capabilities
- type Key
- type KeyEvent
- type Listener
- type ListenerCallback
- type Modifier
- type Sender
- func (s *Sender) ActiveModifiers() Modifier
- func (s *Sender) BackendType() uint8
- func (s *Sender) Capabilities() Capabilities
- func (s *Sender) Close()
- func (s *Sender) Combo(mods Modifier, key Key) error
- func (s *Sender) Flush()
- func (s *Sender) HoldModifier(mods Modifier) error
- func (s *Sender) IsReady() bool
- func (s *Sender) KeyDown(key Key) error
- func (s *Sender) KeyUp(key Key) error
- func (s *Sender) ReleaseAllModifiers() error
- func (s *Sender) ReleaseModifier(mods Modifier) error
- func (s *Sender) RequestPermissions() bool
- func (s *Sender) SetKeyDelay(delayMicroseconds uint32)
- func (s *Sender) Tap(key Key) error
- func (s *Sender) TypeCharacter(codepoint rune) error
- func (s *Sender) TypeText(text string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func KeyToString ¶
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 ¶
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.
type Listener ¶
type Listener struct {
// contains filtered or unexported fields
}
Listener provides global keyboard event monitoring.
func NewListener ¶
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 ¶
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.
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.
const ( ModShift Modifier = C.AXIDEV_IO_MOD_SHIFT ModCtrl Modifier = C.AXIDEV_IO_MOD_CTRL ModAlt Modifier = C.AXIDEV_IO_MOD_ALT ModSuper Modifier = C.AXIDEV_IO_MOD_SUPER ModCapsLock Modifier = C.AXIDEV_IO_MOD_CAPSLOCK ModNumLock Modifier = C.AXIDEV_IO_MOD_NUMLOCK )
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) HasCapsLock ¶
HasCapsLock returns true if CapsLock is active.
func (Modifier) HasNumLock ¶
HasNumLock returns true if NumLock is active.
type Sender ¶
type Sender struct {
// contains filtered or unexported fields
}
Sender provides keyboard input injection capabilities.
func NewSender ¶
NewSender creates a new keyboard Sender instance. Returns an error if allocation fails.
func (*Sender) ActiveModifiers ¶
ActiveModifiers returns the currently active modifiers.
func (*Sender) BackendType ¶
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) HoldModifier ¶
HoldModifier presses the specified modifier keys.
func (*Sender) ReleaseAllModifiers ¶
ReleaseAllModifiers releases all currently held modifiers.
func (*Sender) ReleaseModifier ¶
ReleaseModifier releases the specified modifier keys.
func (*Sender) RequestPermissions ¶
RequestPermissions requests runtime permissions required by the backend. Returns true if the backend is ready after requesting permissions.
func (*Sender) SetKeyDelay ¶
SetKeyDelay sets the delay (in microseconds) used by tap/combo operations.
func (*Sender) TypeCharacter ¶
TypeCharacter injects a single Unicode codepoint.