如何检测iPhone中按下的键盘按键? [英] How to detect Keyboard key pressed in iphone?
问题描述
我想检测用户何时按下任何键盘键。
I want to detect whenever the user presses any keyboard key.
任何只在键入任何字符时调用的方法,而不是在显示键盘时调用。
Any method which is called only on typing any character and not when keyboard is shown.
谢谢!
推荐答案
您可以每次直接处理键盘事件用户按键:
You can directly handle keyboard events every-time a user presses a key :
如果 UITextField
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
// Do something here...
}
如果是 UITextView :
- (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text {
// Do something here...
}
因此,每次按下我们的每个键都会调用其中一个方法键盘。
So every-time one of these method is called for each key you press using keyboard.
您也可以使用NSNotificationCenter。您只需要在ViewDidLoad方法中添加任何这些。
You can use NSNotificationCenter also. You only need do add any of these in ViewDidLoad method.
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
UITextField:
[notificationCenter addObserver:self
selector:@selector(textFieldText:)
name:UITextFieldTextDidChangeNotification
object:yourtextfield];
然后你可以把你的代码放在方法 textFieldText:
:
Then you can put your code in method textFieldText:
:
- (void)textFieldText:(id)notification {
// Do something here...
}
UITextView
[notificationCenter addObserver:self
selector:@selector(textViewText:)
name:UITextViewTextDidChangeNotification
object:yourtextView];
然后你可以把你的代码放在方法 textViewText:
:
Then you can put your code in method textViewText:
:
- (void)textViewText:(id)notification {
// Do something here...
}
希望有所帮助。
这篇关于如何检测iPhone中按下的键盘按键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!