Mac Using key strokes

richifie

macrumors newbie
Original poster
Oct 4, 2009
11
0
How do i use key strokes to set off an action. Like on a game, pressing the left arrow key makes an imageView move two pixels to the left... or something.

So how do i do this?

Would it be in an "if" function, or would the keystroke set off an IBAction?

Thank you in advance,

-David, the MASSIVE noob.
 

gnasher729

macrumors P6
Nov 25, 2005
16,697
3,438
How do i use key strokes to set off an action. Like on a game, pressing the left arrow key makes an imageView move two pixels to the left... or something.

So how do i do this?

Would it be in an "if" function, or would the keystroke set off an IBAction?

Thank you in advance,

-David, the MASSIVE noob.
Neither.

NSView is a subclass of NSResponder. Read the documentation for NSResponder, then in your view, you override the NSResponder methods that you want to override.

For example add this method to your view and see what happens:

Code:
- (void)keyDown:(NSEvent *)theEvent
{
    NSLog (@"keyDown event %@", theEvent);
    [super keyDown:theEvent];
}
 
Comment

richifie

macrumors newbie
Original poster
Oct 4, 2009
11
0
Thank you, but one more thing...

Neither.

NSView is a subclass of NSResponder. Read the documentation for NSResponder, then in your view, you override the NSResponder methods that you want to override.

For example add this method to your view and see what happens:

Code:
- (void)keyDown:(NSEvent *)theEvent
{
    NSLog (@"keyDown event %@", theEvent);
    [super keyDown:theEvent];
}
Okay, so thank you very much for the help, and it helps a lot. Through reading the class reference i get that that code prints to the log the key that was pressed, by how would i perform an action when a specific key is pressed, for example: the "D" key.

Thank you in advance,

-David, the MASSIVE noob.
 
Comment

larswik

macrumors 68000
Sep 8, 2006
1,552
11
Are you learning how to program by trying to solve a specific problem, or are you learning using a book in a structured way?

If you are finishing for specific answers to solve your problem you more then likely will get something to work at some point but no idea why it works. Start at the beginning.

I started here "Learn C on the Mac".
 
Comment

chown33

Moderator
Staff member
Aug 9, 2009
8,671
4,942
vertical
Okay, so thank you very much for the help, and it helps a lot. Through reading the class reference i get that that code prints to the log the key that was pressed, by how would i perform an action when a specific key is pressed, for example: the "D" key.
The usual way: with if or switch statements, evaluating the value of NSEvent's keyCode method.
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.