 |
 |
recognizing multiple keystrokes at the same time.
|
 |
|
 |
|
Forum Regular
Join Date: Jan 2001
Location: San Luis Obispo, California, USA
Status:
Offline
|
|
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' ||
[[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' ||
[[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' ||
[[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' ||
[[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
|
|
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 :-)
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Jan 2001
Location: San Luis Obispo, California, USA
Status:
Offline
|
|
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
|
|
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
- (<font color = green>void</font>)keyDown  NSEvent *)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<length; i++)
{
<font color = green>switch</font>([chars characterAtIndex:i])
{
<font color = green>case</font> 's': ...
}
}
}
</font>[/code]
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Jan 2001
Location: San Luis Obispo, California, USA
Status:
Offline
|
|
Thanks! I'll go try that out!
-robotic
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
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
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|