Class

UIKeyCommand

An object that specifies a key press performed on a hardware keyboard and the resulting action.

Declaration

@interface UIKeyCommand : UICommand

Overview

Hardware keyboards allow a user to hold down the Control, Option, Command, or other modifier key and press another key in combination to initiate commands such as Cut, Copy, or Paste. You can use instances of this class to define custom command sequences that your app recognizes and then provide an appropriate response.

To use this class, you create instances and associate them with your app’s responder objects. Each responder has a keyCommands property that you can redefine and use to return the key command objects that responder supports. Key command sequences are generated only for devices with an attached hardware keyboard.

The system always has the first opportunity to handle key commands. Key commands that map to known system events (such as Cut, Copy and Paste) are automatically routed to the appropriate responder methods. For other key commands, the system looks for an object in the responder chain with a key command object that matches the pressed keys. If it finds such an object, it then searches the responder chain, looking for the first object that implements the corresponding action method, and calls the first one it finds.

iPad apps that run in macOS can use UIKeyCommand to create menu elelments that have keyboard shortcuts.

Topics

Creating a Key Command Object

+ commandWithTitle:image:action:input:modifierFlags:propertyList:

Creates a key command that you can use as a menu element with a shortcut key, or a shortcut key only for a view controller.

+ commandWithTitle:image:action:input:modifierFlags:propertyList:alternates:

Creates a key command with alternatives that you can use as a menu element with a shortcut key, or a shortcut key only for a view controller.

+ keyCommandWithInput:modifierFlags:action:

Creates a new key command object that matches the specified input.

+ keyCommandWithInput:modifierFlags:action:discoverabilityTitle:

Creates and returns a new key command object that matches the specified input and has a title.

Deprecated
Adding Menus and Shortcuts to the Menu Bar and User Interface

Provide quick access to useful actions by adding menus and keyboard shortcuts to your Mac Catalyst app.

Getting Information About the Key Command

title

The key command's title.

image

The key command's image.

input

The string of characters corresponding to the keys that must be pressed to match this key command.

action

The command's action-method selector.

modifierFlags

The bit mask of modifier flags that must be pressed to match this key command.

discoverabilityTitle

An elaborated title that explains the purpose of the key command.

attributes

The attributes indicating the style of the key command.

state

The state of the key command.

Getting Command Alternatives

alternates

An array of alternative actions to take for the key command.

UICommandAlternate

An object representing an alternative action for a command.

Associating Data

propertyList

An object that contains data to associate with the key command.

UICommandTagShare

A value that identifies a command as a Share menu.

Constants

UIKeyModifierFlags

Constants that indicate which modifier keys are pressed.

Input Strings for Special Keys

Constants that represent the text input strings that correspond to special nonvisible keys.

Relationships

Inherits From

See Also

Menu Elements and Keyboard Shortcuts

Adding Menus and Shortcuts to the Menu Bar and User Interface

Provide quick access to useful actions by adding menus and keyboard shortcuts to your Mac Catalyst app.

UIMenuElement

An object representing a menu, action, or command.

UIAction

A menu element that performs its action in a block.

UICommand

A menu element that performs its action in a selector.

UIMenuElementAttributes

Attributes that determine the style of the menu element.

UIMenuElementState

Constants that indicate the state of an action- or command-based menu element.