Documentation
¶
Overview ¶
Package pointer says where the mouse pointer is and puts it somewhere else.
It exists because of a measured failure. An application that shows one display on another -- a viewer, a head-up surface, a desk of captured screens inside a pair of glasses -- gives a person a picture of somewhere the pointer can go and no way to get it there. Dragging the mouse across a display whose content is a capture of elsewhere means dragging it BLIND: the picture does not show where the pointer is, because the pointer is not on the display being captured. The way out, for the person who met this, was unplugging the glasses.
One key that puts the pointer on the screen somebody is looking at fixes that, and this is what such a key needs.
No permission, and no synthetic events ¶
Moving the pointer is CGWarpMouseCursorPosition, which is not an event: it asks the window server to put the cursor somewhere. It needs neither Accessibility nor Input Monitoring, and it works in a plain unbundled Go binary -- unlike CGEventPost, which is silently refused to one (measured in go-macos/hotkey, twice, with the evidence).
Nothing here presses a button or sends a keystroke, on purpose. A package that can move the pointer is a convenience; one that can click is a robot, and the two want different questions asked of them.
Coordinates ¶
Everything here is in CoreGraphics GLOBAL DISPLAY SPACE: pixels, origin at the TOP-LEFT of the main display, y growing DOWNWARDS, all displays in one arrangement. It is the space CGDisplayBounds and CGWarpMouseCursorPosition speak, and the space go-widgets/window reports its screens in.
It is NOT AppKit's. -[NSEvent mouseLocation] is bottom-left with y growing up, which is the other convention and half a screen out if the two are mixed. This package never touches AppKit, so there is one convention here and it is stated.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnsupported is returned by every entry point on non-darwin platforms. // The pointer of a window server is that window server's to move. ErrUnsupported = errors.New("pointer: unsupported on this platform (darwin only)") // ErrNoDisplay reports a display id the window server does not know, or one // that has gone away since the caller looked. It is an ordinary event with an // external panel and routine with a virtual one, which is why it is an error // and not a panic. ErrNoDisplay = errors.New("pointer: no such display") )
Errors reported by the package. They are stable and may be tested with errors.Is.
Functions ¶
func Displays ¶
Displays are the attached displays, in the window server's own order.
Offered because a caller that wants to know whether the id it is holding is still attached has otherwise to warp to it and see -- and because the answer says WHICH display, not merely how many, unlike the count-only call this wraps.
func MoveTo ¶
MoveTo puts the pointer at p.
It also re-associates the mouse with the cursor, which is not decoration. A warp leaves the two briefly disconnected -- the window server damps the mouse for about a quarter of a second so a physical nudge does not fight the jump -- and a person who warps the pointer and immediately moves the mouse finds it stuck. Saying "these are the same thing again" ends that.
func MoveToDisplay ¶
MoveToDisplay puts the pointer in the middle of one display.
It is the whole reason this package exists: an application showing a person a picture of another screen needs one gesture that means "bring the mouse here", and the display id is what such an application has.
Types ¶
type Point ¶
type Point struct {
X, Y float64
}
A Point is a place in CoreGraphics global display space: pixels, origin at the top-left of the main display, y growing downwards.
Floating point because that is what CoreGraphics uses, and because a display with a backing factor of two has half-pixels in this space that are whole pixels on the panel.
func Position ¶
Position is where the pointer is now.
Read from a fresh CGEvent rather than from AppKit, for two reasons that both matter: -[NSEvent mouseLocation] is bottom-left with y growing UP, which is the other convention and half a screen out if the two are mixed; and it needs an NSApplication, which a command-line tool asking where the mouse is has no business creating.
type Rect ¶
type Rect struct {
X, Y, W, H float64
}
A Rect is a rectangle in the same space: a display's bounds, chiefly.
func Bounds ¶
Bounds is a display's rectangle in global display space.
A display that is not there reports ErrNoDisplay rather than the empty rectangle CGDisplayBounds answers with, because "the origin, no size" is a place a caller can accidentally warp to.
func (Rect) Centre ¶
Centre is the middle of the rectangle.
It is where a pointer put "on a display" goes. The middle rather than a corner because a corner is shared with the next display along: half a pixel of rounding there and the pointer lands on the neighbour, which is the one place it must not.