检测按键在虚拟键盘上 [英] Detect key press on virtual keyboard

查看:112
本文介绍了检测按键在虚拟键盘上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为儿子做一个小孩子的游戏,涉及虚拟键盘上的计时。

I am working on a small children's game for my son that involves timing on the virtual keyboard.

是否可以检测到按下虚拟键盘上的键的时间?即获得触摸通知(touchesBegan)?我不需要知道按下哪个键,只是当按下动作开始时。

Is possible to detect when a key on the virtual keyboard is pressed? I.e. get a touch notification (touchesBegan)? I do not need to know which key is pressed, just if and when the press action started.

推荐答案

直接从虚拟键盘获取触摸事件是不可能的。但是,您可以使用放在屏幕外的 UITextField ,以便它不可见并调用 becomeFirstResponder 来显示键盘。然后,当 Khomsan 建议时,您可以实现委托方法 textView:shouldChangeTextInRange:每当按下一个键时都会收到通知。

It's not possible to get the touch events directly from the virtual keyboard. However, you could use a UITextField that you place offscreen, so that it's not visible and call becomeFirstResponder to show the keyboard. Then, as Khomsan suggested, you could implement the delegate method textView:shouldChangeTextInRange: to be notified whenever a key is pressed.

更清洁的可能性是写一个自定义实现 UIKeyInput 协议的 UIControl 。您只需要提供 insertText的实现: deleteBackward hasText (对于这个,你可以简单地总是返回 YES )。要使键盘可见,您需要再次为自定义控件调用 becomeFirstResponder

A cleaner possibility would be to write a custom UIControl that implements the UIKeyInput protocol. You would only need to provide implementations for insertText:, deleteBackward and hasText (for this one, you can simply always return YES). To make the keyboard visible, you would again have to call becomeFirstResponder for your custom control.

这两种方法有一个共同点,你只会在释放键时通知(以便输入文字),而不是触摸实际开始时。正如我所说,不可能直接获得 tochesBegan 事件。如果您需要,您可能需要实现自己的屏幕键盘。

Both of these methods have in common that you only will be notified when the key is released (so that text is entered), not when the touch actually begins. As I said, it's not possible to get the tochesBegan event directly. If you need that, you would probably have to implement your own onscreen keyboard.

这篇关于检测按键在虚拟键盘上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