Here is the basic idea of how i would do it..... but it uses an event loop system
## from previous posts ############### I have plenty of examples for Gtk2 windows, and capturing keyboard events, but I hav'nt found anything for Glib alone, for use in commandline loops. I can put Glib::IO->add_watch (fileno 'STDIN', [qw/in/], \&watch_callback); and watch what is input on STDIN, but is that the best way? Thanks, zentara Re: Does the Glib mainloop have the ability to capture keypresses? by muppet I took that approach in gish, where i faked out the Tk portion of Readline. http://asofyet.org/muppet/software/gtk2-perl/gish.html But it is important that you set up STDIN properly to give you all the characters pressed, instead of using line-buffering. Readline does that for you... in your own app, you'll have to use the termios + stuff from the POSIX module. </muppet> me....... I came up with 2 different versions. The first will trigger the callba +ck on an enter press, to take whole line commands. #!/usr/bin/perl use warnings; use strict; use Glib; use Glib qw/TRUE FALSE/; use FileHandle; #accepts enter to provide input my $fh_in = FileHandle->new(); $fh_in = *STDIN{IO}; + my $main_loop = Glib::MainLoop->new; Glib::IO->add_watch (fileno 'STDIN', [qw/in/], \&watch_callback); $main_loop->run; ###################################### sub watch_callback { while (<$fh_in>) { + my $line = $_; if (defined($line) and $line ne ''){ print $line; } } return 0; } __END__ The above waits for you to type, then press enter, to get a whole line +. Without the FileHandle module, you could still send commands to STDIN, but you needed a Control-d to send it. It would however, allow multi-l +ine input. ############################################################### This one will capture just a key-press, which you can process. #!/usr/bin/perl use warnings; use strict; use Glib; use Glib qw/TRUE FALSE/; 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.
Old Perl Programmer Haiku

In reply to Re: How to pass keyboard presses/sequences like "enter","Tab" and "f1" in the perl scripts by zentara
in thread How to pass keyboard presses/sequences like "enter","Tab" and "f1" in the perl scripts by rajyalakshmi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.