 |
 |
Checking arrow keyDown events
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
Okay, catching keyDown events are easy enough, and I can check between several different keys... except for the arrow keys. To get the character rep of keyDown events, I've been using stuff like
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
<font color = green>if</font>(event.characters().equals(<font color = red>"a"</font>)){...}
</font>[/code]
That works well... but what's "a" for arrow keys? I tried printing the characters from arrow events like so:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
NSSystem.Log(event.characters());
</font>[/code]
(or something like that, I don't have my mac in front of me) and I get something like \701 through \704. So here's what I try:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
<font color = green>if</font>(event.characters().equals(<font color = red>"\\<font color = blue>704</font>"</font>)){...};
</font>[/code]
but the if statement never evaluates to true. Any one knwo why?
Thanks,
F-bacher
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Because it's \704 not \\704?
\704 means character number 704, well actually that's a lame explanation of it, but it'll do
you can use \0 and so on, too
sorry, that makes very little sense. apologies.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Feb 2001
Location: Portland, OR, USA
Status:
Offline
|
|
Angus is right about how you escape special characters, but you shouldn't really do that. NSEvent has a bunch of constants for the values that you want: UpArrowFunctionKey, DownArrayFunctionKey, LeftArrowFunctionKey, RightArrowFunctionKey.
You should also be aware that it is possible for multiple characters to be in the string because the event system coalesces all the available keystrokes together into one event if possible. This happens, for instance, if your app is responding slowly and someone holds down an arrow key and has a high repeat rate - you might get an event with two or more LeftArrow characters instead of just one.
So you should have a loop in your event handler where you go through even character in the characters() string individually (even though the vast majority of the time there will just be one).
Hope this helps!
--Greg
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
Originally posted by Angus_D:
<STRONG>Because it's \704 not \\704?
\704 means character number 704, well actually that's a lame explanation of it, but it'll do
you can use \0 and so on, too
sorry, that makes very little sense. apologies.</STRONG>
Actually, it was \Uf700 through 3, and when you define strings like:
[code]
String a= "\Uf700";
[code]
you get an error for an illegal escape character, so you add another escape character. Akka "\\" -> \
F-bacher
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
Originally posted by gregomni:
<STRONG>Angus is right about how you escape special characters, but you shouldn't really do that. NSEvent has a bunch of constants for the values that you want: UpArrowFunctionKey, DownArrayFunctionKey, LeftArrowFunctionKey, RightArrowFunctionKey.
--Greg</STRONG>
I'm not sure how to use this. NSEvent.RightArrowFunctionKey is a big integer, while event.characters() is a String. Casting doesn't seem to work. The values just aren't the same... arrggghhh...
F-bacher
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Feb 2001
Location: Portland, OR, USA
Status:
Offline
|
|
You need to compare to each character in the string not the whole string...
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
<font color = green>int</font> index;
<font color = green>char</font> key;
<font color = green>for</font> (index = <font color = blue>0</font>; index < event.characters().length(); index++) {
key = event.characters().charAt(index);
<font color = green>if</font> (key == NSEvent.LeftArrowFunctionKey) {
<font color = brown>// <font color = green>do</font> stuff</font>
} <font color = green>else</font> <font color = green>if</font> (key == NSEvent.RightArrowFunctionKey) {
<font color = brown>// <font color = green>do</font> other stuff</font>
} <font color = green>else</font> ....
}
</font>[/code]
[ 06-19-2001: Message edited by: gregomni ]
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
I don't think that will work either. NSEvent.RightArrowFunctionKey is a big integer, not a char, so even casting won't ever make that equality true (the length of a big integer will never equal 1 (the length of all char's)).
F-bacher
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
Also, I shouldn't have to do all that checking with the for loop. Everything works fine if I use letters instead of arrow keys. Holding down w to move up works fine, just not holding down the up arrow function key.
F-bacher
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|