Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Can I capture string typING in STDIN without press enter?

by zentara (Archbishop)
on May 29, 2012 at 20:38 UTC ( #973118=note: print w/ replies, xml ) Need Help??


in reply to Can I capture string typING in STDIN without press enter?

Here is a Glib solution. You might have to jump thru a hoop to get Glib installed on win32, but it should work.

#!/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; my $count = 1; my $timer = Glib::Timeout->add (1000, \&timer_callback, undef, 1 ); 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 sub timer_callback{ $count++; print "$count\n"; return 1; } __END__

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh


Comment on Re: Can I capture string typING in STDIN without press enter?
Download Code
Re^2: Can I capture string typING in STDIN without press enter?
by exilepanda (Monk) on May 30, 2012 at 14:46 UTC
    Wow! This one works... I simply flush an array per each enter pressed.. and capture the last line by your method.. However, could you explain a little more what magic is inside the Glib::Idle?... I don't even know where to find the documentation... @@"
      Hi, see Glib MainLoop for the MainLoop documentation. Documentation is sparse, but essentially, it waits until the Glib eventloop is idle, before running the sub, so as not to interfere with it. It is seen used more when you try to use Glib or Gtk2 code from a thread. If you get into GUI's, you will often read about the thread safety of the GUI toolkit. Gtk2's thread safety is accomplished principally by only using Glib::Idle->add to access the main gui code from within the thread. Here are a couple of examples.

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh
        Thanks a lot for all the suggestions and examples. I am going start with your simpler example above first ( looks more familiar style and easier for me ) but see if I will spot some thread safe is needed for the real run... this is a really nice alternative resort! Thanks again, Cheers! =)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2014-09-16 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (46 votes), past polls