"What do you mean by "doesn't (sic) work"?

Well I think the OP wanted to turn a key into a switch which is on as long as it is held down, and is off when it is released. The code I posted will still "repeat" on linux, ( I hav'nt tried it on Windows). That is if you hold the key down, you will get a succession of "pressed-released events". If the OP had a binding to 'release' , then he would be repeatedly starting-stopping.

Just for fun, and because I think this could be useful in my "bag-o-tricks", I tried a few more things, but was still unsuccessful. For example:

#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new; $mw->bind("<Key>", sub { &pressed } ); $mw->bind("<KeyRelease>", sub { &released } ); MainLoop; sub pressed{ print 'Key: ', ${Ev('K')}, " pressed\n"; $mw->bind("<Key>", sub { } ); $mw->bind("<KeyRelease>", sub { } ); } sub released{ print 'Key: ', ${Ev('K')}, " released\n"; }
This code above will break the "auto-repeat" of the X-windows, but I can't seem to find a way to "re-enable the key bindings" when the key is actually released. I tried adding timers, which watch the output, expecting a succession of "pressed" events, and if the output stops, then call the "stop_function".

For example:

#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new; $mw->bind("<Key>", sub { &pressed } ); MainLoop; sub pressed{ print 'Key: ', ${Ev('K')}, " pressed\n"; $mw->bind("<Key>", sub { } ); $mw->bind("<KeyRelease>", sub {&pressed } ); }
This will just print a succession of "pressed" as long as the key is held down, and will stop when the key is released. So I thought about opening a FH to \$tempvar, and writing the output to it, then have a timer, truncate FH after a time period. If the length($tempvar) == 0, then I know the key was released, and can call the stop_function. But the timers eluded me, because either I spawned multiple timers, or the timing of the timers was funky, and unusable. But as of now I think this is the only method that will work, but it depends on what the "internal repeat key rate" is on your system. So after all that, I figure, it would be better to take a different approach, like have 1 key to initiate, and a differently named key to stop. (Which is normally how it's done).

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

In reply to Re^3: 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.