Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by perltux (Monk)
on Dec 26, 2012 at 02:24 UTC ( [id://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)?

Replies are listed 'Best First'.
Re: Tk::Button: How do I trigger a callback when button is pressed (as opposed to released)?
by golux (Chaplain) 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!
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-25 14:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found