Using key strokes

Discussion in 'Mac Programming' started by richifie, Jun 10, 2012.

  1. richifie macrumors newbie

    Oct 4, 2009
    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.
  2. gnasher729 macrumors P6


    Nov 25, 2005

    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:

    - (void)keyDown:(NSEvent *)theEvent
        NSLog (@"keyDown event %@", theEvent);
        [super keyDown:theEvent];
  3. richifie thread starter macrumors newbie

    Oct 4, 2009
    Thank you, but one more thing...

    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.
  4. larswik macrumors 68000

    Sep 8, 2006
    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".
  5. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    The usual way: with if or switch statements, evaluating the value of NSEvent's keyCode method.

Share This Page