Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Capturing Arrow Keys Input

by Anonymous Monk
on Sep 29, 2009 at 23:35 UTC ( #798213=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to capture the upper, lower, left, and right arrow keys using the code below.
#!/usr/bin/perl use strict; use Term::ReadKey; ReadMode( 'cbreak' ); while (1) { my $char = ReadKey( 0 ); print "Character: $char\n"; } ReadMode( 'normal' );
Please see below when I run this code,
When I type 'c', I get this which is okay. Character: c When I hit 'the upper arrow', I get this output. Character: Character: [ Character: A
Can anyone explain this? Or how do I capture the arrow keys?

Comment on Capturing Arrow Keys Input
Select or Download Code
Re: Capturing Arrow Keys Input
by jakobi (Pilgrim) on Sep 30, 2009 at 00:02 UTC

    Unix side of things:

    Welcome, you just found a nest of killer-bees in your way:)

    Very Short Version:

    There are no arrow keys (sensu key code in terminal windows). Use cat -vet to know what sequence to expect for a key.

    Moderately Short Version:

    On a Unix box, check man termcap, terminfo, maybe also stty, tput, setterm, and possibly curses (there are a number of perl modules as well, incl. for curses use; but I think you should stop well before curses unless you really need it). Termcap and terminfo make sense of the TERM variable (often one of xterm or vt100) and translate that to the actual capabilities to expect from the terminal (/pty/tty; if not avoidable: read up on the master/slave side of ptys as well; also raw, cooked, cbreak; the notions of process groups and controlling ttys).

    This page from the docs of the rxvt-unicode X graphical terminal emulator http://pwet.fr/man/linux/conventions/urxvt might serve to give you some impression of terminal apps and the (historical) esc-codes controllig their use, going back all the way to the use of the earliest typewriters as terminals on mini computers (and most of that cruft is still retained).

    Which keys to expect: how about cat -vet in a shell window. Note that for X, there was a change, so rxvt (and vt100/screen I think?) tend to use the older style, xterm provides the newer cursor/function key codes

Re: Capturing Arrow Keys Input
by Anonymous Monk on Sep 30, 2009 at 02:06 UTC
    I tried to do  cat -vet , but it also gave me something like '^[[A'.

    How do I translate this to something that Term::ReadKey can understand?

      Extending utilitarians answer to the opener below(!?):

      ^[ is the escape 0x1b
      [
      A.

      ^[[ together is the 'CSI' char sequence introduction sequence. Some terminals might write the 8th-bit-variant using a single character of 0x9b (which is an invalid char in utf-8).

      If cat -vet's way to render control chars makes reading too hard, try something like hd or od -x instead and check the hex representation instead. If you see the generated codes and have played a bit with them to gain an understanding, do consider checking the cpan for modules abstracting some of this historic cruft.

      Especially as terminal settings come into play - unless it's either throw away code or you can guarantee that the code sequences are unchanged where-ever you run. Seeing the actual sequences themselves is helpful and IMHO required to gain an understanding, albeit it's not very portable...

      btw: has anyone a known good way to strip tty escape sequences from a typescript created by script other than a 80% quick and dirty guesswork solutions like unescape.pl (which I'd call an example of throw-away code :))?

Re: Capturing Arrow Keys Input
by Utilitarian (Vicar) on Sep 30, 2009 at 07:29 UTC
    you could take a look at Term::TransKeys it seems to provide what you need without the need to interpret the return value of ReadKey. Otherwise you have to check for an escape and match the following sequence to the values returned using jakobi's method above to determine the list of potential matches.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://798213]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (15)
As of 2014-09-16 13:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (24 votes), past polls