inject

package
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0 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 inject synthesises pointer and keyboard input on the host's desktop. It is the one implementation behind the desktop viewer (raw events streamed from a browser) and the input service (single actions from the CLI and the MCP server).

Backends: XTEST on Linux/X11, SendInput on Windows, Quartz events on macOS (CoreGraphics through purego, no cgo); other systems get an Injector whose Open fails with a clear error. An Injector is not safe for concurrent use; callers serialise. Close always releases every key and button the Injector still holds, so an aborted shortcut or drag never leaves the host with a stuck Ctrl or mouse button.

Coordinates are pixels of the virtual screen (all monitors), origin at its top-left on X11 and at the primary monitor's top-left on Windows, exactly as the screenshot service reports monitor geometry.

Index

Constants

View Source
const ViewOnlyEnv = "REMOHOP_DESKTOP_VIEW_ONLY"

ViewOnlyEnv is the host switch that refuses all input injection, for the desktop viewer and the input service alike.

Variables

This section is empty.

Functions

func FindDisplay

func FindDisplay(ctx context.Context) (*x11disp.Display, error)

FindDisplay is x11disp.Find with the host-facing error the screenshot service also gives when there is nothing to drive.

func KeysymRune

func KeysymRune(s Keysym) (rune, bool)

KeysymRune is the inverse of RuneKeysym for printable keysyms (Return and Tab give '\n' and '\t'); ok is false for function keys and the like.

func MacKeyFor

func MacKeyFor(sym Keysym) (vk uint16, shift, ok bool)

MacKeyFor reports the macOS virtual key (kVK_*) that produces sym on US key positions, and whether Shift is part of it.

func MacKeyForEvdev

func MacKeyForEvdev(code uint16) (vk uint16, ok bool)

MacKeyForEvdev reports the macOS virtual key at the position of the Linux evdev key code.

func ViewOnly

func ViewOnly() bool

ViewOnly reports whether the host owner turned input off with ViewOnlyEnv (any value but "" and "0").

Types

type Button

type Button uint8

Button is a mouse button, numbered like X11 core buttons.

const (
	Left    Button = 1
	Middle  Button = 2
	Right   Button = 3
	Back    Button = 8
	Forward Button = 9
)

Mouse buttons. Wheel notches are sent with Scroll, not as buttons.

type DisplayGeometry

type DisplayGeometry struct {
	// X, Y, Width, Height are the display's bounds in global points.
	X, Y, Width, Height float64
	// PixelWidth, PixelHeight are its size in pixels, as a screenshot of
	// it is.
	PixelWidth, PixelHeight int
	// Main marks the main display (menu bar; origin of the global space).
	Main bool
	// ID is the macOS CGDirectDisplayID (0 elsewhere).
	ID uint32
}

DisplayGeometry describes a display on hosts whose input coordinates are not screen pixels. On macOS the injector works in global display points (origin at the main display's top-left; displays left of or above it have negative coordinates), while screenshots are in pixels, two per point on a Retina display. Each display has its own scale.

func (DisplayGeometry) PixelToPoint

func (g DisplayGeometry) PixelToPoint(px, py int) (x, y float64)

PixelToPoint maps pixel (px, py) of the display, counted from its top-left, to the global point at the centre of that pixel, so that on a 2x display pixel 1 lands on the right half of point 0, exactly where a screenshot shows it.

func (DisplayGeometry) PointToPixel

func (g DisplayGeometry) PointToPixel(x, y float64) (px, py int)

PointToPixel maps a global point to the pixel of this display it falls in. The result lies outside 0..PixelWidth-1 (or -Height) when the point is on another display.

type Injector

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

Injector synthesises input through the XTEST extension, which X servers treat exactly like events from a physical device.

func NewX11

func NewX11(c *xgb.Conn) (*Injector, error)

NewX11 takes over an open connection (it is closed by Close, or here on error).

func Open

func Open(ctx context.Context, d *x11disp.Display) (*Injector, error)

Open connects to the X display d, or to the one x11disp finds when d is nil, and prepares to inject input there.

func (*Injector) Button

func (x *Injector) Button(b Button, down bool) error

Button presses or releases a mouse button where the pointer is.

func (*Injector) Close

func (x *Injector) Close() error

Close releases everything held, gives spare keycodes their empty mapping back once clients have had time to use them, and disconnects.

func (*Injector) KeyEvdev

func (x *Injector) KeyEvdev(code uint16, down bool) error

KeyEvdev presses or releases a physical key given by its Linux evdev code; the host's own layout decides what it means. X servers using evdev/libinput keymaps number keys as evdev code + 8.

func (*Injector) KeySym

func (x *Injector) KeySym(sym Keysym, down bool) error

KeySym presses or releases the key producing sym, without adding Shift (see NeedsShift). A release goes to the keycode the press used.

func (*Injector) MoveBy

func (x *Injector) MoveBy(dx, dy int) error

MoveBy moves the pointer by dx, dy pixels from where it is: XTEST's relative motion, which the server handles like a mouse's own movement (pointer acceleration included) and which clients that grab the pointer for relative input (games, 3D viewports) see as motion.

func (*Injector) MoveTo

func (x *Injector) MoveTo(px, py int) error

MoveTo puts the pointer at x, y on the virtual screen.

func (*Injector) MoveToF

func (x *Injector) MoveToF(px, py float64) error

MoveToF is MoveTo for fractional coordinates (rounded: X11 positions are whole pixels).

func (*Injector) NeedsShift

func (x *Injector) NeedsShift(sym Keysym) bool

NeedsShift reports whether the key producing sym must be combined with Shift (an upper-case letter, or "+" on a US layout).

func (*Injector) Pointer

func (x *Injector) Pointer() (int, int, error)

Pointer reports where the pointer is on the virtual screen.

func (*Injector) PointerF

func (x *Injector) PointerF() (float64, float64, error)

PointerF is Pointer as floats.

func (*Injector) ReleaseAll

func (x *Injector) ReleaseAll() error

ReleaseAll lets go of every key and button held through this Injector, where the pointer is.

func (*Injector) Scroll

func (x *Injector) Scroll(dx, dy int) error

Scroll turns the wheel by whole notches: positive dy is down, positive dx is right.

func (*Injector) TypeRune

func (x *Injector) TypeRune(r rune) error

TypeRune types one character (see RuneKeysym) with a press and a release, holding Shift around it if the layout needs that.

type Keysym

type Keysym uint32

Keysym identifies the symbol a key produces, with X11 keysym values on every platform (Windows translates them to virtual keys). Unicode characters outside Latin-1 use the X convention 0x01000000 + code point.

const (
	KeysymReturn   Keysym = 0xff0d
	KeysymTab      Keysym = 0xff09
	KeysymShiftL   Keysym = 0xffe1
	KeysymShiftR   Keysym = 0xffe2
	KeysymControlL Keysym = 0xffe3
	KeysymAltL     Keysym = 0xffe9
	KeysymSuperL   Keysym = 0xffeb
	KeysymLevel3   Keysym = 0xfe03 // ISO_Level3_Shift (AltGr)

)

Keysyms the backends need to know by name.

func RuneKeysym

func RuneKeysym(r rune) (Keysym, bool)

RuneKeysym returns the keysym that types r: Latin-1 characters are their own keysyms, newline and tab are Return and Tab, anything else printable is a Unicode keysym. It reports false for control characters and invalid runes, which cannot be typed.

Jump to

Keyboard shortcuts

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