Keys ......again

Member
Posts: 102
Joined: 2005.01
Post: #1
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
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #2
Does it beep regardless of what key you hit?
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #3
yea any key i hit makes it do the click-like error sound

its pretty sad...
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #4
Your view needs to be set as the initial first responder. This can be done in IB.
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #5
i made a connection from the window to the view makeing it the initial first responder, it does the exact same thing
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2006.10
Post: #6
Add this to your view
Code:
- (void)awakeFromNib { [[self window] makeFirstResponder:self]; }
- (BOOL)acceptsFirstResponder { return YES; }
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #7
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?
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2006.10
Post: #8
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Spaces on Snow Leopard and ctrl/arrow keys GolfHacker 19 14,679 Dec 4, 2012 03:18 PM
Last Post: bmantzey
  Multiple Keys down in SDL silver9172 2 4,757 May 30, 2009 06:16 PM
Last Post: scgames
  [NSEvent keyCode] to actual keys? teknein 8 9,082 Sep 5, 2007 07:27 AM
Last Post: teknein
  Keys I Can Use In Games Nick 10 5,558 Jul 22, 2004 11:49 PM
Last Post: AnotherJake
  Configurable keys - describe key that was pressed MattDiamond 13 7,097 Oct 27, 2003 11:23 PM
Last Post: OneSadCookie