Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Regex arrow key problem

by BrowserUk (Patriarch)
on Dec 01, 2010 at 19:24 UTC ( [id://874718]=note: print w/replies, xml ) Need Help??


in reply to Regex arrow key problem

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 : '←' 91 : '[' 68 : 'D' 27 : '←' 91 : '[' 67 : 'C' 27 : '←' 91 : '[' 65 : 'A' 27 : '←' 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 (←, '←', "\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.

Replies are listed 'Best First'.
Re^2: Regex arrow key problem
by austin43 (Acolyte) on Dec 01, 2010 at 19:32 UTC
    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.
        Bummer. There's a way to do it for seemingly every other key, but when I try a hex code on arrow keys, it won't work. I looked at this page: http://api.farmanager.com/en/winapi/virtualkeycodes.html It shows all of the hex codes for keys, and http://perldoc.perl.org/perlre.html says I can use \x followed by the 2 hex digits to capture a key. Left arrow would be \x25, and right arrow \x27, but I'm not sure why that doesn't work.
      Character 0x52 is uppercase letter "R".
        I know, I was just using an example. 0x25 is left arrow and 0x27 is right, but it won't work when I put it in as
        if ($input =~ m/\x25/)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-16 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found