Using key strokes

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

  1. macrumors newbie

    Joined:
    Oct 4, 2009
    #1
    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. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    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];
    }
    
     
  3. thread starter macrumors newbie

    Joined:
    Oct 4, 2009
    #3
    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. macrumors 68000

    Joined:
    Sep 8, 2006
    #4
    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. macrumors 603

    Joined:
    Aug 9, 2009
    #5
    The usual way: with if or switch statements, evaluating the value of NSEvent's keyCode method.
     

Share This Page