iDevGames Forums
Keys ......again - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Keys ......again (/thread-5922.html)



Keys ......again - Coin - Jan 29, 2005 11:52 AM

i know there are a ton of keyDown posts.. but i still cant get it to work.

Code:
- (void)keyDown:(NSEvent *)theEvent
{
    NSString *key = [theEvent charactersIgnoringModifiers];
    switch ([key characterAtIndex:0])    {
            case NSUpArrowFunctionKey:
                ypos += 4;
                break;
            case NSDownArrowFunctionKey:
                break;
            case NSLeftArrowFunctionKey:
                break;
            case NSRightArrowFunctionKey:
                break;
    }
    [self setNeedsDisplay:YES];
}


down left and right i don't care about right now, but when i hit up the little error sound plays. why


Keys ......again - Josh - Jan 29, 2005 04:03 PM

Does it beep regardless of what key you hit?


Keys ......again - Coin - Jan 29, 2005 05:05 PM

yea any key i hit makes it do the click-like error sound

its pretty sad...


Keys ......again - Josh - Jan 29, 2005 06:12 PM

Your view needs to be set as the initial first responder. This can be done in IB.


Keys ......again - Coin - Jan 29, 2005 08:21 PM

i made a connection from the window to the view makeing it the initial first responder, it does the exact same thing


Keys ......again - belthaczar - Jan 29, 2005 08:39 PM

Add this to your view
Code:
- (void)awakeFromNib { [[self window] makeFirstResponder:self]; }
- (BOOL)acceptsFirstResponder { return YES; }



Keys ......again - Coin - Jan 29, 2005 09:06 PM

thanks, that made it work better.

although it didnt need this line,
[[self window] makeFirstResponder:self];

i just added the accepts first responder part and its all good

now i know that to make it go diagonaly i have to poll for events (from reading posts at this site)

but i couldnt find accual code for how to do that.

i am guessing it meanns that it scans all the events, and does things for all the ones that are true. wouldnt that be slower and not very efficient consitering id have to make a BOOL for every event type?


Keys ......again - belthaczar - Jan 29, 2005 09:38 PM

You don't need to poll for events. The cocoa run loop does that for you and sends the interesting bits to your responder via the keyDown: and keyUp: methods. If you want to be able to handle multiple keypresses at once, you could do something like this:

Code:
typedef struct {
    BOOL left;
    BOOL right;
    BOOL up;
    BOOL down;
} InputKeys;

- (void)keyDown:(NSEvent *)event
{
    if ([event isARepeat]) return;  // ignore repeats
    unichar key = [[event charactersIgnoringModifiers] characterAtIndex:0];
    if (key == NSLeftArrowFunctionKey  || key == 'a' || key == 'A') { keys.left  = 1; return; }
    if (key == NSRightArrowFunctionKey || key == 'd' || key == 'D') { keys.right = 1; return; }
    if (key == NSUpArrowFunctionKey    || key == 'w' || key == 'W') { keys.up    = 1; return; }
    if (key == NSDownArrowFunctionKey  || key == 's' || key == 'S') { keys.down  = 1; return; }
}

- (void)keyUp:(NSEvent *)event
{
    unichar key = [[event charactersIgnoringModifiers] characterAtIndex:0];
    if (key == NSLeftArrowFunctionKey  || key == 'a' || key == 'A') { keys.left  = 0; return; }
    if (key == NSRightArrowFunctionKey || key == 'd' || key == 'D') { keys.right = 0; return; }
    if (key == NSUpArrowFunctionKey    || key == 'w' || key == 'W') { keys.up    = 0; return; }
    if (key == NSDownArrowFunctionKey  || key == 's' || key == 'S') { keys.down  = 0; return; }
}

and then handle all of your input in some other method that gets called periodically by an NSTimer.