且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iPhone / iPad应用程序使用键盘快捷键?

更新时间:2023-01-01 12:27:26

虽然这个问题相当陈旧,但Apple已经在iOS 7中添加了对键盘特定快捷方式的支持。查看 UIKeyCommand类,用于硬件键盘快捷键的特殊支持。我们的高级步骤是:

While this question is fairly old, Apple has added support for keyboard specific shortcuts in iOS 7. Check out the UIKeyCommand class in the UIKit Framework for special support for hardware keyboard shortcuts. The high level steps for us are:


  1. 使用静态 keyCommandWithInput:modifierFlags:action: UIKeyCommand 类上的方法,用于为带有修饰符的指定键创建 UIKeyCommand 的实例。请注意,系统处理的任何键盘快捷键(如Command-C for copy)都不起作用。您还将传递将在使用键盘快捷键时调用的操作选择器

  2. 将此新创建的 UIKeyCommand 与您的响应器对象相关联(s)从响应者对象的 keyCommand 属性中返回它们。

  3. 按下键时,将调用您的选择器。

  1. Use the static keyCommandWithInput:modifierFlags:action: method on the UIKeyCommand class to create an instance of the UIKeyCommand for the specified keys with modifiers. Note that any keyboard shortcuts handled by the system (like Command-C for copy) will not work. You will also pass an action selector that will be called when the keyboard shortcut is used
  2. Associate this newly created UIKeyCommand with your responder object(s) by returning them from the responder object's keyCommand property.
  3. When the keys are pressed, your selector will be called.