http://qs321.pair.com?node_id=11144047


in reply to Capture a non-printable char and test what it is

Capturing non-printable characters isn't that different from capturing any other character. One gotcha is that on a terminal your program usually does not receive any characters until the user hits the <ENTER> key.

If you want to capture individual characters, you need to check which characters have a meaning to your console - and thus never reach your program. The following small example uses Term::ReadKey in "raw" mode to capture one byte and then prints its hex value plus its interpretation. This works for the escape key and also for combinations with the <CTRL> key.

A warning, though: This example reads one byte, not one character. If your terminal uses UTF-8 encoding and you enter a key outside the ASCII range, then you'll receive only part of one character (Dealing with UTF-8 is left as an exercise to the reader). Also, cursor keys, function keys and the like are usually sequences which start with an escape character, my short example drops the rest of the sequence.

use Term::ReadKey; use charnames ':full'; ReadMode 4; print "Enter a key: "; my $key = ReadKey(0); printf "%v02x",$key; print " = ",charnames::viacode(ord $key),"\n"; END { ReadMode 0; }

Replies are listed 'Best First'.
Re^2: Capture a non-printable char and test what it is
by almsdealer (Acolyte) on May 22, 2022 at 17:16 UTC
    Thank you. knowing about the ord function is useful.