Figure 15-8. Data synthesized by eventGenerate
Here's the input loop. Most of the characters in the string
"Hello Perl/Tk" are their own keysyms, but for
those that aren't, we provide a mapping through the hash
%keysysms.
foreach (split '', 'Hello Perl/Tk') {
$_ = $keysyms{$_} if exists $keysyms{$_};
$e->eventGenerate('<KeyPress>', -keysym => $_);
$mw->idletasks;
$mw->after(200);
}
After a short delay, we enter the Button's space, press it, and
release it. The release event invokes the Button's callback,
which prints "Hello Perl/Tk".
$mw->after(1000);
$b->eventGenerate('<Enter>');
$b->eventGenerate('<ButtonPress-1>');
$b->eventGenerate('<ButtonRelease-1>');
We create a virtual event using eventAdd. Once a
virtual event is defined, we must create an actual binding to trigger
the event. The following code creates the virtual event
<<Gromit>>. Notice that virtual event
names are surrounded by double angle brackets to distinguish them
from real event names.
The <<Gromit>> virtual event is bound
to the real event, <KeyPress>. Once defined,
we bind <<Gromit>> to the subroutine
look_for_gromit, which simply searches for the
string "Gromit" (in this case, from an Entry
widget).
We call bindDump and eventInfo
to display interesting binding and event information.
my $e = $mw->Entry->pack;
$e->focus;
$e->eventAdd('<<Gromit>>' => '<KeyPress>');
$e->bind('<<Gromit>>' => \&look_for_gromit);
&bindDump($e);
print $e->eventInfo, "\n";
sub look_for_gromit {
my $text = $_[0]->get;
print "Found Gromit in '$text'\n" if $text =~ /Gromit/i;
}
Figure 15-9. Searching for Gromit
As soon as we type the t and !
characters, look_for_gromit prints this:
Found Gromit in '123gROMit'
Found Gromit in '123gROMit!'
This is an excerpt from the bindDump output,
showing the Entry widget's instance bindings.
2. Binding tag '.entry' has these bindings:
<<Gromit>> : Tk::Callback=ARRAY(0x82d5160)
CODE(0x8270928)