Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Regex arrow key problem

by austin43 (Acolyte)
on Dec 01, 2010 at 18:46 UTC ( #874708=perlquestion: print w/ replies, xml ) Need Help??
austin43 has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone, I am having a bit of trouble with getting arrow keys to work with regular expressions.
Readmode(4); my $input = lc(ReadKey(0)); if ($input =~ m/left arrow key here/) { goto LEFT; } elsif ($input =~ m/right arrow key here/) { goto RIGHT; }
I have tried the \x switch for matching the hex code, and no luck. I also tried \0 switch for matching octal, and still no luck. I can get other keys like escape to work, but not the right or left arrow keys. Any ideas?

Comment on Regex arrow key problem
Download Code
Re: Regex arrow key problem
by ikegami (Pope) on Dec 01, 2010 at 19:13 UTC

    [ Please place blocks of computer text (e.g. code, data and output) in <c>...</c> tags. ]

    The first step would be to find out what ReadKey is returning. You could use

    use Data::Dumper; { local $Data::Dumper::Useqq = 1; print(STDERR Dumper($input)); }
      The left arrow key is returning "\e[D\n", and the right arrow key is returning "\e[C\n". How do I match this?
        if ($input eq "\e[D\n")
        and
        if ($input eq "\e[C\n")

        You obviously removed the lc to get those values. Be careful about re-adding lc. Only lowercase $input after checking for these special keys.

      I tried implementing it within my code instead of in its own script and it returns $VAR1 = undef;. EDIT: I accidentally put it above ReadMode(0); and that's why it returned undef. Now I put it below the $input string and it returns
      RIGHT ARROW
      $VAR1 = "\e"; $VAR1 = "["; $VAR1 = "c";
      LEFT ARROW
      $VAR1 = "\e"; $VAR1 = "["; $VAR1 = "d";
        So only one of the three characters is returned at a time. BrowserUK has already posted how to handle that.
Re: Regex arrow key problem
by kennethk (Monsignor) on Dec 01, 2010 at 19:17 UTC
    Please wrap code in <code> tags - as the author of a node, you can edit it. Please read Writeup Formatting Tips.

    I would suggest you check to make sure that the codes you are getting correspond to what you expect, perhaps with some code similar to

    use strict; use warnings; use Term::ReadKey; ReadMode(4); my %keys = GetControlChars; my $int = $keys{INTERRUPT}; while ((my $input = ReadKey(0)) ne $int) { print ord($input), "\n"; } print ord($input), "\n"; ReadMode(0);

    I would also suggest you likely want to use equality (as I've done above, see perlop) instead of regular expressions (perlre). As well, see Term::ReadKey, which does not contain a Readmode method (Perl is case sensitive).

      This spits out 27 91 68 for left, and 27 91 67 for right. Now what?
Re: Regex arrow key problem
by ww (Bishop) on Dec 01, 2010 at 19:19 UTC
    Reformat, please, using <code>...</code> tags around your code -- See Markup in the Monastery -- or, at least, pay heed to the instructions above and below the text entry box.

    Edit; don't create a second node when you screw up the first.

    Please state your problem clearly -- in this case, you might have asked (naively) "what values are returned by the arrow keys?" But if that's your intent, that's not a Perl question; it's one you might better answer for yourself by using Google or the like. And since you don't tell us the values to which you applied \x and \0, nor precisely how, any reply is likely to be no better than a WAG.

    I hope (but am not certain) that this hint may be helpful: "keycodes" and/or "scancodes"

      I clicked "update" on this post, and it created a new one for some reason. I've tried google already, and have yet to find anything that works. I've looked at scancodes and tried many different codes that I've found, all to no avail.
Re: Regex arrow key problem
by BrowserUk (Pope) on Dec 01, 2010 at 19:24 UTC

    What you are probably missing is that the "extended keys" generate multiple values for each keypress.

    Below shows the output when four arrow keys are pressed in the order: left, right, up, down:

    use Term::ReadKey;; { ReadMode(4); print ord($c), " : '$c'" while ($c=ReadKey(0)) ne "\cM"; ReadMode(0) };; 27 : '&#8592;' 91 : '[' 68 : 'D' 27 : '&#8592;' 91 : '[' 67 : 'C' 27 : '&#8592;' 91 : '[' 65 : 'A' 27 : '&#8592;' 91 : '[' 66 : 'B'

    So, in order to match the left arrow key, you would need to use "\e[D" in your regex. But, ReadKey() returns those 3 characters one at a time, so you would need to accumulate them before you could match that.

    But then the problem becomes how do you decide when you see the escape character (&#8592;, '←', "\e"; 27), whether the user has pressed the escape key, or some other keys that also generates more characters.


    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.
      So there's no way to use \x52 or whatever the hex code is for the arrow keys?
        So there's no way to use \x52 or whatever the hex code is for the arrow keys?

        Not that I'm aware of.


        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.
        Character 0x52 is uppercase letter "R".

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-09-17 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (71 votes), past polls