input

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 17 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package input carries keyboard, mouse, and gamepad input from the client to the host: a fixed binary wire format, key code tables, and host injectors.

Index

Constants

View Source
const (
	PadDPadUp        = gamepad.ButtonDPadUp
	PadDPadDown      = gamepad.ButtonDPadDown
	PadDPadLeft      = gamepad.ButtonDPadLeft
	PadDPadRight     = gamepad.ButtonDPadRight
	PadStart         = gamepad.ButtonStart
	PadBack          = gamepad.ButtonBack
	PadLeftThumb     = gamepad.ButtonLeftThumb
	PadRightThumb    = gamepad.ButtonRightThumb
	PadLeftShoulder  = gamepad.ButtonLeftShoulder
	PadRightShoulder = gamepad.ButtonRightShoulder
	PadGuide         = gamepad.ButtonGuide
	PadA             = gamepad.ButtonA
	PadB             = gamepad.ButtonB
	PadX             = gamepad.ButtonX
	PadY             = gamepad.ButtonY
)

Gamepad buttons use the XInput bit layout, plus Guide.

View Source
const (
	KeyA uint16 = 0x04 + iota
	KeyB
	KeyC
	KeyD
	KeyE
	KeyF
	KeyG
	KeyH
	KeyI
	KeyJ
	KeyK
	KeyL
	KeyM
	KeyN
	KeyO
	KeyP
	KeyQ
	KeyR
	KeyS
	KeyT
	KeyU
	KeyV
	KeyW
	KeyX
	KeyY
	KeyZ
	Key1
	Key2
	Key3
	Key4
	Key5
	Key6
	Key7
	Key8
	Key9
	Key0
	KeyEnter
	KeyEscape
	KeyBackspace
	KeyTab
	KeySpace
	KeyMinus
	KeyEqual
	KeyLeftBracket
	KeyRightBracket
	KeyBackslash
	KeyNonUSHash
	KeySemicolon
	KeyQuote
	KeyGrave
	KeyComma
	KeyPeriod
	KeySlash
	KeyCapsLock
	KeyF1
	KeyF2
	KeyF3
	KeyF4
	KeyF5
	KeyF6
	KeyF7
	KeyF8
	KeyF9
	KeyF10
	KeyF11
	KeyF12
	KeyPrintScreen
	KeyScrollLock
	KeyPause
	KeyInsert
	KeyHome
	KeyPageUp
	KeyDelete
	KeyEnd
	KeyPageDown
	KeyRight
	KeyLeft
	KeyDown
	KeyUp
	KeyNumLock
	KeyKPDivide
	KeyKPMultiply
	KeyKPMinus
	KeyKPPlus
	KeyKPEnter
	KeyKP1
	KeyKP2
	KeyKP3
	KeyKP4
	KeyKP5
	KeyKP6
	KeyKP7
	KeyKP8
	KeyKP9
	KeyKP0
	KeyKPDecimal
	KeyNonUSBackslash
	KeyApplication
	KeyPower
	KeyKPEqual
	KeyF13
	KeyF14
	KeyF15
	KeyF16
	KeyF17
	KeyF18
	KeyF19
	KeyF20
	KeyF21
	KeyF22
	KeyF23
	KeyF24
)

Key codes are USB HID usages from the Keyboard/Keypad page (0x07), the one key vocabulary every client and host maps through.

View Source
const (
	KeyMute           uint16 = 0x7F
	KeyVolumeUp       uint16 = 0x80
	KeyVolumeDown     uint16 = 0x81
	KeyKPComma        uint16 = 0x85
	KeyInternational1 uint16 = 0x87 // JIS Ro
	KeyInternational3 uint16 = 0x89 // JIS Yen
	KeyInternational4 uint16 = 0x8A // JIS Henkan
	KeyInternational5 uint16 = 0x8B // JIS Muhenkan
	KeyLang1          uint16 = 0x90 // Hangul, Mac Kana
	KeyLang2          uint16 = 0x91 // Hanja, Mac Eisu
	KeyLeftControl    uint16 = 0xE0
	KeyLeftShift      uint16 = 0xE1
	KeyLeftAlt        uint16 = 0xE2
	KeyLeftGUI        uint16 = 0xE3
	KeyRightControl   uint16 = 0xE4
	KeyRightShift     uint16 = 0xE5
	KeyRightAlt       uint16 = 0xE6
	KeyRightGUI       uint16 = 0xE7
)
View Source
const (
	ModLeftControl uint8 = 1 << iota
	ModLeftShift
	ModLeftAlt
	ModLeftGUI
	ModRightControl
	ModRightShift
	ModRightAlt
	ModRightGUI
)

Modifier bits, in HID boot-report order.

View Source
const (
	BackendUinput = "uinput"
	BackendXTest  = "xtest"
	BackendPortal = "portal"
)

Backend names of the Linux injector.

