<!--
{
  "availability" : [
    "iOS: 7.0.0 -",
    "iPadOS: 7.0.0 -",
    "macCatalyst: 13.1.0 -",
    "tvOS: -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UIKeyCommand",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(cs)UIKeyCommand"
  },
  "title" : "UIKeyCommand"
}
-->

# UIKeyCommand

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

```
@MainActor class UIKeyCommand
```

## 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`](/documentation/UIKit/UIResponder/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`](/documentation/UIKit/UIKeyCommand) to create menu elements that have keyboard shortcuts.

## Topics

### Creating a key command object

[`init(title:image:action:input:modifierFlags:propertyList:alternates:discoverabilityTitle:attributes:state:)`](/documentation/UIKit/UIKeyCommand/init(title:image:action:input:modifierFlags:propertyList:alternates:discoverabilityTitle:attributes:state:))

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:`](/documentation/UIKit/UIKeyCommand/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:`](/documentation/UIKit/UIKeyCommand/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.

[`init(input:modifierFlags:action:)`](/documentation/UIKit/UIKeyCommand/init(input:modifierFlags:action:))

Creates a key command that matches the specified input.

[`init(coder:)`](/documentation/UIKit/UIKeyCommand/init(coder:))

Creates a key command from data in an unarchiver.

[`init()`](/documentation/UIKit/UIKeyCommand/init())

Creates a key command.

[Adding menus and shortcuts to the menu bar and user interface](/documentation/UIKit/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 app built with Mac Catalyst.

[Navigating an app’s user interface using a keyboard](/documentation/UIKit/navigating-an-app-s-user-interface-using-a-keyboard)

Navigate between user interface elements using a keyboard and focusable UI elements in iPad apps and apps built with Mac Catalyst.

### Getting information about the key command

[`title`](/documentation/UIKit/UIKeyCommand/title)

The key command’s title.

[`image`](/documentation/UIKit/UIKeyCommand/image)

The key command’s image.

[`input`](/documentation/UIKit/UIKeyCommand/input)

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

[`action`](/documentation/UIKit/UIKeyCommand/action)

The command’s action-method selector.

[`modifierFlags`](/documentation/UIKit/UIKeyCommand/modifierFlags)

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

[`UIKeyModifierFlags`](/documentation/UIKit/UIKeyModifierFlags)

Constants that indicate which modifier keys are pressed.

[`discoverabilityTitle`](/documentation/UIKit/UIKeyCommand/discoverabilityTitle)

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

[`attributes`](/documentation/UIKit/UIKeyCommand/attributes)

The attributes indicating the style of the key command.

[`state`](/documentation/UIKit/UIKeyCommand/state)

The state of the key command.

### Getting command alternatives

[`alternates`](/documentation/UIKit/UIKeyCommand/alternates)

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

[`UICommandAlternate`](/documentation/UIKit/UICommandAlternate)

An object representing an alternative action for a command.

### Localizing keyboard shortcuts

[`allowsAutomaticLocalization`](/documentation/UIKit/UIKeyCommand/allowsAutomaticLocalization)

A Boolean value that determines whether the system automatically remaps keyboard shortcuts based on the keyboard layout.

[`allowsAutomaticMirroring`](/documentation/UIKit/UIKeyCommand/allowsAutomaticMirroring)

A Boolean value that determines whether the system automatically swaps input strings for some keyboard shortcuts when the interface direction changes.

### Overriding the key event delivery behavior

[`wantsPriorityOverSystemBehavior`](/documentation/UIKit/UIKeyCommand/wantsPriorityOverSystemBehavior)

A Boolean value that indicates whether the key command takes precedence over text input or focus movements.

### Associating data

[`propertyList`](/documentation/UIKit/UIKeyCommand/propertyList)

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

[`UICommandTagShare`](/documentation/UIKit/UICommandTagShare)

A value that identifies a command as a Share menu.

### Converting strings to key commands

[Input strings for special keys](/documentation/UIKit/input-strings-for-special-keys)

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

### Deprecated

[`init(input:modifierFlags:action:discoverabilityTitle:)`](/documentation/UIKit/UIKeyCommand/init(input:modifierFlags:action:discoverabilityTitle:))

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



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)