http://www.perlmonks.org?node_id=157255

arc_of_descent has asked for the wisdom of the Perl Monks concerning the following question:


Hi Monks,
I'm using ActiveState's perl and I downoaded the Term::ReadKey
module as my intention is to build a simple shell program.
I started my adventure with deciding to conquer ReadKey.
However, i am facing sme problems (as always).

#!/usr/bin/perl -w use strict; use Term::ReadKey; my $key; ReadMode 'raw'; $key = ReadKey(0); print "Key is ", ord($key), "\n"; ReadMode 0; print "hit enter to exit...\n"; <>;
Now, above program works great for all keys, except
the Enter(\n) key. If i press Enter as the first key
The ReadKey function doesn't return until i press another key.
Is this a quirk with Term::ReadKey on windows platform,
or am i doing something wrong?

Thanx for any help

arc_of_descent

Replies are listed 'Best First'.
Re: Term::ReadKey under win32
by perigeeV (Hermit) on Apr 07, 2002 at 12:16 UTC
    From Term::Getch:

    This module is for all those Win32 users who can't get Term::ReadKey to work with ActiveState's Perl. I don't know if anybody else can, or can't get it to work, but I know I had a heck of a time today trying to get Term::ReadKey to work. So, finally, out of desperation, I hacked out this small attempt at a portable solution to the delima. Behold, Term::Getch;

    I think I may have to play with this today, too.

      Thanx a lot for the link.
      Along the way, i picked up Win32::Console.
      I was amazed at the possibilities the console under win32 offers.
      But unfortunately, i still don't have what i want.
      The getch in Term::Getch and the input in Win32::Console are both supposedly
      non-blocking.
      So far, Term::ReadKey works great for me, except
      for the funny thing with the newline. I'm still stuck.

      --
      arc_of_descent

Re: Term::ReadKey under win32
by Dog and Pony (Priest) on Apr 07, 2002 at 11:53 UTC
    Sadly, I have no Windows box to try this on here. But you might want to try to set other ReadModes, such as "ultra-raw". Windows has some differences as to how it sends its enter command, and looking at the docs, it sounds like changing ReadMode might just do it. Just remember that Windows is actually sending two "numbers" (13 and 10) for every enter.

    Like I said, couldn't test it. But it may be worth a try. :)

    Update: Sorry that I forgot to mention this. Yes, your code works as expected on linux. It says "Key is 10" when I press enter, then moves on, just like it should.


    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
      Hi,
      Thanx for your suggestion, but it has already been explored by me.
      Further more, the Term::ReadKey docs says that
      under windows, the ultra-raw mode is the same as the raw mode.
      Since u mentioned that you don't have a windows box,
      I'd like to know whether that code works on linux terminals.

      Thanx

      --
      arc_of_descent

        You get a \n value ..Does this help ?
        #!/usr/bin/perl -w use strict; use Term::ReadKey; my $key; ReadMode 0; $key = ReadKey(0); print "Key is ", ord($key), "\n"; ReadMode 0; print "hit enter to exit...\n"; <>;
Re: Term::ReadKey under win32
by perigeeV (Hermit) on Apr 08, 2002 at 03:35 UTC

    I have found that giving ReadKey() a timeout value seems to workaround this behavior. Instead of calling ReadKey(0), call it with an insane value: ReadKey(1000000000), or you can set it to one second and catch the timeout. Then just reissue the func call.


      Hey I found some help alright. this is the script that works on win xp #!/usr/bin/perl -w use Term::ReadKey; print "Gimme a word: ";$|++; while (1) { $|++; ReadMode 5; $key = ReadKey (10); chomp ($key); ReadMode "normal"; printf "*"; $|++; #printf "\nYou said %s, char number %03d\n", #$key, ord $key; last if ($key =~ /\r\n/); } print "\n";
Re: Term::ReadKey under win32
by jlongino (Parson) on Apr 08, 2002 at 02:31 UTC
    Don't know if this will help or not, but a wrote a module Term::TermKeys that requires Term::ReadKey and was intended to make using it a bit easier. In retrospect, it was an interesting experiment with little practical use for others, but it did what I needed at the time. If nothing else, maybe it will give decent examples on how to use the module. BTW, I had no problems with the Enter key, if memory serves me correctly. HTH,

    --Jim

Re: Term::ReadKey under win32
by zakzebrowski (Curate) on Apr 08, 2002 at 12:17 UTC
    If you do a google search, you might be able to come up with the modules that allow you to play zork... This program has switches that uses the appropriate module for the appropriate architcture...

    ----
    Zak
Re: Term::ReadKey under win32
by kart124 (Initiate) on Jul 03, 2007 at 12:30 UTC
    Hey here is something that works on my windows XP box. here is the code: #!/usr/bin/perl -w use Term::ReadKey; print "Gimme a word: ";$|++; while (1) { $|++; ReadMode 5; $key = ReadKey (10); chomp ($key); ReadMode "normal"; printf "*"; $|++; #printf "\nYou said %s, char number %03d\n", #$key, ord $key; last if ($key =~ /\r\n/); } print "\n"; -Thanks kart124
Re: Term::ReadKey under win32 (filed)
by tye (Sage) on Jul 03, 2007 at 16:36 UTC

    Bug report filed, http://rt.cpan.org/Ticket/Display.html?id=27944, which includes the work-around of using a long delay, ReadKey(~0/2-1), instead of ReadKey(0), and the simple code change that will likely fix the problem. Thanks.

    Too bad it isn't worth filing the bug that ReadKey(0) should have meant a maximum delay of 0 instead of an unbounded delay. :)

    - tye