Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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

Replies are listed 'Best First'.
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
Re: ReadKey can't see 0 key...
by SuicideJunkie (Vicar) 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?

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?
[stevieb]: shmem thanks for the 'insight' :P
[shmem]: good thing that Sun already took "OpenWindows", otherwise I'd not stop to shudder imagining an "OpenWindows" from MS
[shmem]: more garbage in, more garbage out that would be
[stevieb]: I found that win10 broke a C# library I was using for one project while enhancing tests for a Perl dist, which breaks other Perl dists, and I'm about to throw my hands up on berrybrew. win2k12 broke one thing, win10 breaks something...
[stevieb]: ...unrelated which requires replacing a lot of code and a whole lib. I'm about to go nix only ffs
[shmem]: stevieb: what you're doing sounds afwully complex. Too much for me this evening to provide brighter insight ;-)
[stevieb]: I don't even own a Windows computer. Both my girl and I have a laptop each with Linux. I'm supporting Windows in some of my projects and I can't even guage whether it's worth it or not.
[stevieb]: shmem It's something I desired to have years ago, which is why I took over berrybrew. Cross-platform build/test automation locally, or over the network Test::BrewBuild
[shmem]: sounds good.

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2017-03-28 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (342 votes). Check out past polls.