O'Reilly logo

Advanced Perl Programming by Sriram Srinivasan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Event Bindings

An event binding associates a callback function with any type of event. You have already seen instances of event bindings—the button widget’s command property arranges for a user-defined procedure to be called on a mouse click, for example. The bind() command provides a more general (and hence low-level) access to the most fundamental events such as keyboard and mouse button presses and releases. (A mouse click is a press and release, so we are talking about really low-level events here.) Other “interesting” event types include mouse motion, the mouse pointer entering or leaving a window, and windows getting mapped or resized on the display. All widgets themselves rely on the bind method for their own functionality, and allow you to create extra bindings yourself. The bound procedure executes if the event you’re tracking happens inside that widget or is related to that widget (such as a window resize).

The syntax of bind is as follows:

$widget-->bind(event sequence, callback);

The event sequence is a string containing a sequence of basic events, with each basic event contained in angle brackets. Examples of event sequences are as follows:

"<a>"                   # Key "a" pressed (Control/shift/meta not
                        # pressed)
"<Control-a>            # Control and a pressed
"<Escape> <Control-a>"  # Two-event sequence
"<Button1>"             # Mouse button 1 clicked
"<Button1-Motion>"      # Mouse moves while Button 1 is down

A single event (within angle brackets) has the following generic syntax:

"<modifier-modifier...-modifier-type-detail ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required