View Source
const InjectMarker = 0x504C4E4B // "PLNK"

InjectMarker tags the keys a host injects (dwExtraInfo on Windows, the event source's user data on macOS), so that a client capturing the system's keys on the same machine leaves them alone rather than sending them back to the host.

View Source
const MaxEventSize = 16

MaxEventSize is the largest encoded event in bytes.

View Source
const MaxGamepads = 16
View Source
const WheelNotch = 120

WheelNotch is one detent of a standard mouse wheel; finer values come from high-resolution wheels and trackpads.

Variables

View Source
var (
	ErrShortBuffer = errors.New("input: short buffer")
	ErrInvalid     = errors.New("input: invalid event")
)
View Source
var ErrUnsupported = errors.New("input: unsupported on this host")

ErrUnsupported is returned for events the host cannot inject, such as gamepads with no virtual controller driver or host-bound rumble.

Functions

func EvdevFromHID added in v0.2.0

func EvdevFromHID(usage uint16) (uint16, bool)

EvdevFromHID maps a HID usage to a Linux evdev key code.

func HIDFromEvdev added in v0.2.0

func HIDFromEvdev(code uint16) (uint16, bool)

HIDFromEvdev maps a Linux evdev key code to a HID usage.

func HIDFromMac

func HIDFromMac(keyCode uint16) (uint16, bool)

HIDFromMac maps a macOS virtual key code to a HID usage.

func HIDFromWindows

func HIDFromWindows(vk uint16, extended bool) (uint16, bool)

HIDFromWindows maps a Windows virtual-key code and its extended-key flag to a HID usage. When the exact pair is unknown, the flag is ignored, since APIs disagree on it for some keys (NumLock, Pause).

func IsSecureAttention

func IsSecureAttention(ev Event) bool

IsSecureAttention reports whether ev is Delete going down with Ctrl and Alt held, on either side: Ctrl+Alt+Del.

func KeyByName

func KeyByName(name string) (uint16, bool)

KeyByName is the HID usage of a key by its chord name, in any case.

func KeyName

func KeyName(usage uint16) string

KeyName is the chord name of the key with this HID usage, "" for a key chords cannot name, modifiers among them.

func MacFromHID

func MacFromHID(usage uint16) (uint16, bool)

MacFromHID maps a HID usage to a macOS virtual key code.

func SendSAS

func SendSAS() error

SendSAS is Windows' secure attention sequence; other systems have none.

func SwapsCmdCtrl

func SwapsCmdCtrl(clientOS, hostOS string) bool

SwapsCmdCtrl reports whether a Cmd and Ctrl swap between a client and a host on these GOOS values does anything: one is macOS and the other not.

func WindowsFromHID

func WindowsFromHID(usage uint16) (vk uint16, extended bool, ok bool)

WindowsFromHID maps a HID usage to a Windows virtual-key code and whether the key must be sent as an extended key.

Types

type Button

type Button uint8
const (
	ButtonLeft Button = iota + 1
	ButtonRight
	ButtonMiddle
	ButtonX1
	ButtonX2
)

type Event

type Event struct {
	Type     Type
	Key      KeyEvent
	MouseAbs MouseMoveAbs
	MouseRel MouseMoveRel
	Button   MouseButton
	Wheel    MouseWheel
	Gamepad  GamepadState
	Rumble   RumbleEvent
}

Event is one input event of any Type. Only the field matching Type is meaningful; a tagged struct rather than an interface keeps encoding and decoding free of allocations.

func CtrlAltDel

func CtrlAltDel(held uint8) []Event

CtrlAltDel is the key events of Ctrl+Alt+Del pressed and let go, over what held already has down, which stays down.

func SwapCmdCtrl

func SwapCmdCtrl(ev Event) Event

SwapCmdCtrl is ev with Ctrl and the GUI key (Cmd, Windows) traded, on each side: the key itself and the modifiers it carries.

func (*Event) Decode

func (e *Event) Decode(src []byte) (int, error)

Decode reads one event from the start of src into e and returns the bytes consumed, so several events can share one datagram. It rejects unknown types, unknown flag bits, and out-of-range values.

func (*Event) Encode

func (e *Event) Encode(dst []byte) (int, error)

Encode writes e to dst in little-endian wire form and returns the bytes written: one type byte, then the payload.

type GamepadState

type GamepadState struct {
	Index                     uint8
	Buttons                   uint32
	LeftTrigger, RightTrigger uint8
	LeftX, LeftY              int16
	RightX, RightY            int16
}

GamepadState is the full state of one controller. Stick axes are signed with positive X right and positive Y up, as in XInput.

type Held

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

Held tracks the keys and mouse buttons a host injected down and not up, to let go of them when the client that pressed them is gone: a client that drops mid Alt-Tab must not leave Alt down on the host.

func (*Held) KeyDown

func (h *Held) KeyDown(usage uint16) bool

KeyDown reports whether the key with usage is held.

func (*Held) Modifiers

func (h *Held) Modifiers() uint8

Modifiers is the modifier keys held, as Mod* bits.

func (*Held) Note

func (h *Held) Note(ev Event)

Note records an event the host injected.

func (*Held) Release

func (h *Held) Release() []Event

Release forgets what is held and returns the events that let go of it: buttons, then keys, then modifiers, each key event carrying the modifiers still down after it.

type Injector

type Injector interface {
	Inject(ev Event) error
	Close() error
}

Injector replays client input on the host. Implementations post real OS events; tests must not call them unless PLANCK_LIVE_INPUT_TEST=1.

type KeyEvent

type KeyEvent struct {
	Usage     uint16 // HID usage, page 0x07
	Pressed   bool
	Modifiers uint8 // Mod* bits held once this event has taken effect
	// SystemKeyCapture is set when the client captured a key combination
	// its own OS would otherwise act on (Cmd-Tab, Alt-Tab, the Windows key).
	SystemKeyCapture bool
}

KeyEvent is one key going down or up.

type LinuxInjector added in v0.2.0

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

LinuxInjector injects keyboard and mouse input on a Linux host through /dev/uinput, X11 XTest or the RemoteDesktop portal, whichever opens first (see backendOrder), and plugs gamepads into virtual controllers.

func NewInjector added in v0.2.0

func NewInjector() (*LinuxInjector, error)

NewInjector opens the first injection backend that works. Set PLANCK_INJECT_BACKEND to uinput, xtest or portal to use only that one.

func (*LinuxInjector) Backend added in v0.2.0

func (l *LinuxInjector) Backend() string

Backend names the stack in use: BackendUinput, BackendXTest or BackendPortal.

func (*LinuxInjector) Close added in v0.2.0

func (l *LinuxInjector) Close() error

Close destroys the virtual devices and closes the controllers.

func (*LinuxInjector) Gamepads added in v0.2.0

func (l *LinuxInjector) Gamepads() (*gamepad.VirtualPads, error)

Gamepads opens the virtual controllers on first use, for slot management and rumble. A failed open is kept.

func (*LinuxInjector) Inject added in v0.2.0

func (l *LinuxInjector) Inject(ev Event) error

func (*LinuxInjector) SetGamepadType added in v0.2.0

func (l *LinuxInjector) SetGamepadType(t gamepad.PadType)

SetGamepadType sets what virtual controllers appear as.

func (*LinuxInjector) SetTarget added in v0.2.0

func (l *LinuxInjector) SetTarget(display, desktop Rect)

SetTarget makes absolute moves cover display, within the desktop of every display. A zero display goes back to the primary one.

type MouseButton

type MouseButton struct {
	Button  Button
	Pressed bool
}

type MouseMoveAbs

type MouseMoveAbs struct{ X, Y uint16 }

MouseMoveAbs is a pointer position normalized over the streamed display: 0 is the left or top edge and 65535 the right or bottom edge.

type MouseMoveRel

type MouseMoveRel struct{ DX, DY int16 }

type MouseWheel

type MouseWheel struct{ DX, DY int16 }

MouseWheel scrolls in WheelNotch units; positive DY scrolls up (away from the user) and positive DX scrolls right.

type Rect

type Rect struct{ X, Y, W, H int }

Rect is a display's place on the host's desktop, in physical pixels.

type RumbleEvent

type RumbleEvent struct {
	Index             uint8
	LowFreq, HighFreq uint16
	DurationMs        uint16
}

RumbleEvent travels host to client: motor strengths for one controller.

type SendTracer added in v0.2.1

type SendTracer interface {
	TraceSends(on func() bool, f func(SentMouse))
}

SendTracer is an injector that reports each mouse event it posts to f while on reports true, on the goroutine that injects, for input traces. on is called for each event, so it should be an atomic load. Set it before the first Inject.

type SentMouse added in v0.2.1

type SentMouse struct {
	Flags     uint32 `json:"flags"`
	DX        int32  `json:"dx"`
	DY        int32  `json:"dy"`
	MouseData uint32 `json:"mouse_data"`
	Err       string `json:"err,omitempty"`
}

SentMouse is one mouse event an injector posted, as the system call took it: SendInput's MOUSEINPUT on Windows.

type Targeter

type Targeter interface {
	SetTarget(display, desktop Rect)
}

Targeter is an injector whose absolute moves can cover one display of a desktop of several: display within desktop, the bounds of every display together. A zero display covers the primary display.

type Type

type Type uint8
const (
	TypeKey Type = iota + 1
	TypeMouseMoveAbs
	TypeMouseMoveRel
	TypeMouseButton
	TypeMouseWheel
	TypeGamepad
	TypeRumble
)

Jump to

Keyboard shortcuts

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