键盘 iPhone

发布于 2024-07-20 21:47:15 字数 50 浏览 5 评论 0原文

是否有可能知道用户何时触摸iphone键盘? 当用户触摸键盘上的某个按钮时...:/

Is possible to know when the user touch the keyboard iphone? When the user touch some button from keyboard... :/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随风而去 2024-07-27 21:47:15

最简单的方法是使用 TextField。 即使您的 UI 不需要一个,您也可以将其框架设置为零,这样它就不会显示在屏幕上。 然后,您可以使用文本字段的委托回调方法来访问按下的键。

- (void)viewDidLoad {
    [super viewDidLoad];
    //CGRectZero because we don't want the textfield to be shown onscreen
    UITextField *f = [[UITextField alloc] initWithFrame:CGRectZero];
    //We set the delegate so we can grab keypressed
    f.delegate = self; 
    [self.view addSubview:f];
    [f becomeFirstResponder];  //Show the keyboard
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
                                                       replacementString:(NSString *)string {
    if (string.length >0) {
       NSLog(@"%@ Pressed",string);
    }
    else {
       NSLog(@"Backspcae pressed");
    }        
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    NSLog(@"return pressed");
    return YES;
}

注意:为了避免编译器警告,请确保在 .h 文件中该类明确表示它实现了 UITextFieldDelegate 协议。 IE:

@interface MyViewController : UIViewController <UITextFieldDelegate> 

The easiest way is to use a TextField. Even is your UI Does not call for one, you can set it's frame to zero so it doesnt show up onscreen. Then you can get access to the keys pressed by using the text field's delegate callback methods.

- (void)viewDidLoad {
    [super viewDidLoad];
    //CGRectZero because we don't want the textfield to be shown onscreen
    UITextField *f = [[UITextField alloc] initWithFrame:CGRectZero];
    //We set the delegate so we can grab keypressed
    f.delegate = self; 
    [self.view addSubview:f];
    [f becomeFirstResponder];  //Show the keyboard
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
                                                       replacementString:(NSString *)string {
    if (string.length >0) {
       NSLog(@"%@ Pressed",string);
    }
    else {
       NSLog(@"Backspcae pressed");
    }        
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    NSLog(@"return pressed");
    return YES;
}

Note: to avoid a compiler warning, make sure in your .h file the class explicitly says it implements the UITextFieldDelegate protocal. ie:

@interface MyViewController : UIViewController <UITextFieldDelegate> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文