beetlebug

ztk::inject

Sends one input event to the window of a toplevel, through the same path as an event of the backend.

Synopsis

ztk::inject PATHNAME motion X Y ?-state MODS?
ztk::inject PATHNAME press BUTTON X Y ?-state MODS?
ztk::inject PATHNAME release BUTTON X Y ?-state MODS?
ztk::inject PATHNAME wheel X Y DELTA ?-state MODS?
ztk::inject PATHNAME key KEYSYM ?press|release? ?-state MODS?
ztk::inject PATHNAME text STRING ?-state MODS?

Arguments

Name Type Default Meaning
PATHNAME widget path A widget. The event goes to the window of its toplevel.
motion X Y form Moves the pointer. X and Y are logical pixels in the toplevel.
press BUTTON X Y form Presses a mouse button at the point.
release BUTTON X Y form Releases a mouse button at the point.
wheel X Y DELTA form Turns the wheel at the point. DELTA is as in <MouseWheel>: 120 is one step up.
key KEYSYM form press and release A key, by its keysym name or by one character. press or release sends only that half.
text STRING form Sends the text as a commit of an input method: a press and a release of the keysym of each character.
-state integer 0 The modifier bits, as -state of event generate. It goes at the end of each form.

The dispatcher does the hit test, the hover state, <Enter> and <Leave>, the grab, the focus, and the click count, as for a real event. So a press and a release on a button invoke it.

Return value

An empty string.

Errors

Error When
bad window path name "W" No widget has the name.
window "W" is not shown The toplevel has no window.
bad form "X": must be motion, press, release, wheel, key, or text An unknown form.
bad keysym "X" key with an unknown keysym.
bad key action "X": must be press or release key with a third word other than press or release.
the text is not valid UTF-8 text with bytes that are not UTF-8.
expected number but got "X" A coordinate that is no number.
wrong # args: should be "ztk::inject toplevel motion x y" A form with the wrong count of words. Each form has its own message.

Platforms

Platform Works
Windows Yes
macOS Yes
Linux, Wayland Yes
Linux, X11 Yes
Web Yes
iOS, Android Yes
Headless Yes

Example

set n 0
button .b -text Count -command {incr n}
pack .b -padx 20 -pady 20
update
set x [expr {[winfo x .b] + 5}]
set y [expr {[winfo y .b] + 5}]
ztk::inject . motion $x $y
ztk::inject . press 1 $x $y
ztk::inject . release 1 $x $y
set n

See also