2

This is to implement a keyboard based game controller. Left/Right arrows makes the character walk. shift+left/right makes the character run.

Here's the code I'm using so far:

- (void)keyDown:(NSEvent *)event{

    if ([event modifierFlags] & NSShiftKeyMask) {
        NSLog(@"Shift key pressed");
    }
    // logic follows
}

This works fine if shift is pressed before pressing an arrow key. But if an arrow key is pressed and you need to accelerate, pressing shift won't cause anything to happen...

So, I see this kind of answer: https://stackoverflow.com/a/420691/987818

But I don't understand where this NSResponder is being used. For info, I use Cocos2D (objc game engine).

thanks for any leads :-) J.

Community
  • 1
  • 1
Jem
  • 6,226
  • 14
  • 56
  • 74
  • 1
    For an example of implementing flagsChanged check out the KKInput implementation of Kobold2D: https://github.com/kobold2d/Kobold2D/blob/master/Kobold2D/__Kobold2D__/kobold2d/KKInputKeyboard.m – CodeSmile Jan 30 '12 at 22:30
  • @LearnCocos2D Hey thanks Steffen. That KK file just solved all it all! Great stuff, now my CCLayer implementation properly handles the shift key using the built-in -(BOOL)ccFlagsChanged:(NSEvent*)event; Nice! Have a great day. – Jem Jan 31 '12 at 08:43

1 Answers1

3

You need to implement flagsChanged: in the same class where you implement this -keyDown: method, or just any NSResponder subclass that may be able to catch this event (e.g. the NSApplication).

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005