0

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?

1
  • 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? Commented Jul 22, 2011 at 12:37

2 Answers 2

1

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 } 
Sign up to request clarification or add additional context in comments.

Comments

0

You can use

- (void)controlTextDidChange:(NSNotification *)aNotification 

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.