Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Tk::Button: How do I trigger a callback when button is pressed (as opposed to released)?

by perltux (Scribe)
on Dec 26, 2012 at 02:24 UTC ( #1010305=perlquestion: print w/ replies, xml ) Need Help??
perltux has asked for the wisdom of the Perl Monks concerning the following question:

With Tk::Button the -command callback is invoked when the button is released. How do I additionally add a callback that gets invoked when the button is pressed?

Basically my aim is to make a sort of on-screen piano keyboard where pressing each Tk::Button widget with the mouse sends a MIDI 'note on' command and subsequently releasing the mouse button sends a MIDI 'note-off' command to a synthesizer.

Comment on Tk::Button: How do I trigger a callback when button is pressed (as opposed to released)?
Re: Tk::Button: How do I trigger a callback when button is pressed (as opposed to released)?
by Anonymous Monk on Dec 26, 2012 at 03:25 UTC
      Thanks, I did actually look at bindings but I couldn't figure out how to use them for this situation. An example code snippet for this specific situation would be most appreciated.
Re: Tk::Button: How do I trigger a callback when button is pressed (as opposed to released)?
by golux (Pilgrim) on Dec 26, 2012 at 04:31 UTC
    Hi perltux,

    Use Button-1 for button-down events (as opposed to ButtonRelease-1 for button-up events). For example:

    #!/usr/bin/perl -w use strict; use warnings; use Tk; my $mw = new MainWindow(-title => 'Button press example'); my $bt = $mw->Button(-bg => '#ffefb5', -text => 'Press Me'); my $txt = $mw->Text(-bg => 'white'); $bt->pack($txt); $bt->bind('<Button-1>' => sub { button_down($txt) }); $bt->bind('<ButtonRelease-1>' => sub { button_up($txt) }); Tk::MainLoop; sub button_down { my ($text) = @_; my $time = localtime(time()); $text->insert("end", "$time: Button Down\n"); } sub button_up { my ($text) = @_; my $time = localtime(time()); $text->insert("end", "$time: Button Up\n"); }
    say  substr+lc crypt(qw $i3 SI$),4,5
      Thanks, that works great!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1010305]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2015-07-06 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (71 votes), past polls