in reply to Tk bind KeyPress KeyRelease Problem
#!/usr/bin/perl use warnings; use strict; use Tk; my $counter = 0; my $timer; my $timer1; my $timerrunning = 0; my $mw = MainWindow->new; $mw->geometry('+250+250'); $mw->bind("<Key>", sub { &pressed } ); $mw->bind("<KeyRelease>", sub { } ); MainLoop; #----------------------------------- sub pressed{ my($widget) = @_; my $e = $widget->XEvent; # get reference to X11 event structure if($timerrunning == 1){ $timer->cancel; &start_timer} else{&start_timer} $counter++; print "$counter\n"; &do_function( $e->K.'-pressed' ); } #----------------------------------- sub start_timer{ $timerrunning = 1; $counter++; $timer = $mw->after(300, sub{$counter = 0; $timerrunning = 0; &start_timer1; $timer->cancel; }); } #------------------------------------- sub start_timer1{ $timer1 = $mw->after(400, sub { if($counter == 0){&stop_function} }); } #-------------------------------------- sub do_function{ my $key = shift; print "$key\n"; } #---------------------------------------- sub stop_function{ print "Stopping\n"; $mw->bind("<Key>", sub { &pressed } ); $timerrunning = 0; $counter = 0; } #----------------------------------------
|
|---|