home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _47ab9748ce01a4c73a62e31109d3f6b9 < prev    next >
Encoding:
Text File  |  2004-06-01  |  2.7 KB  |  60 lines

  1. use strict;
  2.  
  3. sub virtevents1 {
  4.  
  5.     my( $demo ) = @_;
  6.  
  7.     my $mw = $MW->WidgetDemo(
  8.         -name             => $demo,
  9.         -text             => [ "This demonstration shows how you can use keysyms (keyboard symbols) to programmatically synthesize events that simulate a person typing on the keyboard. To learn about keyboard keysyms, run to previous demonstration, \"Show keyboard symbols\".
  10.  
  11. A virtual event named <<pTkRules>> is defined that is activated by pressing the \"caps lock\" key (go ahead, press \"caps lock\"). A callback is bound to that virtual event - the callback synthesizes physicals events that \"type\" into the Entry widget displayed below.  Pressing the \"Synthesize\" Button calls eventGenerate(), which synthesizes the virtual event <<pTkRules>> directly.
  12.  
  13. Warning:  it's easy to make this demonstration recurse indefinitely because synthesized physical events behave just like the real thing. So, it's possible for the <<pTkRules>> callback to eventGenerate() the keysym that activates the <<pTkRules>> virtual event, which invokes the <<pTkRules>> callback to eventGenerate() the keysym that activates the <<pTkRules>> virtual event, which ...", -wraplength => '6i' ],
  14.         -title            => 'Simulate KeyPress events.',
  15.         -iconname         => 'vevents1',
  16.     );
  17.  
  18.     # Define a virtual event - <<pTkRules>> - that is activated when
  19.     # the physical event - pressing the "caps lock" key - occurs. 
  20.  
  21.     $mw->eventAdd( qw/ <<pTkRules>> <Caps_Lock> / );
  22.  
  23.     # Alphabetics are their own keysyms.  The %keysyms hash maps other
  24.     # characters to their keysym string. To see the keysyms associated
  25.     # with keyboard characters run the previous widget demonstration.
  26.  
  27.     my %keysyms = (' ' => 'space', '/' => 'slash', '!' => 'exclam' );
  28.  
  29.     # Create an Entry widget for a person or this program to type into.
  30.     # The Button explicitly generates the virtual event.
  31.  
  32.     my $e = $mw->Entry->pack;
  33.     my $b = $mw->Button(
  34.     -command => sub { $mw->eventGenerate( '<<pTkRules>>' ) },
  35.         -text    => 'Synthesize <<pTkRules>>',
  36.     )->pack;
  37.  
  38.     # Now bind the virtual event to a callback that "types" for us.
  39.     
  40.     $mw->bind( qw/ <<pTkRules>> / => sub {
  41.  
  42.     # This subroutine is invoked whenever the "caps lock" key is
  43.     # pressed or the virtual event <<pTkRules>> is programatically
  44.     # generated via eventGenerate.
  45.  
  46.     $e->focus;
  47.     $mw->update;
  48.     my $string_to_type = 'Perl/Tk rules!';
  49.  
  50.     foreach ( split '', $string_to_type ) {
  51.         $_ = $keysyms{$_} if exists $keysyms{$_};
  52.         $e->eventGenerate( '<KeyPress>', -keysym => $_ );
  53.         $mw->idletasks;
  54.         $mw->after( 100 );
  55.  
  56.     } # end sub type characters
  57.     } );
  58.  
  59. } # end virtevents1
  60.