<!--
{
  "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

[`convenience init(title: String, image: UIImage?, action: Selector, input: String, modifierFlags: UIKeyModifierFlags, propertyList: Any?, alternates: [UICommandAlternate], discoverabilityTitle: String?, attributes: UIMenuElement.Attributes, state: UIMenuElement.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.

[`+ (instancetype) commandWithTitle:(NSString *) title image:(UIImage *) image action:(SEL) action input:(NSString *) input modifierFlags:(UIKeyModifierFlags) modifierFlags propertyList:(id) 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.

[`+ (instancetype) commandWithTitle:(NSString *) title image:(UIImage *) image action:(SEL) action input:(NSString *) input modifierFlags:(UIKeyModifierFlags) modifierFlags propertyList:(id) propertyList alternates:(NSArray<UICommandAlternate *> *) 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.

[`convenience init(input: String, modifierFlags: UIKeyModifierFlags, action: Selector)`](/documentation/UIKit/UIKeyCommand/init(input:modifierFlags:action:))

Creates a key command that matches the specified input.

[`init?(coder: NSCoder)`](/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

[`var title: String`](/documentation/UIKit/UIKeyCommand/title)

The key command’s title.

[`var image: UIImage?`](/documentation/UIKit/UIKeyCommand/image)

The key command’s image.

[`var input: String?`](/documentation/UIKit/UIKeyCommand/input)

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

[`var action: Selector?`](/documentation/UIKit/UIKeyCommand/action)

The command’s action-method selector.

[`var modifierFlags: UIKeyModifierFlags`](/documentation/UIKit/UIKeyCommand/modifierFlags)

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

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

Constants that indicate which modifier keys are pressed.

[`var discoverabilityTitle: String?`](/documentation/UIKit/UIKeyCommand/discoverabilityTitle)

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

[`var attributes: UIMenuElement.Attributes`](/documentation/UIKit/UIKeyCommand/attributes)

The attributes indicating the style of the key command.

[`var state: UIMenuElement.State`](/documentation/UIKit/UIKeyCommand/state)

The state of the key command.

### Getting command alternatives

[`var alternates: [UICommandAlternate]`](/documentation/UIKit/UIKeyCommand/alternates)

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

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

An object representing an alternative action for a command.

### Localizing keyboard shortcuts

[`var allowsAutomaticLocalization: Bool`](/documentation/UIKit/UIKeyCommand/allowsAutomaticLocalization)

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

[`var allowsAutomaticMirroring: Bool`](/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

[`var wantsPriorityOverSystemBehavior: Bool`](/documentation/UIKit/UIKeyCommand/wantsPriorityOverSystemBehavior)

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

### Associating data

[`var propertyList: Any?`](/documentation/UIKit/UIKeyCommand/propertyList)

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

[`let UICommandTagShare: String`](/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

[`convenience init(input: String, modifierFlags: UIKeyModifierFlags, action: Selector, discoverabilityTitle: String)`](/documentation/UIKit/UIKeyCommand/init(input:modifierFlags:action:discoverabilityTitle:))

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

### Initializers

[`convenience init(title: String, subtitle: String?, image: UIImage?, selectedImage: UIImage?, preferredImageVisibility: UIMenuElement.ImageVisibility, action: Selector, input: String, modifierFlags: UIKeyModifierFlags, propertyList: Any?, alternates: [UICommandAlternate], discoverabilityTitle: String?, attributes: UIMenuElement.Attributes, state: UIMenuElement.State)`](/documentation/UIKit/UIKeyCommand/init(title:subtitle:image:selectedImage:preferredImageVisibility:action:input:modifierFlags:propertyList:alternates:discoverabilityTitle:attributes:state:))

## Relationships

### Conforms To

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

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`CVarArg`](/documentation/Swift/CVarArg)

[`NSSecureCoding`](/documentation/Foundation/NSSecureCoding)

[`Sendable`](/documentation/Swift/Sendable)

[`NSCoding`](/documentation/Foundation/NSCoding)

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

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Equatable`](/documentation/Swift/Equatable)

[`NSCopying`](/documentation/Foundation/NSCopying)

[`Hashable`](/documentation/Swift/Hashable)

### Inherits From

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

---

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)