in reply to Re^2: Tk bind KeyPress KeyRelease Problem
in thread Tk bind KeyPress KeyRelease Problem
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:
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".#!/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"; }
For example:
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).#!/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 } ); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Tk bind KeyPress KeyRelease Problem
by Anonymous Monk on Sep 13, 2004 at 22:34 UTC |