简体   繁体   English

iOS UITextInput 控件:从硬件键盘接收向前删除键

[英]iOS UITextInput control: Receiving forward-delete key from hardware keyboard

I have implemented a complicated UITextInput that works quite well on iOS save for one missing feature:我已经实现了一个复杂的 UITextInput,除了缺少一个功能外,它在 iOS 上运行良好:

  • when the user has a hardware keyboard connected (at least, in the simulator) and no text is selected, and presses Forward Delete, nothing happens.当用户连接了硬件键盘(至少在模拟器中)并且没有选择文本时,并按下 Forward Delete 键,什么也没有发生。

Note that I am NOT referring to the key labeled Delete on a Mac, which is just a Backspace key -- that works correctly already, using an implementation of the UIKeyInput deleteBackward method.请注意,我指的不是 Mac 上标记为 Delete 的键,它只是一个 Backspace 键 - 使用 UIKeyInput deleteBackward 方法的实现已经可以正常工作。

Two questions:两个问题:

  1. Is it possible for iOS to process Forward Delete correctly (for example a Bluetooth Mac hardware keyboard with Fn + Delete aka not Backspace, but Forward Delete). iOS 是否有可能正确处理转发删除(例如,带有 Fn + Delete 的蓝牙 Mac 硬件键盘,又名不是 Backspace,而是转发删除)。 This works on my Mac laptop?这适用于我的 Mac 笔记本电脑吗? (The answer seems like a yes because I just tested Forward Delete on a hardware keyboard (my host Mac dev machine) in the Simulator in Safari.) (答案似乎是肯定的,因为我刚刚在 Safari 模拟器中的硬件键盘(我的主机 Mac 开发机器)上测试了 Forward Delete。)

  2. If it is possible and common in other apps, how do I intercept this key command?如果可能并且在其他应用程序中很常见,我该如何拦截此按键命令? I cannot find what to send as the string to create a UIKeyCommand, nor can I find any API method that makes sense to do this in an easy fashion.我找不到要发送的内容作为创建 UIKeyCommand 的字符串,也找不到任何 API 方法以简单的方式执行此操作。

Note also that selecting text and pressing the Forward Delete key works properly already in this custom text editor, so it appears iOS knows about this key.另请注意,选择文本并按 Forward Delete 键已在此自定义文本编辑器中正常工作,因此 iOS 似乎知道此键。

FYI this code is in C# but answers to part (2) in Swift or Objective-C would be welcome as well.仅供参考,此代码是在 C# 中编写的,但也欢迎在 Swift 或 Objective-C 中回答第 (2) 部分。

(My current best guess might be try messing with the code in this question ? -- Seems like a real headache just to get this one fairly common key?) (我目前最好的猜测可能是尝试弄乱这个问题中的代码? - 只是为了获得这个相当常见的密钥似乎真的很头疼?)

UPDATE:更新:

Now pressing Delete (right-delete not backspace) somehow works on iOS, at least the simulator.现在按 Delete(右键删除而不是退格键)在 iOS 上以某种方式工作,至少在模拟器上是这样。 It may have been a bug in the TextInput system since now when I press the right-Delete key, the system calls my code to select a single character to the right of my cursor and then calls my DeleteBackwards code to delete the selection (which code knows how to delete selected text).这可能是 TextInput 系统中的一个错误,因为现在当我按下右删除键时,系统调用我的代码来选择光标右侧的一个字符,然后调用我的 DeleteBackwards 代码来删除选择(哪个代码知道如何删除选定的文本)。 Strange but happy this is working as expected now, "out of the box".奇怪但很高兴这现在按预期工作,“开箱即用”。

You can use Unicode code point 0x7f , for example in Swift:您可以使用 Unicode 代码点0x7f ,例如在 Swift 中:

UIKeyCommand(input: "\u{7f}", modifierFlags: [], action: #selector(forwardDelete))

Objective-C:目标-C:

[UIKeyCommand keyCommandWithInput:[NSString stringWithFormat:@"%C", 0x7f] modifierFlags:0 action:@selector(forwardDelete)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM