Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

ReadKey can't see 0 key...

by markseger (Beadle)
on Nov 11, 2010 at 20:07 UTC ( #870922=perlquestion: print w/ replies, xml ) Need Help??
markseger has asked for the wisdom of the Perl Monks concerning the following question:

I'm sure I'm doing something silly OR maybe it's my terminal emulator, though I'm using standard putty. I'm real stumped and am hoping someone can spot my problem... The attached piece of code simply uses ReadKey to do single char I/O and echo its binary character value.

When there's input, I print ">>> GOT INPUT". Then I read it and print its value. If I type 8900098 the following is what I see:


root@poker collectl# ./sht.pl
>>> GOT INPUT
INPUT: 56
>>> GOT INPUT
INPUT: 57
>>> GOT INPUT
>>> GOT INPUT
>>> GOT INPUT
>>> GOT INPUT
INPUT: 57
>>> GOT INPUT
INPUT: 56

so what happened to the echos of the '0's? Here's the code that does it:

#!/usr/bin/perl -w use Term::ReadKey; use IO::Select; ReadMode 4; my $sel=new IO::Select(STDIN); my $ctrlCFlag=0; while (!$ctrlCFlag) { while(my @ready = $sel->can_read(1)) { foreach my $filehandle (@ready) { if ($filehandle eq 'STDIN') { print ">>> GOT INPUT\n"; while (my $char=ReadKey -1) { my $byte=unpack('C', $char); printf "INPUT: %d\n", $byte; if ($byte==3) { $ctrlCFlag=1; last; } } } } } } ReadMode 0;

can_read() says there's something to be read on STDIN but ReadKey isn't see anything. And what's so special about '0'? Isn't it just another key? Any and all help will be greatly appreciated. -mark

Comment on ReadKey can't see 0 key...
Download Code
Re: ReadKey can't see 0 key...
by SuicideJunkie (Priest) on Nov 11, 2010 at 20:21 UTC

    The first thing that comes to mind is that zero is a false value.

    Are you sure about what your while condition is evaluating to?

Re: ReadKey can't see 0 key...
by Tanktalus (Canon) on Nov 11, 2010 at 20:41 UTC

    Without trying anything, I'd suggest that ReadKey is returning the string '0' which gets evaluated by perl to be false. Try changing your while to:

    while (defined (my $char = ReadKey -1))

      bingo! That was soooo obvious I'm embarrassed I didn't see it myself. I guess that's what can happen with too many cups of coffee - or maybe not enough! Thanks again... -mark

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-12-22 02:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (110 votes), past polls