Version 8 of event generate

Updated 2010-06-10 09:27:38 by ZhangWeiwu

event generate window event ?option value option value ...?

Generates a window event and arranges for it to be processed just as if it had come from the window system. Window gives the path name of the window for which the event will be generated; it may also be an identifier (such as returned by winfo id) as long as it is for a window in the current application. Event provides a basic description of the event, such as <Shift-Button-2> or <<Paste>>. If Window is empty the whole screen is meant, and coordinates are relative to the screen. Event may have any of the forms allowed for the sequence argument of the bind command except that it must consist of a single event pattern, not a sequence. Option-value pairs may be used to specify additional attributes of the event, such as the x and y mouse position; see EVENT FIELDS below. If the -when option is not specified, the event is processed immediately: all of the handlers for the event will complete before the event generate command returns. If the -when option is specified then it determines when the event is processed. Certain events, such as key events, require that the window has focus to receive the event properly.


How to "warp" the mouse pointer to some other position:

  event generate $window <Motion> -x $x -y $y -warp 1

How to simulate a mouse click?

If you wish to generate the mouse click event on a button, but not need to make the button visually look as if clicked, you don't need the following detail. Simply use "invoke" command of button widget would do.

If you do wish to have a visual feedback:

The following works for the <Motion>, but not for the clicking:

 package require Tk
 pack [canvas .c]
 set id [.c create rect 10 10 50 50 -fill red]
 .c bind $id <ButtonRelease-1> [list %W itemconfigure $id -fill green]
 raise .
 after 1000 {
    event generate .c <Motion> -x 30 -y 30 -warp 1
    event generate .c <Enter>
    event generate .c <ButtonPress-1>
    after 100
    event generate .c <ButtonRelease-1>
 }

Can anyone shine a light? RS 2009-05-27


LGT: to add -x 30 -y 30 to <ButtonPress-1> seems to solve the problem :

    event generate .c <ButtonPress-1> -x 30 -y 30

RS Yup, confirmed - thanks! I thought the <Motion> event should make it clear enough where the mouse points...

LGT: here is another way of doing :

    event generate .c <Enter>
    update
    set X [expr [winfo pointerx .] - [winfo rootx .]]
    set Y [expr [winfo pointery .] - [winfo rooty .]]   
    event generate .c <ButtonPress-1> -x $X -y $Y

'update' is needed otherwise the old coordinates still matter.

See also:


Tk syntax help - Category Command