[ Team LiB ] Previous Section Next Section

Generating Events

Your application can use the event generate command to programmatically generate events, in essence emulating user interaction. You can generate either standard windowing events or virtual events. However, you can generate events only for the current application; you can't send events to other applications running on your system. (In other words, you can't use the event generate command to have your application control another application.)

The first argument to event generate is the target widget for the event. You may provide either the path name of the widget, or the window identifier (such as returned by winfo id) as long as it is for a window in the current application.

The second argument is an event specification, using the same syntax as for creating event bindings. (See "Event Syntax" on page 439.) However, you can't generate an event sequence (such as <KeyPress-Escape><KeyPress-a>), only single events.

As an example, the following command delivers a ButtonPress-3 event to a widget:

event generate .b <ButtonPress-3>

graphics/common_icon.gif

A widget must have focus to receive key events.


Remember that a widget must have keyboard focus to receive KeyPress or KeyRelease events. You can use the focus command to assign keyboard focus to a widget:

focus .e1
event generate .e1 <KeyPress-a>

The event generate command also accepts options to specify additional attributes of the event, such as the x and y mouse position. Table 29-4 lists the event generate options. Of note is the -warp option, added in Tk 8.3. If you provide a -warp value of True, then the mouse pointer moves to the x and y coordinates of the generated event; otherwise, the mouse pointer remains at its current location. For example, the following commands moves the mouse pointer to the point 10,20 relative to the top-left corner of the main window:

event generate . <Motion> -x 10 -y 20 -warp 1
    [ Team LiB ] Previous Section Next Section