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 > recognizing multiple keystrokes at the same time.

recognizing multiple keystrokes at the same time.
Thread Tools
Forum Regular
Join Date: Jan 2001
Location: San Luis Obispo, California, USA
Status: Offline
Reply With Quote
Jul 19, 2001, 01:15 PM
 
I'm working on a small opengl game that is controlled via the keyboard.
I can move my object around the screen right now using the w,s,a, and d keys. However, I can't get it to recognize to keys pressed at the same time... it only responds to the first one pressed.

Here's my code:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
- (<font color = green>void</font>)keyDown: (NSEvent *)aEvent
{
<font color = green>if</font>([[aEvent characters] characterAtIndex:<font color = blue>0</font>]=='w' &#0124;&#0124;
[[aEvent characters] characterAtIndex:<font color = blue>1</font>]=='w')
{[m_triangle increaseVel];
}

<font color = green>if</font>([[aEvent characters] characterAtIndex:<font color = blue>0</font>]=='s' &#0124;&#0124;
[[aEvent characters] characterAtIndex:<font color = blue>1</font>]=='s')
{[m_triangle decreaseVel];
}

<font color = green>if</font>([[aEvent characters] characterAtIndex:<font color = blue>0</font>]=='a' &#0124;&#0124;
[[aEvent characters] characterAtIndex:<font color = blue>1</font>]=='a')
{[m_triangle rotateL];
}

<font color = green>if</font>([[aEvent characters] characterAtIndex:<font color = blue>0</font>]=='d' &#0124;&#0124;
[[aEvent characters] characterAtIndex:<font color = blue>1</font>]=='d')
{[m_triangle rotateR];
}
}
</font>[/code]

what am I doing wrong?

-robotic
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Jul 20, 2001, 06:06 PM
 
The [event characters] string in all likelyhood has all of the characters pressed in it. You need to iterate through all the characters in the string, processing each one. This is why NSEvent has an NSString that can contain multiple characters and not just a single unichar variable :-)
     
robotic  (op)
Forum Regular
Join Date: Jan 2001
Location: San Luis Obispo, California, USA
Status: Offline
Reply With Quote
Jul 20, 2001, 07:30 PM
 
Thanks!

So how would I pull the characters out of the string that NSEvent passes? Hmmmm.... time to delve back into the documentation


-robotic
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Jul 22, 2001, 11:01 PM
 
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
- (<font color = green>void</font>)keyDownNSEvent *)anEvent
{
NSString *chars = [anEvent characters];
<font color = green>int</font> i, length = [chars length];

<font color = green>for</font> (i=<font color = blue>0</font>; i&lt;length; i++)
{
<font color = green>switch</font>([chars characterAtIndex:i])
{
<font color = green>case</font> 's': ...
}
}
}
</font>[/code]
     
robotic  (op)
Forum Regular
Join Date: Jan 2001
Location: San Luis Obispo, California, USA
Status: Offline
Reply With Quote
Jul 24, 2001, 01:04 PM
 
Thanks! I'll go try that out!


-robotic
     
   
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 09:43 AM.
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