macos iphone如何处理关键事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5216245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:47:25  来源:igfitidea点击:

how to handle key events in iphone

iphoneobjective-ciosmacosnsevent

提问by aqavi_paracha

Hi I am working on an iphone application and want to handle keyboard events in iphone. In Mac, there is a class NSEvent which handles both keyboard and mouse events, and in ios (iphone/ipad) the counterpart of NSEvent is UIEvent which handles only touch events. I know ios API does not provide this functionality, but how can i handle key events in iphone??? Any good tutorial or sth, to get started...

嗨,我正在开发一个 iphone 应用程序,想在 iphone 中处理键盘事件。在 Mac 中,有一个类 NSEvent 处理键盘和鼠标事件,而在 ios (iphone/ipad) 中,NSEvent 的对应物是 UIEvent,它只处理触摸事件。我知道 ios API 不提供此功能,但是我如何处理 iphone 中的关键事件???任何好的教程或某事,开始...

回答by Ishu

You cant directly code for keyboad;s key and there is no mouse in case of device.

您不能直接为键盘编码;s 键并且在设备的情况下没有鼠标。

you can make your logics for different kind of charectersets or you can make your logics in textField delgate methods or Textview Delegates method

您可以为不同类型的字符集制作逻辑,也可以在 textField 委托方法或 Textview 委托方法中制作逻辑

textView delegate

文本视图委托

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

textField delegate

文本字段委托

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

You can also use Notification for textField and Textview.

您还可以对 textField 和 Textview 使用通知。

For TextField use this

对于 TextField 使用这个

call this register method in viewDidLoad

在 viewDidLoad 中调用这个注册方法

-(void)registerForTextFieldNotifications {

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

    [notificationCenter addObserver:self
                           selector:@selector (handle_TextFieldTextChanged:)
                               name:UITextFieldTextDidChangeNotification
                             object:self.textField];

}


- (void) handle_TextFieldTextChanged:(id)notification {



    if([iSinAppObj.passCodeString isEqualToString:lockTextField.text])
    {   
        //code here
    }

}

and for text view you need to change only event name like this

对于文本视图,您只需要像这样更改事件名称

[notificationCenter addObserver:self
                               selector:@selector (handle_TextFieldTextChanged:)
                                   name:UITextViewTextDidChangeNotification
                                 object:self.textField];

回答by Jose Cherian