Re: Regex arrow key problem
by BrowserUk (Patriarch) 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 : '←'
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.
| [reply] [d/l] [select] |
|
|
So there's no way to use \x52 or whatever the hex code is for the arrow keys?
| [reply] |
|
|
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.
| [reply] |
|
|
|
|
|
|
Character 0x52 is uppercase letter "R".
| [reply] |
|
|
|
|
|
Re: Regex arrow key problem
by ikegami (Patriarch) 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));
}
| [reply] [d/l] [select] |
|
|
The left arrow key is returning "\e[D\n", and the right arrow key is returning "\e[C\n". How do I match this?
| [reply] |
|
|
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.
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
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";
| [reply] [d/l] [select] |
|
|
So only one of the three characters is returned at a time. BrowserUK has already posted how to handle that.
| [reply] |
Re: Regex arrow key problem
by kennethk (Abbot) 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). | [reply] [d/l] [select] |
|
|
This spits out 27 91 68 for left, and 27 91 67 for right. Now what?
| [reply] |
|
|
| [reply] |
|
|
|
|
|
Re: Regex arrow key problem
by ww (Archbishop) 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" | [reply] [d/l] [select] |
|
|
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.
| [reply] |