Spencer_Lists wrote:
I have tried everything I can think of to echo keypresses. Here is my
attempt to do it with Win32Console. I can get the key values and
everything else returned by input but I just can't figure out how to
echo the input. Sorry about the text formatting, my e-mail program
messes it up and will not allow me to change it.

use strict;
use Win32::Console;

my $console = Win32::Console->new(STD_INPUT_HANDLE);
my $return = $console-> Mode(ENABLE_ECHO_INPUT);
my $chars = 0;
my $string;

until ($chars eq "\r") {
    my @list = $console->Input();
    $chars = chr($list[5]);
        $string .= $chars if($list[1] == 1);#   only record down keys
                # [0] event type 1 for kbd [1] key up or down? [2] repeat count 
[3] keycode [4] scancode [5] ascii value [6] control key states ???
                print "\n string is $string\n" if($list[1] == 1);
        }
print "\nDONE";
exit;

From the MSDN documentation:

ENABLE_ECHO_INPUT - Characters read by the ReadFile or ReadConsole function are written to the active screen buffer as they are read. This mode can be used only if the ENABLE_LINE_INPUT mode is also enabled.

ENABLE_LINE_INPUT - The ReadFile or ReadConsole function returns only when a carriage return character is read. If this mode is disabled, the functions return when one or more characters are available.

So it appears that you can't use ENABLE_ECHO_INPUT in the way you are trying to. You can echo the characters yourself with a print, if you turn off buffering of STDOUT:

#!perl -w
use strict;
use warnings;

use Win32::Console;

my $console = Win32::Console->new(STD_INPUT_HANDLE);

my $char = '';
until ($char eq "\r") {
        my @list = $console->Input();

        $char = '';
        if($list[0] == 1) { # only keys, not mouse events
                if($list[1] == 1) { # only key down events
                        $char = chr($list[5]);
                        local $|=1; # auto-flush
                        print $char;
                }
        }
}
exit;
__END__


I think that Term::ReadKey is probably easier although in this example I need to press the <enter> key twice to get it to exit - if anyone can fix or explain why then I'd be grateful.

#!perl -w
use strict;
use warnings;

use Term::ReadKey;

ReadMode(3); # cbreak mode - doesn't need cr/lf to flush input

my $char='';
until($char eq "\r" ) {  # <return> to exit
        $char = ReadKey(0);
        print $char;
}
ReadMode(0); # Reset tty mode before exiting
exit(0)
__END__

Regards,
Rob.
--
Robert May
Win32::GUI, a perl extension for native Win32 applications
http://perl-win32-gui.sourceforge.net/
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
  • console echo Spencer_Lists
    • Re: [aswin32] console echo Robert May

Reply via email to