[Cocoa] Problem with keyDown
I'm having a problem moving my character in my view using the keyDown method. The problem is that whenever I hold down an arrow key, there's a brief pause after the character moves. It's kind of hard to explain, but my movement looks like this:
(holding down up key)
[character moves up]
[pause]
[character moves up]
[character moves up]
[character moves up]
[character moves up]
I'm can't figure out why there's a pause when I first push the key. My code looks like this:
Anyone have any idea what might be happening?
-Criolo
(holding down up key)
[character moves up]
[pause]
[character moves up]
[character moves up]
[character moves up]
[character moves up]
I'm can't figure out why there's a pause when I first push the key. My code looks like this:
Code:
- (void)keyDown: (NSEvent *)event
{
int key = [[event characters] characterAtIndex: 0];
if ( key == NSUpArrowFunctionKey ) //up
{
[player incrementY];
[self display];
}
}Anyone have any idea what might be happening?
-Criolo
It's most likely due to key repeat settings; a key usually doesn't begin to repeat until after a second or so (try holding down a letter key in a textfield to see this). Rather than going by keyDown events, you might want to track both keyDown and keyUp events to determine whether the key is currently pressed; e.g. set a variable to "true" when you receive a keyDown event and "false" when you receive the corresponding keyUp.
your game is itterating based on keydown events, and not polling the key.
so in keydown() you have pos+=1, and keydown gets called once when you hit it, then a pause, then a flood of keydowns occours until you release the key.
you should have, in your keydown, a globalvariable assignment : keys[key] = 0
where : char keys[128];
0 means key is down, and 1 means it is up.
then somewhere else you say if(keys[23]==0)pos+=1.0
--------------------------
advanced : this still isnt the right way. when you are doing nothing, there are no events, when you are mashing on the keyboard, you have like 2000+ events/sec. The game bogs down.....
true keyboard polling is the answer to this problem. I have yet to verify that HID can do true polling. Apple claims it can. The word polling has been confused by HID already, by saying you can "poll the event queue" but that is not true polling.
DirectInput on win32 can do true polling.
linux+xfree86 Can't do it.
If anyone here claims to have a true polling hid example, please post it for us to see!
so in keydown() you have pos+=1, and keydown gets called once when you hit it, then a pause, then a flood of keydowns occours until you release the key.
you should have, in your keydown, a globalvariable assignment : keys[key] = 0
where : char keys[128];
0 means key is down, and 1 means it is up.
then somewhere else you say if(keys[23]==0)pos+=1.0
--------------------------
advanced : this still isnt the right way. when you are doing nothing, there are no events, when you are mashing on the keyboard, you have like 2000+ events/sec. The game bogs down.....
true keyboard polling is the answer to this problem. I have yet to verify that HID can do true polling. Apple claims it can. The word polling has been confused by HID already, by saying you can "poll the event queue" but that is not true polling.
DirectInput on win32 can do true polling.
linux+xfree86 Can't do it.
If anyone here claims to have a true polling hid example, please post it for us to see!
I would think that polling would be slower than events, as events only bother with a keystroke when it actually happens as opposed to polling which checks the keys whether they're down or not.
Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Even if your program is doing nothing but clearing the window every frame it is hard to imagine any possible way that event handling could take up enough time to bog down your application. Even if you could press 2000+ keys a second (yeah right), I don't think it would cause any change in your framerate.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Cocoa/OpenGL drawing full-screen problem | ultitech | 5 | 7,036 |
Jan 13, 2011 01:11 PM Last Post: SethWillits |
|
| Please help me with problem (OpenGl\Cocoa\Objective-C) | wyrmmage | 4 | 3,112 |
Aug 20, 2006 05:48 PM Last Post: wyrmmage |
|
| Cocoa obj-c method invocation problem | FreeKQuency23 | 3 | 2,743 |
Mar 12, 2006 12:48 AM Last Post: Blacktiger |
|
| combo keydown | hyperzoanoid | 9 | 3,802 |
Aug 24, 2002 10:23 AM Last Post: hyperzoanoid |
|

