[Cocoa] Problem with keyDown

Post: #1
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]
[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:

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

Quote this message in a reply
Posts: 57
Joined: 2002.04
Post: #2
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.
Quote this message in a reply
Post: #3
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!
Quote this message in a reply
Posts: 370
Joined: 2002.04
Post: #4
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?
Quote this message in a reply
Post: #5
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Cocoa/OpenGL drawing full-screen problem ultitech 5 10,492 Jan 13, 2011 01:11 PM
Last Post: SethWillits
  Please help me with problem (OpenGl\Cocoa\Objective-C) wyrmmage 4 4,618 Aug 20, 2006 05:48 PM
Last Post: wyrmmage
  Cocoa obj-c method invocation problem FreeKQuency23 3 3,582 Mar 12, 2006 12:48 AM
Last Post: Blacktiger
  combo keydown hyperzoanoid 9 5,547 Aug 24, 2002 10:23 AM
Last Post: hyperzoanoid