如何检测 iPhone 屏幕键盘上的触摸

发布于 2024-12-18 20:12:54 字数 234 浏览 2 评论 0原文

我试图检测用户在打字时触摸键盘上的按键的时间,例如,当按下某个键足够长的时间时,我会弹出一个对话框。

通过测量 touchesEndedtouchesBegan 之间的时间来检测常规视图的这一点很简单。但是,我没有从键盘收到任何事件。关于如何从键盘接收 touchesEndedtouchesBegan 事件有什么想法吗?

I'm trying to detect how long a user is touching a key on a keyboard when she is typing, so that, for instance, I popup a dialog when a certain key is pressed for a long enough time.

Detecting this for a regular view is trivial with measuring the time between touchesEnded and touchesBegan. However, I don't receive any events from the keyboard. Any ideas on how one can receive touchesEnded and touchesBegan events from the keyboard?

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

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

发布评论

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

评论(3

写给空气的情书 2024-12-25 20:12:54

您无法将您想要的内容添加到系统提供的键盘中。但是,您可以通过实现自己的自定义键盘来实现您想要的目的,该键盘可以具有您想要处理长按键的任何方法。请参阅上一个问题作为如何实现自定义键盘的示例。

实现此操作时,您应该使用 iOS 提供的 UILongPressGestureRecognizer

You cannot add what you want to the system-provided keyboard. However, you could achieve what you want by implementing your own custom keyboard, which could have whatever methods you wanted to handle long key presses. See previous this previous question as one example of how to approach implementing a custom keyboard.

When implementing this, rather than examining touchesEnded and touchesBegan to identify when a long touch occurred, you should the iOS-provided UILongPressGestureRecognizer.

浅语花开 2024-12-25 20:12:54

我不相信你能够做到这一点(特别是在应用程序商店应用程序中),因为系统本身有额外的菜单,当按住某些键时会显示,甚至可能没有屏幕键盘(蓝牙键盘) 。

I don't believe you will be able to do this (esp. in an App Store app) as the system itself has extra menus it displays when holding down on certain keys and there may not even be an on screen keyboard (bluetooth keyboard).

意中人 2024-12-25 20:12:54

将这些添加到 TextField 的侦听器中

// editing began:
[textField addTarget:self action:@selector(textFieldEditingDidBegin:) forControlEvents:UIControlEventEditingDidBegin];
// editing changed:
[textField addTarget:self action:@selector(editingChanged:)forControlEvents:UIControlEventEditingChanged];
// editing ended: 
[textField addTarget:self action:@selector(editingDidEnd:) forControlEvents:UIControlEventEditingDidEnd];

您还可以使用这些通知来了解键盘何时显示和隐藏:

// keyboard shows on screen:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow) name:UIKeyboardDidShowNotification object:nil];
// keyboard is hidden:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide) name:UIKeyboardDidHideNotification object:nil];

Add these to listeners to your TextField

// editing began:
[textField addTarget:self action:@selector(textFieldEditingDidBegin:) forControlEvents:UIControlEventEditingDidBegin];
// editing changed:
[textField addTarget:self action:@selector(editingChanged:)forControlEvents:UIControlEventEditingChanged];
// editing ended: 
[textField addTarget:self action:@selector(editingDidEnd:) forControlEvents:UIControlEventEditingDidEnd];

You can also use these notifications that knows when the keyboard shows and hides:

// keyboard shows on screen:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow) name:UIKeyboardDidShowNotification object:nil];
// keyboard is hidden:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide) name:UIKeyboardDidHideNotification object:nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文