1

I recognize which key user enter in textfield with below code.but i can't recognize keys like 'Caps lock' - 'shift' - 'control' - 'command' - 'option' - 'tab' how can i recognize them ?

- (void)keyUp:(NSEvent *)theEvent
{
    unichar keyChar = 0;
    keyChar = [theArrow characterAtIndex:0];
    NSString *aci=[NSString stringWithFormat:@"%d",keyChar];
}
  • possible dupe of http://stackoverflow.com/questions/5837041/how-to-get-the-caps-lock-pressed-shift-key-key-state-programmatically-on-ios-s – rishi May 09 '12 at 07:38

1 Answers1

1

Those are modifier keys and they don't generate NSKeyDown and NSKeyUp events. They generate NSFlagsChanged events. The corresponding NSResponder method is -flagsChanged:.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • thank you,it works.but how to recognize which key pressed in this method? –  May 09 '12 at 07:45
  • 1
    Check `[theEvent modifierFlags]`. For example, `if ([theEvent modifierFlags] & NSControlKeyMask) { /* Control key is pressed */ } else { /* Control key is not pressed */ }`. If you need to know when one of the modifier keys is *first* pressed, then you need to store the flags in an instance variable and compare the new flags with the old to see what changed. Be sure to initialize this recorded state based on the event that was current at the time you started caring about those key presses, using the `-currentEvent` method of either `NSWindow` or `NSApplication` if you don't have an event. – Ken Thomases May 09 '12 at 08:17
  • Thank you so much. just one more question with this two way i can't recognize when user press TAB key . shat should i do about it? –  May 09 '12 at 13:48
  • 1
    [Technical Q&A QA1454: How to make NSTextField accept tab, return and enter keys](https://developer.apple.com/library/mac/#qa/qa1454/) – Ken Thomases May 09 '12 at 13:58