Handling more than one key press at once

Moderator
Posts: 370
Joined: 2006.08
Post: #1
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:


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
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
wyrmmage Wrote:First off: sorry about posting two threads about different things.
No need to apologize - we're here to help. Smile

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!
Quote this message in a reply
Moderator
Posts: 370
Joined: 2006.08
Post: #3
ah, okay Smile Thank you for your help; I'll try that.
-wyrmmage

*EDIT*
Yep, that took care of the problem. Thank you! ^_^
Quote this message in a reply
Moderator
Posts: 370
Joined: 2006.08
Post: #4
One more question: If I define two rectangles that are at the same Z-depth (-1.0), only one of them shows up Annoyed 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 Sad
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
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #5
Try disabling depth testing.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 370
Joined: 2006.08
Post: #6
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! Smile
-wyrmmage
Quote this message in a reply
Jones
Unregistered
 
Post: #7
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! Smile
-wyrmmage

glDisable(GL_DEPTH_TEST) will turn off depth testing.

glEnable/glDisable
Quote this message in a reply
Moderator
Posts: 370
Joined: 2006.08
Post: #8
ok, thank you everyone! *now closing topic*

-wyrmmage
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Capturing Arrow Key Press robmcq 5 5,254 Oct 7, 2005 01:19 AM
Last Post: codemattic
  CB Key handling skyhawk 6 3,528 Aug 12, 2003 10:13 AM
Last Post: kelvin