连接外部键盘时支持命令,控制和箭头键 [英] Support command, control and arrow keys when an external keyboard is attached

查看:140
本文介绍了连接外部键盘时支持命令,控制和箭头键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iOS应用程序,我想要做的就是提供更多支持基本命令键组合,如命令+ a,命令+ s,命令+ x,命令+ v和朋友,当用户连接蓝牙键盘时。

I have an iOS app where all I want to do is provide more support for basic command key combinations such as command+a, command+s, command+x, command+v and friends when a user has connected a bluetooth keyboard.

我知道这是可能的。诸如Textastic和许多其他编辑应用程序之类的应用程序支持这些功能。

I know this is possible. Applications such as Textastic and many other editing apps support these features.

我查看了UIResponder(它似乎只在弹出菜单中使用),UIKeyInput, UITextInput [Delegate]和其他API文档没有参考如何执行此操作。

I have looked at UIResponder (which appears to only be used by the pop-up menu), UIKeyInput, UITextInput[Delegate] and other API docs with no reference to how to do this.

使用(BOOL)canPerformAction很容易支持在UIMenuController中显示哪些选项: (SEL)动作withSender:(id)发件人。但是,当连接蓝牙键盘时,似乎不会调用此方法。因此,我需要专门为键盘或其他方法实现另一种方法。

It is easy to support which options are displayed in the UIMenuController using (BOOL)canPerformAction:(SEL)action withSender:(id)sender. However, this method does not appear to be called when a bluetooth keyboard is attached. So I either need to implement another method specifically for the keyboard or something else.

我现在怀疑文本子系统可能负责执行操作。但是,我找不到任何文件!此外,执行操作时不会调用任何UITextInput委托方法。我几乎认为解决方案是私有的,我需要特别询问Apple如何访问这些功能(我刚刚完成)。

I now suspect it could be that the text subsystem is responsible for performing the actions. However, there's no documentation that I can find! Also, none of the UITextInput delegate methods are called when an operation is performed. I'm almost thinking the solution is private and that I need to ask Apple specifically on how to get access to these features (which I have just done).

更新:

positionFromPosition:inDirection:offset:是用于检测何时按下箭头键的方法。在iOS调用此方法之后,它调用setSelectedText:。你需要解决剩下的管道问题。

positionFromPosition:inDirection:offset: is the method to use to detect when the arrow key is pressed. After iOS has called this method it then calls setSelectedText:. You'll need to work out the rest of the plumbing.

更新2:

我能够回答所有问题。请参阅标记的解决方案以获得答案。

I was able to answer all of the questions I had. Please refer to the marked solution for the answers.

推荐答案

想出来。我已经实现了UIMenuController方法来支持剪切,复制和粘贴。通过实现这些功能将蓝牙键盘连接到物理设备,您将自动获得基本命令选项(a,c,x,v和朋友)。您需要完全实现UIMenuController UIResponderStandardEditActions。我想要注意的是,在iOS模拟器中它将触发命令(a,c,x,v和friends)动作。当蓝牙键盘连接到物理设备时,我能够观察到的这些功能 - 即使在模拟器中启用了模拟硬件键盘。

Figured it out. I had already implemented the UIMenuController methods to support cut, copy and paste. By implementing these features and connecting a bluetooth keyboard to a physical device you will automatically get the basic command options (a, c, x, v and friends). You need to completely implement UIMenuController UIResponderStandardEditActions. I want to note that in the iOS simulator it will not trigger command (a, c, x, v and friends) actions. I was able to observe these features working only when a bluetooth keyboard was attached to a physical device -- even though "Simulate Hardware Keyboard" had been enabled in the simulator.

关于控制/选项键;当您完全实现UITextInput时,您还将获得使用箭头键,箭头键+控件,箭头键+选项等时的所有移动功能。查看Apple提供的SimpleTextInput示例代码文件,以便更好地了解如何实现此功能。

Regarding control/option keys; when you fully implement UITextInput you will also get all of the movement functionality when using arrow keys, arrow keys + control, arrow keys + option, etc. Look at the SimpleTextInput example code file provided by Apple for a better idea on how to implement this.

话虽如此,您可以创建自己的键盘组合。如果我想要这些功能,Apple会让我为它创建一张票,我不这样做......我真的不在乎支持他们提供的任何东西。因此,目前无法使用Command + s(保存)等命令。

With that said, you can not create your own keyboard combinations. Apple asked me to create a ticket for it if I wanted those features, which I don't... I really don't care to support anything more than what they have provided. So, commands like Command+s (save), etc. are not currently possible.

一个有趣的小故事; Apple说我想做的事情不可能(使用命令+ c,命令+ v等)与API。我想这表明你对他们的技术专长有何帮助。 非常令人失望。

A fun little story for you; Apple said that what I wanted to do was not possible (using command+c, command+v, etc.) with the API. I guess that shows how you helpful their technical expertise is. Very disappointing.

更新:

自在iOS 7中,Apple已经轻而易举地捕获了任何关键命令,esc等。以下是一个如何捕获各种不同键的示例:

Since iOS 7, Apple has made it trivial to capture any key command, esc, etc. Here is an example of how one could capture a variety of different keys:

return @[[UIKeyCommand keyCommandWithInput:UIKeyInputEscape modifierFlags:0 action:@selector(escapeKeyPressed:)],
         [UIKeyCommand keyCommandWithInput:@"f" modifierFlags:UIKeyModifierCommand action:@selector(commandFindPressed:)],
         [UIKeyCommand keyCommandWithInput:@"s" modifierFlags:UIKeyModifierCommand action:@selector(commandSavePressed:)],
         [UIKeyCommand keyCommandWithInput:@"b" modifierFlags:UIKeyModifierControl action:@selector(controlBKeyPressed:)],
         [UIKeyCommand keyCommandWithInput:@"f" modifierFlags:UIKeyModifierControl action:@selector(controlFKeyPressed:)],
         [UIKeyCommand keyCommandWithInput:@"r" modifierFlags:UIKeyModifierControl action:@selector(controlRKeyPressed:)]
         ];
...

- (void)commandSavePressed:(UIKeyCommand *)keyCommand
{
    [self saveFile];
}

这篇关于连接外部键盘时支持命令,控制和箭头键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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