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

Binding keys to events?

by tamaguchi (Pilgrim)
on Aug 02, 2006 at 11:22 UTC ( [id://565203]=perlquestion: print w/replies, xml ) Need Help??

tamaguchi has asked for the wisdom of the Perl Monks concerning the following question:

I wonder which is the standard way to bind keys to events in perl? Suppose I would like the program to write: "You have pressed key 'K'" when I have pressed key 'K'. How should I do this?
Thank you for any help.

Replies are listed 'Best First'.
Re: Binding keys to events?
by rodion (Chaplain) on Aug 02, 2006 at 11:31 UTC
Re: Binding keys to events?
by johngg (Canon) on Aug 02, 2006 at 13:20 UTC
    I would guess from one of your previous posts that you are asking in the context of Tk. You can do something like this

    $widget->bind(q{<KeyPress>}, \&callback);
    and you can get access to the event to find out which key was pressed in the callback() routine. Have a look at the Tk::bind and Tk::Event man pages for more info.

    Cheers,

    JohnGG

Re: Binding keys to events?
by zentara (Archbishop) on Aug 02, 2006 at 13:21 UTC
    Here are a couple of examples, including a Tk example. GUI's will not need Term::Readkey and the cumbersome while(1) loop.
    #!/usr/bin/perl use Term::ReadKey; #passing ReadKey() an argument of -1 to indicate not to block: ReadMode('cbreak'); while(1){ my $char; if (defined ($char = ReadKey(0)) ) { print "$char->", ord($char),"\n"; # input was waiting and i +t was $char } else { # no input was waiting } } ReadMode('normal'); # restore normal tty settings
    #!/usr/bin/perl use warnings; use strict; use Tk; my $down = 0; my $mw = MainWindow->new; $mw->bind("<Key>", sub { &pressed } ); $mw->bind("<KeyRelease>", sub { &released } ); MainLoop; sub pressed{ my($widget) = @_; my $e = $widget->XEvent; # get reference to X11 event structure my $binding = 'Character = ' . $e->N . ', keysym = ' . $e->K . '.'; print "$binding\n"; print $e->K," pressed\n"; } sub released{ my($widget) = @_; my $e = $widget->XEvent; # get reference to X11 event structure my $binding = 'Character = ' . $e->N . ', keysym = ' . $e->K . '.'; print "$binding\n"; print $e->K," released\n"; }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Binding keys to events?
by HuckinFappy (Pilgrim) on Aug 02, 2006 at 13:39 UTC
    Use IO::Prompt
    ( my $_prompt = <<END_OF_PROMPT ) =~ s/^\s+?\|//gm; | You have encountered a Gru, what would you like to do? | Do you want to: | [1] Run away | [2] Fight | [3] Try to talk your way out of this | [4] Apply salt to your leg and let him eat you | [q] Quit | Please choose [ 1-4,Q ]: END_OF_PROMPT my $_response = prompt $_prompt, -one_char, -require => { "Must be one of 1-4 or 'Q'\n$_prompt" => qr/[1-4q]/smix };

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-03-29 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found