Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Checking arrow keyDown events

Checking arrow keyDown events
Thread Tools
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Jun 18, 2001, 12:23 PM
 
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
Reply With Quote
Jun 18, 2001, 12:43 PM
 
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
Reply With Quote
Jun 18, 2001, 02:31 PM
 
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
Reply With Quote
Jun 18, 2001, 09:38 PM
 
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 "\\" -&gt; \

F-bacher
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Jun 18, 2001, 09:42 PM
 
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
Reply With Quote
Jun 18, 2001, 10:59 PM
 
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 &lt; 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
Reply With Quote
Jun 19, 2001, 10:10 AM
 
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
Reply With Quote
Jun 19, 2001, 10:17 AM
 
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
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:21 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2