I use Term :: ReadKey in ReadMode ('cbreak') to read a single character and perform an action based on input. This works great for all other keys except the arrow keys. When the arrow keys are pressed, the action is performed 3 times, and I understand that this is because the arrow keys translate to "^ [[A], etc.
How to translate arrow keys into some arbitrary single value that ReadKey can interpret?
I tried the following code, but it does not work:
use Term::ReadKey;
ReadMode('cbreak');
my $keystroke = '';
while ($keystroke ne 'h') {
print "Enter key: ";
$keystroke = ReadKey(0);
chomp($keystroke);
if(ord($keystroke) == 27) {
$keystroke = ('0');
}
}
Here my code is based on the assumption:
use Term::RawInput;
use strict;
use warnings;
my $keystroke = '';
my $special = '';
while(lc($keystroke) ne 'i' && lc($keystroke) ne 't'){
my $promptp = "Enter key: ";
($keystroke,$special) = rawInput($promptp, 1);
if ($keystroke ne '') {
print "You hit the normal '$keystroke' key\n";
} else {
print "You hit the special '$special' key\n";
}
chomp($keystroke);
$keystroke = lc($keystroke);
}
if($keystroke eq 'i') {
}
if($keystroke eq 't') {
}
Now, no matter what I click, I cannot exit this loop
Here's the conclusion:
Enter key:
Enter key:
Enter key: You hit the normal 't' key
source
share