Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^5: Tk: Binding Keys to events

by rcseege (Pilgrim)
on Oct 04, 2006 at 00:05 UTC ( [id://576211]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Tk: Binding Keys to events
in thread Tk: Binding Keys to events

Thanks for providing the small functional example! You might consider doing some variation of this:

use Tk; use strict; my $mw = MainWindow->new; my $c = $mw->Scrolled('Canvas', -width => 200, -height => 200, -background =>'blue', -scrollregion => [ 0, 0, 500, 500 ] )->pack(qw/-expand 1 -fill both/); ## Bind Both Press and Release events my $keyHR = {}; $mw->bind('<KeyPress-Up>', [\&ButtonPress, "up", $keyHR]); $mw->bind('<KeyPress-Down>', [\&ButtonPress, "down", $keyHR]); $mw->bind('<KeyPress-Right>', [\&ButtonPress, "right", $keyHR]); $mw->bind('<KeyPress-Left>', [\&ButtonPress, "left", $keyHR]); $mw->bind('<KeyRelease-Up>', [\&ButtonRelease, "up", $keyHR]); $mw->bind('<KeyRelease-Down>', [\&ButtonRelease, "down", $keyHR]); $mw->bind('<KeyRelease-Right>', [\&ButtonRelease, "right", $keyHR]); $mw->bind('<KeyRelease-Left>', [\&ButtonRelease, "left", $keyHR]); my $rect = $c->createRectangle(100, 100, 150, 150, -fill => 'yellow'); MainLoop; sub ButtonRelease { my ($w, $dirKey, $keyHR) = @_; $keyHR->{$dirKey} = 0; Move($keyHR); } sub ButtonPress { my ($w, $dirKey, $keyHR) = @_; $keyHR->{$dirKey} = 1; Move($keyHR); } sub Move { my $keyHR = shift; my $speed = 5; my $x = 0; $x += $speed if $keyHR->{right}; $x -= $speed if $keyHR->{left}; my $y = 0; $y += $speed if $keyHR->{down}; $y -= $speed if $keyHR->{up}; $c->move($rect, $x, $y); }
Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://576211]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-25 06:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found