sid_com
sid_com

Reputation: 25117

Term::ReadKey::ReadKey: Why MS Windows behaves differently than Linux?

Term::ReadKey:

#!/usr/bin/env perl
use warnings;
use 5.012;
use Term::ReadKey;

my $key;

ReadMode 4;
print "Enter a key: ";
$key = ReadKey();
printf "|%s|\n", $key // 'undef';
$key = ReadKey(-1);
printf "|%s|\n", $key // 'undef';
$key = ReadKey(-1);
printf "|%s|\n", $key // 'undef';
ReadMode 0;

say "END";

When I run this script on Windows or Linux an press k I get both times this output:

Enter a key: |k|  
|undef|  
|undef|  
END

When I press the Up "key" I get

Enter a key: |  
|[|  
|A<  
END  

on Linux, but on Windows the script comes to standstill:

Enter a key:

Why do I get here not some strange signs instead?

Upvotes: 0

Views: 575

Answers (2)

ikegami
ikegami

Reputation: 386361

You can't get keys from an interface that give you characters. That's why you got a whole bunch of stuff for one key.

On the unix side, the keys got transformed into terminal-specific inline escape sequences you have to figure out yourself.

In Windows, you can get the keys themselves, but obviously not from a interface that gives you characters like ReadKey apparently does.

I recently showed how to get keys in Windows. No idea how to do so in unix.

Upvotes: 1

frezik
frezik

Reputation: 2316

Simply put, Term::ReadKey assumes Unixy terminals, which Windows doesn't provide (unless you use Cygwin).

You might try Win32::Console instead. Or there may be some incantations that will get Term::ReadKey to work--good luck on that.

Upvotes: 3

Related Questions