in reply to Reading all nonblocking keystrokes in Win2000

Somewhere in the dusty, musty, trivial bits of DOS knowledge that's in the back of my brain I seem to remember that the getc() call under DOS has to be called _twice_ for special characters. The first byte is a flag byte and the second one contains the actual keyboard scan code.

The Term::ReadKey module appears to use getc() or Win32Peek() to get its characters. I'll bet if you get a null (ord==0) and then immediately read for another key you'll get the scan code for the special key. I don't have a MS system handy or I'd test this theory out...

Update:Google does it again. An explanation of getc under ths baroque environment.

  • Comment on Re: Reading all nonblocking keystrokes in Win2000

Replies are listed 'Best First'.
Re: Re: Reading all nonblocking keystrokes in Win2000
by Washizu (Scribe) on Nov 16, 2001 at 08:39 UTC

    Term::ReadKey uses Win32Peek(), but could anyone explain exactly how to use this subroutine anywhere else? I don't know how it is being called since it isn't defined in the Term::ReadKey package or any of the modules it uses (as far as I could tell).

    -----------------------------------
    Washizu
    The best offense is a good offense.

      You shouldn't need to bother with Win32Peek() at all. It's probably just a fancy-ass timed wrapper for getc(). Since Google doesn't seem to think it's part of the Win32 API it's probably something that the Term::ReadKey author whipped up to do timed reads.

      What happens when you get a zero-byte and then do a second ReadKey() right away? Kinda like (pseudocode):

      c=ReadKey if (c==0) { # Oh, this is a special key c=ReadKey }

        I tried the following:

        #!/usr/local/bin/perl5 # Use Perl 5 use Term::ReadKey; my $c = ""; ReadMode(5); while ($c ne "q") # Press q to exit { $c = ReadKey(-1); if (ord($c) == 0) { $c .= ReadKey(-1); } if ($c ne "") { print ord $c; } } ReadMode(0);

        It didn't seem to work. How can Win32Peek() be hidden? I didn't even know this was possible in perl. There are a few things that bother me about Term::ReadKey:

        1. Win32Peek() is somehow both hidden from view, but I can't figure out how to even call it from another script.
        2. I am not sure where the ReadKey subroutine returns a value in the Win32 ReadKey sub. (Can you return a value by putting data into @_?)

        -----------------------------------
        Washizu
        The best offense is a good offense.