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
- func FindDisplay(ctx context.Context) (*x11disp.Display, error)
- func KeysymRune(s Keysym) (rune, bool)
- func MacKeyFor(sym Keysym) (vk uint16, shift, ok bool)
- func MacKeyForEvdev(code uint16) (vk uint16, ok bool)
- func ViewOnly() bool
- type Button
- type DisplayGeometry
- type Injector
- func (x *Injector) Button(b Button, down bool) error
- func (x *Injector) Close() error
- func (x *Injector) KeyEvdev(code uint16, down bool) error
- func (x *Injector) KeySym(sym Keysym, down bool) error
- func (x *Injector) MoveBy(dx, dy int) error
- func (x *Injector) MoveTo(px, py int) error
- func (x *Injector) MoveToF(px, py float64) error
- func (x *Injector) NeedsShift(sym Keysym) bool
- func (x *Injector) Pointer() (int, int, error)
- func (x *Injector) PointerF() (float64, float64, error)
- func (x *Injector) ReleaseAll() error
- func (x *Injector) Scroll(dx, dy int) error
- func (x *Injector) TypeRune(r rune) error
- type Keysym
Constants ¶
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 ¶
FindDisplay is x11disp.Find with the host-facing error the screenshot service also gives when there is nothing to drive.
func KeysymRune ¶
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 ¶
MacKeyFor reports the macOS virtual key (kVK_*) that produces sym on US key positions, and whether Shift is part of it.
func MacKeyForEvdev ¶
MacKeyForEvdev reports the macOS virtual key at the position of the Linux evdev key code.
Types ¶
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 Open ¶
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) Close ¶
Close releases everything held, gives spare keycodes their empty mapping back once clients have had time to use them, and disconnects.
func (*Injector) KeyEvdev ¶
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 ¶
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 ¶
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) MoveToF ¶
MoveToF is MoveTo for fractional coordinates (rounded: X11 positions are whole pixels).
func (*Injector) NeedsShift ¶
NeedsShift reports whether the key producing sym must be combined with Shift (an upper-case letter, or "+" on a US layout).
func (*Injector) ReleaseAll ¶
ReleaseAll lets go of every key and button held through this Injector, where the pointer is.
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 ¶
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.