Handling more than one key press at once
First off: sorry about posting two threads about different things.
Anyway: I've been trying to program "pong" on my mac, and I've gotten to the stage where I have two rectangles up on the screen, but now I need to be able to move them. I've been using code from nehe's site (nehe.gamedev.net), so I have a basic for handling user input, it looks like this:
obviously I'm using carbon and Objective-C to program this; the file that that piece of code ^^^^^ resides in is an extension of NSResponder. If a user presses the up or down arrow, it moves one paddle, if it presses the "w" or "s" key, it moves the other paddle; this would be fine if I were going to use AI for one of the paddles, but I'm trying to make the game two-player.
Therefore I need to know: how do I handle more than one key being pressed, so I can tell if one off the (w or s) keys is being pressed and if one of the (up or down arrow keys) is being pressed at the same time?
Thank you for your help :)
-wyrmmage
Anyway: I've been trying to program "pong" on my mac, and I've gotten to the stage where I have two rectangles up on the screen, but now I need to be able to move them. I've been using code from nehe's site (nehe.gamedev.net), so I have a basic for handling user input, it looks like this:
Code:
- (void) keyDown:(NSEvent *)theEvent
{
unichar unicodeKey;
unicodeKey = [ [ theEvent characters ] characterAtIndex:0 ];
switch( unicodeKey )
{
case 0xF700:
[glView moveHleft];
break;
case 0xF701:
[glView moveHright];
break;
case 0x0077:
[glView moveYleft];
break;
case 0x0073:
[glView moveYright];
break;
default:
break;
}
}obviously I'm using carbon and Objective-C to program this; the file that that piece of code ^^^^^ resides in is an extension of NSResponder. If a user presses the up or down arrow, it moves one paddle, if it presses the "w" or "s" key, it moves the other paddle; this would be fine if I were going to use AI for one of the paddles, but I'm trying to make the game two-player.
Therefore I need to know: how do I handle more than one key being pressed, so I can tell if one off the (w or s) keys is being pressed and if one of the (up or down arrow keys) is being pressed at the same time?
Thank you for your help :)
-wyrmmage
wyrmmage Wrote:First off: sorry about posting two threads about different things.No need to apologize - we're here to help.

wyrmmage Wrote:how do I handle more than one key being pressed, so I can tell if one off the (w or s) keys is being pressed and if one of the (up or down arrow keys) is being pressed at the same time?The general way to do this is to set a state for each key or axis of movement on keyDown, act on it in your run loop, and unset it on keyUp. For example:
Code:
@interface MyClass : NSResponder {
int leftPaddleMoveDirection;
int rightPaddleMoveDirection;
}
// ...
- (void) keyDown: (NSEvent *) event {
switch ([[event characters] characterAtIndex: 0]) {
case 'w':
leftPaddleMoveDirection = 1;
break;
case 's':
leftPaddleMoveDirection = -1;
break;
case NSUpArrowFunctionKey:
rightPaddleMoveDirection = 1;
break;
case NSDownArrowFunctionKey:
rightPaddleMoveDirection = -1;
break;
}
}
- (void) keyUp: (NSEvent *) event {
switch ([[event characters] characterAtIndex: 0]) {
case 'w':
case 's':
leftPaddleMoveDirection = 0;
break;
case NSUpArrowFunctionKey:
case NSDownArrowFunctionKey:
rightPaddleMoveDirection = 0;
break;
}
}
- (void) run {
if (leftPaddleMoveDirection == 1) {
[glView moveLeftPaddleUp];
} else if (leftPaddleMoveDirection == -1) {
[glView moveLeftPaddleDown];
}
if (rightPaddleMoveDirection == 1) {
[glView moveRightPaddleUp];
} else if (rightPaddleMoveDirection == -1) {
[glView moveRightPaddleDown];
}
}Hope this helps!
ah, okay
Thank you for your help; I'll try that.
-wyrmmage
*EDIT*
Yep, that took care of the problem. Thank you! ^_^
Thank you for your help; I'll try that.-wyrmmage
*EDIT*
Yep, that took care of the problem. Thank you! ^_^
One more question: If I define two rectangles that are at the same Z-depth (-1.0), only one of them shows up
To get the other one to show up, I have to use the glTranslatef() function and make them at a diffferent depth; changing the actual Z-depth of the points themselves and not applying the translation does not seem to work 
What is causing this? I've set my near clipping pane to be at 0.1f and my far pane to be at 10.0f
-wrmmage
To get the other one to show up, I have to use the glTranslatef() function and make them at a diffferent depth; changing the actual Z-depth of the points themselves and not applying the translation does not seem to work 
What is causing this? I've set my near clipping pane to be at 0.1f and my far pane to be at 10.0f
-wrmmage
Try disabling depth testing.
Sir, e^iπ + 1 = 0, hence God exists; reply!
hmmm....not quite sure how to do that, but I believe I've found the problem; I had set the first variable in gluPerspective to 100, instead of 45, which messed up the process.
Thank everyone!
-wyrmmage
Thank everyone!

-wyrmmage
wyrmmage Wrote:hmmm....not quite sure how to do that, but I believe I've found the problem; I had set the first variable in gluPerspective to 100, instead of 45, which messed up the process.
Thank everyone!
-wyrmmage
glDisable(GL_DEPTH_TEST) will turn off depth testing.
glEnable/glDisable
ok, thank you everyone! *now closing topic*
-wyrmmage
-wyrmmage
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Capturing Arrow Key Press | robmcq | 5 | 4,830 |
Oct 7, 2005 01:19 AM Last Post: codemattic |
|
| CB Key handling | skyhawk | 6 | 3,208 |
Aug 12, 2003 10:13 AM Last Post: kelvin |
|

