PDA

View Full Version : Keys ......again


Coin
2005.01.29, 02:52 PM
i know there are a ton of keyDown posts.. but i still cant get it to work.

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

Josh
2005.01.29, 07:03 PM
Does it beep regardless of what key you hit?

Coin
2005.01.29, 08:05 PM
yea any key i hit makes it do the click-like error sound

its pretty sad...

Josh
2005.01.29, 09:12 PM
Your view needs to be set as the initial first responder. This can be done in IB.

Coin
2005.01.29, 11:21 PM
i made a connection from the window to the view makeing it the initial first responder, it does the exact same thing

belthaczar
2005.01.29, 11:39 PM
Add this to your view

- (void)awakeFromNib { [[self window] makeFirstResponder:self]; }
- (BOOL)acceptsFirstResponder { return YES; }

Coin
2005.01.30, 12:06 AM
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?

belthaczar
2005.01.30, 12:38 AM
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:


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.