Hi,

I'm writing a program that needs single character input from the console window. So Term::ReadKey seems to be a natural solution.

So I wrote some perl code as follows. Usually the program asks for a key, and gets it. Sometimes it wants to check for the existance of a keypress without delay, so fin_key and fin_iskey were to deal with those. And then fin_kbexpect was to deal with the case where it wants a whole line, ending with carriage return. One could switch out of raw mode, and and use <STDIN> for a whole line, but funny things happen when switching from raw to normal mode: if the user has typed characters before the mode is switched to normal, they don't echo so it is confusing. This is confusing, but understandable behavior.

What is less understandable is that when using the commented out version of fin_key, called from fin_kbexpect, the carriage return key seems to be delayed. If one types a carriage return, nothing seems to happen (it is not printed by the debug print statement) until another key is pressed. Then the carriage return stands up to be counted, and so does the next key. However, it is pretty confusing for the user to have to "type ahead" to get the previous line ending to be recognized.

The uncommented version of fin_key, which uses fin_iskey and a sleep loop, doesn't have such a problem.

Looking at the internals of Term::ReadKey (which I can't claim to fully understand) it appears that there is a difference: calling the getch vs calling the ReadConsoleInput. Perhaps mixing those isn't the best idea?

Anyway, the current handling of carriage returns by ReadKey( 0 ) is extremely disconcerting in use. The


my $debug = 1; ##################################### { my $kbd_mode; use Term::ReadKey;

#  sub fin_key
#  { unless ( $kbd_mode )
#    { ReadMode('raw');
#      $kbd_mode = 1;
#    }
#    my $key = ReadKey( 0 );
#    return ord $key;
#  }
  #####################################
  sub fin_iskey
  { unless ( $kbd_mode )
    { ReadMode('raw');
      $kbd_mode = 1;
    }
    my $key = ReadKey( -1 );
    return 0  unless defined $key;
    return ord $key;
  }
  #####################################
  sub fin_key
  { my $key;
    $key = & fin_iskey();
    until ( $key )
    { sleep ( .1 );
      $key = & fin_iskey();
    }
  }
  #####################################
  sub fin_done
  { if ( $kbd_mode )
    { ReadMode('normal');
      print "kbd mode was set\n";
      $kbd_mode = 0;
    }
    return;
  }
  #####################################
  END { & fin_done(); }
}
#####################################
sub fin_kbexpect
{ my $gotcr;
  my $buf = '';
  until ( $gotcr )
  { my $key = & fin_key();
    print "key=$key "  if $debug;
    if ( $key == 13 )
    { $buf .= chr 10;
      do_type( chr( $key ) . chr( 10 ));
      $gotcr = 1;
    } elsif ( $key == 8 )
    { do_type( "\cH \cH" );
      substr $buf, -1, 1, '';
    } elsif ( $key > 127  ||  $key < 32 )
    { do_type( "\cG" );
    } else
    { $buf .= chr $key;
      do_type( chr $key );
    }
  }
  $in{'buf'} = $buf;
  $in{'ix'} = 0;
  $in{'max'} = length $buf;
  return;
}


-- Glenn -- http://nevcal.com/ =========================== Like almost everyone, I receive a lot of spam every day, much of it offering to help me get out of debt or get rich quick. It's ridiculous. -- Bill Gates

And here is why it is ridiculous:
The division that includes Windows posted an operating profit of $2.26 billion on revenue of $2.81 billion.
--from Reuters via http://biz.yahoo.com/rc/031113/tech_microsoft_msn_1.html


So that's profit of over 400% of investment... with a bit more investment in Windows technology, particularly in the area of reliability, the profit percentage might go down, but so might the bugs and security problems? Seems like it would be a reasonable tradeoff. WalMart earnings are 3.4% of investment.

_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to