PDA

View Full Version : cocoa events: handling multiple key input


FreeKQuency23
2006.04.13, 03:38 PM
Hi,

I am trying to write controls for a first person camera with strafing and movement
using the 'wasd' keys.

Currently Im using a single event keypress handler in cocoa:
-(void)keyDown: (NSEvent *)theEvent

How can I handle more than one keypress at a time? like if you want to strafe and move forward at the same time, you would press both 'w' and 'd' and then of course ill need more keys for shooting and jumping or crouching, etc...

thanks.

One consideration is to completly switch my windowing to carbon libraries.
but Id rather not rewrite everything. There has to be a way in cocoa.

Taxxodium
2006.04.13, 04:11 PM
Instead of doing if else stuff, use switch.. case

The keyDown method will be called if it detects a keypress, even if multiple keys are hit at the same time.

ThemsAllTook
2006.04.13, 04:39 PM
How this is generally done: Keep internal state variables which represent the state of the keyboard controls, set/unset them in keyDown and keyUp, and act on them in your run loop. Performing the relevant action based only on the information in the incoming event in a keyDown handler is rarely useful in this situation.

FreeKQuency23
2006.04.13, 04:47 PM
Thanks everyone. I found some very useful example code on how to do this.
Here is the link in case anyone else is interested.

http://www.cocoadev.com/index.pl?HandlingMultipleKeyDown

peace