0
votes

I have a textbox and every time something is typed into it. I want to capture the key pressed and perform some action. For this I need to capture the keyboard event. Currently I am trying to override the keyDown: function but it wont work.

So how can I do it?

2
It always helps to characterize "won't work." Crashes? Nothing happens? Keys appear in box, but no keyDown messages? Code is helpful, too. And is this a NSTextField or a NSTextView? Do you really need the event itself, or just the character? - Caleb

2 Answers

1
votes

Don't override keyDown. Register for notifications when the text control text has changed. The following will work for an NSTextField:

// Listen for change events on fields
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(valueChanged:)
                                             name:@"NSControlTextDidChangeNotication"
                                           object:textField ];

...

 (void) valueChanged:(NSNotification*)notification {
  // TODO  -- look at the stringValue of your TextField
}
0
votes

You can use

- (void)controlTextDidChange:(NSNotification *)aNotification

This will be use full if you need to validate the data in your textField ....