but I'm not sure why that doesn't work.

Because ReadKey "converts" inbound keycodes to VT100/termcap-compatible control sequences for 'portability' sake.

The full decision tree required to decode these sequences--at least those emulated on a win32 system--is:

#! perl -slw use strict; use Term::ReadKey; ReadMode( 4 ); while( 1 ) { my $c = ReadKey( 0 ); if( $c eq "\e" ) { # got an escape if( $c = ReadKey(-1) ) { ## If its an extended key they'll be another character in +the buffer if( $c eq '[' ) { # Could be an extended key, the next character to dete +rmine which $c = ReadKey( -1 ); if( $c eq "A" ) { print "Got uparrow"; } elsif( $c eq "B" ) { print "Got downarrow"; } elsif( $c eq "C" ) { print "Got rightarrow"; } elsif( $c eq "D" ) { print "Got leftarrow"; } elsif( $c eq "2" ) { print "Got Insert"; $c = ReadKey( 0 ); ## Discard (useless) '~' } elsif( $c eq "3" ) { print "Got Delete"; $c = ReadKey( 0 ); ## Discard (useless) '~' } elsif( $c eq "1" ) { print "Got Home"; $c = ReadKey( 0 ); ## Discard (useless) '~' } elsif( $c eq "4" ) { print "Got End"; $c = ReadKey( 0 ); ## Discard (useless) '~' } else { print "Ignoring unknown extended key: '$c;"; next; } } elsif( $c eq "O" ) { ## Another set of extnded keys $c = ReadKey( -1 ); if( $c eq 'y' ) { print "Got PageUp"; } elsif( $c eq 's' ) { print "Got PageDown"; } else { print "Ignoring unknown extended key: '$c;"; next; } } else { print "Ignoring unknown extended sub-selector '$c'"; next; } } else { # Just a simple escape, so quit print "Got Escape; exiting"; last; } } elsif( $c eq chr(0) ) { print "Got a null"; } else { print "Got '$c' ", ord( $c ); } } ReadMode( 0 );

But note: that doesn't allow any access to the function keys; or ctrl/alt/shift combinations with the arrow and other navigation keys. It's really a rather useless module in that respect.

If you are using Windows, then the more useful module for keyboard (and mouse) handling is Win32::Console::Input(), which allows you access to all of the Windows defined virtual keycodes + the actual scancodes; but it is still quite a messy affair and totally non-portable,


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^5: Regex arrow key problem by BrowserUk
in thread Regex arrow key problem by austin43

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.