Handling keyboard events on cocoa
I have a textbox and every time something is typed into it. I want to captur开发者_开发知识库e 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?
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
}
You can use
- (void)controlTextDidChange:(NSNotification *)aNotification
This will be use full if you need to validate the data in your textField ....
精彩评论