Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Capture a non-printable char and test what it is

by haj (Priest)
on May 21, 2022 at 15:35 UTC ( #11144047=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11144047]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2023-04-02 01:33 GMT
Find Nodes?
    Voting Booth?

    No recent polls found