Last week I had posted a query about getting keyboard input in a non
blocking way.

I received several replies, so I thought I would post back the code I
developed which seems to work.

This is not the code I will end up using; it is more like a proof of
concept program.

use strict;
use warnings;
use Win32::Console;

my $signame = '';
my $havebrk = 0;

$SIG{INT}   = sub {$signame = $_[0]; $havebrk = 1;};    # CTRL-C.
$SIG{BREAK} = sub {$signame = $_[0]; $havebrk = 1;};    # CTRL-BREAK,
CTRL-ScrollLock.

my $STDIN = new Win32::Console(STD_INPUT_HANDLE);
$STDIN -> Mode(ENABLE_PROCESSED_INPUT);

while (1)
{
  if ($STDIN->GetEvents())
  {
    # We do this inner loop here to make shure that we read
    # all of the characters in the key buffer.
    while ($STDIN->GetEvents())
    {
        # Read console event.
        my @input = $STDIN->Input();

      # input[0] is the event type- 1 for keyboard, 2 for mouse. So what
is 0 for?
        if (defined $input[0] and $input[0] == 1)
      {
        my ($eventType, $keyState, $keyCount, $keyCode, $scanCode,
$keyValue, $keyFlags) = @input;

        if ($havebrk) {die "User termination on signal $signame.\n\n";}

        # KeyState of 1 means key down.
        if ($keyState == 1)
        {
          if ($keyValue == 0x00)
          {
            # Most control keys fall in here.
            if ($keyCode == 16)  {print "\nSHIFT key pressed.\n";}
            if ($keyCode == 17)  {print "\nCTRL key pressed.\n";}
            if ($keyCode == 18)  {print "\nALT key pressed.\n";}
            if ($keyCode == 19)  {print "\nBREAK key pressed.\n";}
            if ($keyCode == 20)  {print "\nCAPS LOCK key pressed.\n";}
            if ($keyCode == 33)  {print "\nPG UP key pressed.\n";}
            if ($keyCode == 34)  {print "\nPG DN key pressed.\n";}
            if ($keyCode == 35)  {print "\nEND key pressed.\n";}
            if ($keyCode == 36)  {print "\nHOME key pressed.\n";}
            if ($keyCode == 37)  {print "\nLEFT ARROW key pressed.\n";}
            if ($keyCode == 38)  {print "\nUP ARROW key pressed.\n";}
            if ($keyCode == 39)  {print "\nRIGHT ARROW key pressed.\n";}
            if ($keyCode == 40)  {print "\nDOWN ARROW key pressed.\n";}
            if ($keyCode == 45)  {print "\nINS key pressed.\n";}
            if ($keyCode == 46)  {print "\nDEL key pressed.\n";}
            if ($keyCode == 91)  {print "\nLEFT WINDOWS key
pressed.\n";}
            if ($keyCode == 92)  {print "\nRIGHT WINDOWS key
pressed.\n";}
            if ($keyCode == 93)  {print "\nCONTEXT key pressed.\n";}
            if ($keyCode == 112) {print "\nF1 pressed.\n";}
            if ($keyCode == 113) {print "\nF2 pressed.\n";}
            if ($keyCode == 114) {print "\nF3 pressed.\n";}
            if ($keyCode == 115) {print "\nF4 pressed.\n";}
            if ($keyCode == 116) {print "\nF5 pressed.\n";}
            if ($keyCode == 117) {print "\nF6 pressed.\n";}
            if ($keyCode == 118) {print "\nF7 pressed.\n";}
            if ($keyCode == 119) {print "\nF8 pressed.\n";}
            if ($keyCode == 120) {print "\nF9 pressed.\n";}
            if ($keyCode == 121) {print "\nF10 pressed.\n";}
            if ($keyCode == 122) {print "\nF11 pressed.\n";}
            if ($keyCode == 123) {print "\nF12 pressed.\n";}
            if ($keyCode == 144) {print "\nNUM LOCK pressed.\n";}
            if ($keyCode == 145) {print "\nSCROLL LOCK pressed.\n";}
          }
    
          elsif ($keyValue >= 0x7f)
          {
            # High line draw chars etc fall in here, however
            # I was never able to get it to trigger.
            print "High char pressed.\n";
          }
    
          else
          {
            # *Almost* Everything else is a printable ASCII character.
            if    ($keyValue ==  8) {print "BACKSPACE key pressed.\n";}
            elsif ($keyValue ==  9) {print "TAB key pressed.\n";}
            elsif ($keyValue == 13) {print "ENTER key pressed.\n";}
            elsif ($keyValue == 27) {print "ESC key pressed.\n";}
            else
            {
              # When here, presumably a printable character has been
pressed.
              my $keyChr = chr($keyValue);
              print "\nChar pressed: $keyChr\n";
            }
          }
        }

        # KeyState of 0 means that a key was released.
        elsif ($keyState == 0)
        {
          if ($keyValue == 0x00)
          {
            if ($keyCode == 16)  {print "SHIFT key released.\n\n";}
            if ($keyCode == 17)  {print "CTRL key released.\n\n";}
            if ($keyCode == 18)  {print "ALT key released.\n\n";}
            if ($keyCode == 19)  {print "BREAK key released.\n\n";}
            if ($keyCode == 20)  {print "CAPS LOCK key released.\n\n";}
            if ($keyCode == 33)  {print "PG UP key released.\n\n";}
            if ($keyCode == 34)  {print "PG DN key released.\n\n";}
            if ($keyCode == 35)  {print "END key released.\n\n";}
            if ($keyCode == 36)  {print "HOME key released.\n\n";}
            if ($keyCode == 37)  {print "LEFT ARROW key released.\n\n";}
            if ($keyCode == 38)  {print "UP ARROW key released.\n\n";}
            if ($keyCode == 39)  {print "RIGHT ARROW key
released.\n\n";}
            if ($keyCode == 40)  {print "DOWN ARROW key released.\n\n";}
            if ($keyCode == 45)  {print "INS key released.\n\n";}
            if ($keyCode == 46)  {print "DEL key released.\n\n";}
            if ($keyCode == 91)  {print "LEFT WINDOWS key
released.\n\n";}
            if ($keyCode == 92)  {print "RIGHT WINDOWS key
released.\n\n";}
            if ($keyCode == 93)  {print "CONTEXT key released.\n\n";}
            if ($keyCode == 112) {print "F1 released.\n\n";}
            if ($keyCode == 113) {print "F2 released.\n\n";}
            if ($keyCode == 114) {print "F3 released.\n\n";}
            if ($keyCode == 115) {print "F4 released.\n\n";}
            if ($keyCode == 116) {print "F5 released.\n\n";}
            if ($keyCode == 117) {print "F6 released.\n\n";}
            if ($keyCode == 118) {print "F7 released.\n\n";}
            if ($keyCode == 119) {print "F8 released.\n\n";}
            if ($keyCode == 120) {print "F9 released.\n\n";}
            if ($keyCode == 121) {print "F10 released.\n\n";}
            if ($keyCode == 122) {print "F11 released.\n\n";}
            if ($keyCode == 123) {print "F12 released.\n\n";}
            if ($keyCode == 144) {print "NUM LOCK released.\n\n";}
            if ($keyCode == 145) {print "SCROLL LOCK released.\n\n";}
          }

          elsif ($keyValue >= 0x7f)
          {
            # High line draw chars etc fall in here.
            print "High char released.\n\n";
          }
    
          else
          {
            if    ($keyValue ==  8) {print "BACKSPACE key
released.\n\n";}
            elsif ($keyValue ==  9) {print "TAB key released.\n\n";}
            elsif ($keyValue == 13) {print "ENTER key released.\n\n";}
            elsif ($keyValue == 27) {print "ESC key released.\n\n";}
            else
            {
              my $keyChr = chr($keyValue);
              print "Char released: $keyChr\n\n\n\n";
            }
          }
        }
        }
    }
  }

  # I made the delay 3 seconds so you can see what
  # happens when you enter multiple characters
  # outside of the INPUT loop.
  print "ongoing task.\n";
  select undef, undef, undef, 3;
}
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to