Cocoa Keyboard help

Member
Posts: 196
Joined: 2002.04
Post: #1
Hi,
I'm trying to get the arrow keys to work for my game, and it's not working. What am I doing wrong? Here's the code:

- (void)keyDown: (NSEvent *)theEvent {
NSString *keyChar = [theEvent characters];

if ( [keyChar isEqualToString:@""] ) {
//movement goes here
}

[self setNeedsDisplay:YES];
}

Thanks,
Iceman
Quote this message in a reply
Jeff Binder
Unregistered
 
Post: #2
Try something like this:

Code:
- (void)keyDownNSEvent *)theEvent {
    NSString *keyChars = [theEvent characters];
    int index;

    for (index = 0; index < [keyChars length]; index++) {
        if ([keyChars characterAtIndex:index] == NSUpArrowFunctionKey) {
            //movement goes here
        }

        //...
    }

    [self setNeedsDisplay:YES];
}
Quote this message in a reply
Member
Posts: 196
Joined: 2002.04
Post: #3
Thanks I like the feel of using arrow keys much better on my game Smile .

Iceman
Quote this message in a reply
AlainODea
Unregistered
 
Post: #4
To get a multiple-keys down keyboard interaction coded in Cocoa you can see an article I posted called GameKeyboardHandling on the Cocoa Dev wiki:
http://www.cocoadev.com/index.pl?GameKeyboardHandling
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #5
Again, we appreciate the link but this thread is 3 years old. Smile
Quote this message in a reply
nabobnick
Unregistered
 
Post: #6
You can also do this:
Code:
BOOL upArrow = NO;
BOOL downArrow = NO;

-(void)keyDown: (NSEvent *)theEvent {
NSString *keyChar = [theEvent characters];

if ( [keyChars characterAtIndex:index] == NSUpArrowFunctionKey ) {
upArrow = YES;
}
}

-(void)keyUp: (NSEvent *)theEvent {
NSString *keyChar = [theEvent characters];

if ( [keyChars characterAtIndex:index] == NSUpArrowFunctionKey ) {
upArrow = NO;
}
}

// Function called from timer or polling loop
-(void)gameMain {
if (upArrow) {
//Move up
}

if (downArrow) {
//Move down
}

[self setNeedsDisplay:YES];
}

EDIT: It's good info have (sorry about the old post).
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Keyboard Handling skyhawk 10 6,728 Oct 26, 2010 08:47 AM
Last Post: skyhawk
  keyboard limitations daveh84 8 4,624 Feb 13, 2009 02:48 PM
Last Post: ferum
  Keyboard Question rogue 6 3,877 Jul 10, 2003 06:36 AM
Last Post: rogue