Hi again, I think I have something that works. I couldn't give up on this, because something in my subconcious mind told me it had to be possible. It was just a question of "pulling it out" with all it's multiple feedback complexities. So the code below does it with a counter and 2 timers. The first timer will set and reset itself as the key auto repeats, and it will reset the counter to 0 after 300 ms. If the key is held down, the counter should never hit 0. The second timer, waits for the counter to hit 0, and will call the stop function, and reset the bindings. I have it setup with 300 and 400 milliseconds, and that seems to work on my machine. If you go too low, like to 50 and 100 ms, you will get a single repeat sneaking thru, before the counter can setup. So this timer has to be set higher than the repeat rate on your machine. I'm sure this code can be refined, but it's good enough for a "proof of concept".
#!/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; } #----------------------------------------

I'm not really a human, but I play one on earth. flash japh

In reply to Re: Tk bind KeyPress KeyRelease Problem by zentara
in thread Tk bind KeyPress KeyRelease Problem by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.