Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Is it possible in perl to detect keyboard events like as "when keydown"

by perladdict (Chaplain)
on Apr 20, 2007 at 10:15 UTC ( #611115=perlquestion: print w/ replies, xml ) Need Help??
perladdict has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I am trying to write a wrapper on view creation tool, that is developed in perl,its an commandline tool.
For every new view creation i have to repeate several steps by selecting argument option.
On command prompt i have to select the option and ENTER key to proceed to the fallowing step,i don't know
how to do event based scripting in perl,is there any way in perl to record keyboard events.
For reference below is the tool i am using.
[ 1] [l] LOOK at views of a different user [ 2] [c] CREATE a New View [ 3] [s] SYNCHRONIZE a view from another region [ 4] [d] DELETE (remove) one of the views below [ 5] [q] QUIT (exit from viewmgt) [ 6] patilr__070413_test_view1 [ 7] patilr__070420_test2 Action [1 - 7, letter shortcuts] ==>
After selecting anyone option i have to press enterkey to proceed nextstep,
how can i capture keyevent in script,Can any one help me to find a solution for this.
Keep pouring your ideas.....

Comment on Is it possible in perl to detect keyboard events like as "when keydown"
Download Code
Re: Is it possible in perl to detect keyboard events like as "when keydown"
by naikonta (Curate) on Apr 20, 2007 at 10:53 UTC
    Well, while I don't do much screen-based applications, I would search on CPAN modules under the Term:: namespace.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Is it possible in perl to detect keyboard events like as "when keydown"
by jettero (Monsignor) on Apr 20, 2007 at 10:55 UTC

    I used to do it by hand using an ancient method from perlfaq5 (ish). These days, I use Term::ReadKey to read a single key.

    It appears the really ancient version has been replaced with a more modern version and below it is the Term::ReadKey I just suggested. Obviously someone is keeping these documents up to date.

    Hopefully few remember forking to /usr/bin/stty to enter passwords.

    UPDATE: I just realized you specifically asked for keyboard events. You might find a POE module that generates events on keystrokes, but I'm not sure which would be best. I have used various Wheels and Server modules. POE is fun.

    -Paul

Re: Is it possible in perl to detect keyboard events like as "when keydown"
by cdarke (Prior) on Apr 20, 2007 at 10:59 UTC
    Maybe I misunderstand, but if you are selecting an option then pressing ENTER, isn't that a read from stdin?
    chomp($option=<STDIN>)
    If that is not what you want, use Term::ReadKey, although you might need Win32::Console on Windoze.
    Or do you want to send keystrokes to the tool?
Re: Is it possible in perl to detect keyboard events like as "when keydown"
by jeanluca (Deacon) on Apr 20, 2007 at 11:39 UTC
    Although I get the impression that the answer you're looking for has been mentioned already, but if not maybe the post howto capture a keystroke can help you out!

    LuCa
Re: Is it possible in perl to detect keyboard events like as "when keydown"
by zentara (Archbishop) on Apr 20, 2007 at 13:49 UTC
    I'm not sure if you can extend this to "keydown/keyup", you may need Gtk2 (a gui) for that. But this is a commandline eventloop using GLib that will record keys.

    The nice thing about the Glib eventloop, is you can add timers, and filehandle watches to it, to accomplish most anything. If you seek more examples, google for "Glib keypress".

    #!/usr/bin/perl use warnings; use strict; use Glib; use Glib qw/TRUE FALSE/; # thanks to muppet for this use Term::ReadKey; $|++; ReadMode('cbreak'); my $main_loop = Glib::MainLoop->new; Glib::Idle->add( sub{ my $char; if (defined ($char = ReadKey(0)) ) { print "$char->", ord($char),"\n"; #process key presses here } return TRUE; #keep this going }); $main_loop->run; ReadMode('normal'); # restore normal tty settings __END__

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Is it possible in perl to detect keyboard events like as "when keydown"
by fmerges (Chaplain) on Apr 20, 2007 at 19:26 UTC

    Hi,

    You can also take a look at IO::Prompt.

    Regards,

    fmerges at irc.freenode.net
Re: Is it possible in perl to detect keyboard events like as "when keydown"
by su6esh.s (Initiate) on Dec 04, 2011 at 14:25 UTC
    am having the same problem please any one can help me???????
      No. Repeat after me, PERL IS NOT JAVASCRIPT, PERL IS NOT JAVASCRIPT, PERL IS NOT JAVASCRIPT.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-11-28 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (200 votes), past polls