Keys ......again
i know there are a ton of keyDown posts.. but i still cant get it to work.
down left and right i don't care about right now, but when i hit up the little error sound plays. why
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
Does it beep regardless of what key you hit?
yea any key i hit makes it do the click-like error sound
its pretty sad...
its pretty sad...
Your view needs to be set as the initial first responder. This can be done in IB.
i made a connection from the window to the view makeing it the initial first responder, it does the exact same thing
Add this to your view
Code:
- (void)awakeFromNib { [[self window] makeFirstResponder:self]; }
- (BOOL)acceptsFirstResponder { return YES; }
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?
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?
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:
and then handle all of your input in some other method that gets called periodically by an NSTimer.
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.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Spaces on Snow Leopard and ctrl/arrow keys | GolfHacker | 19 | 11,297 |
Dec 4, 2012 03:18 PM Last Post: bmantzey |
|
| Multiple Keys down in SDL | silver9172 | 2 | 3,904 |
May 30, 2009 06:16 PM Last Post: scgames |
|
| [NSEvent keyCode] to actual keys? | teknein | 8 | 7,456 |
Sep 5, 2007 07:27 AM Last Post: teknein |
|
| Keys I Can Use In Games | Nick | 10 | 5,106 |
Jul 22, 2004 11:49 PM Last Post: AnotherJake |
|
| Configurable keys - describe key that was pressed | MattDiamond | 13 | 6,544 |
Oct 27, 2003 11:23 PM Last Post: OneSadCookie |
|

