UIKeyCommand Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/UIKit.framework
Availability
Available in iOS 7.0 and later.
Declared in
UIResponder.h

Overview

The UIKeyCommand class specifies a key presses performed on a hardware keyboard and the resulting action that should take place. Hardware keyboards allow a user to hold down the Control, Option, Command, or other modifier key and press a 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, UIKit 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 walks the responder chain looking for the first object that implements the corresponding action method and calls the first one it finds.

Tasks

Creating a Key Command Object

Getting the Key Command Attributes

Properties

input

The string of characters corresponding to the keys that must be pressed to match this key command. (read-only)

@property(nonatomic, readonly) NSString *input
Availability
  • Available in iOS 7.0 and later.
Declared In
UIResponder.h

modifierFlags

The bit mask of modifier flags that must be pressed to match this key command. (read-only)

@property(nonatomic, readonly) UIKeyModifierFlags modifierFlags
Availability
  • Available in iOS 7.0 and later.
Declared In
UIResponder.h

Class Methods

keyCommandWithInput:modifierFlags:action:

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

+ (UIKeyCommand *)keyCommandWithInput:(NSString *)input modifierFlags:(UIKeyModifierFlags)modifierFlags action:(SEL)action
Parameters
input

The keys that must be pressed by the user. The string must contain one or more characters corresponding to the keys that were pressed. For a list of special characters that do not have a textual representation, see “Input Strings for Special Keys”.

modifierFlags

The bit mask of modifier keys that must be pressed. You can use this parameter to specify which modifier keys (Command, Option, and so on) must also be pressed by the user. You may specify more than one modifier key. For a list of possible values, see “UIKeyModifierFlags”.

action

The action method to execute on the responder object.

Return Value

The key command object.

Discussion

After creating a key command object, you associate it with a responder object by returning it from that object’s keyCommands property. You might have to reimplement that property to return your key command objects.

Availability
  • Available in iOS 7.0 and later.
Declared In
UIResponder.h

Constants

UIKeyModifierFlags

These constants indicate which modifier keys are pressed.

typedef {     UIKeyModifierAlphaShift     = 1 << 16,
   UIKeyModifierShift          = 1 << 17,
   UIKeyModifierControl        = 1 << 18,
   UIKeyModifierAlternate      = 1 << 19,
   UIKeyModifierCommand        = 1 << 20,
   UIKeyModifierNumericPad     = 1 << 21,
} UIKeyModifierFlags;
Constants
UIKeyModifierAlphaShift

The Caps Lock key is currently engaged.

Available in iOS 7.0 and later.

Declared in UIResponder.h.

UIKeyModifierShift

The Shift key is pressed.

Available in iOS 7.0 and later.

Declared in UIResponder.h.

UIKeyModifierControl

The Control key is pressed.

Available in iOS 7.0 and later.

Declared in UIResponder.h.

UIKeyModifierAlternate

The Option key is pressed.

Available in iOS 7.0 and later.

Declared in UIResponder.h.

UIKeyModifierCommand

The Command key is pressed.

Available in iOS 7.0 and later.

Declared in UIResponder.h.

UIKeyModifierNumericPad

The key is located on the numeric keypad.

Available in iOS 7.0 and later.

Declared in UIResponder.h.

Input Strings for Special Keys

These constants represent the text input strings that correspond to special non visible keys.

NSString *const UIKeyInputUpArrow;
NSString *const UIKeyInputDownArrow;
NSString *const UIKeyInputLeftArrow;
NSString *const UIKeyInputRightArrow;
NSString *const UIKeyInputEscape;
Constants
UIKeyInputUpArrow

The string representing the up arrow key.

Available in iOS 7.0 and later.

Declared in UIResponder.h.

UIKeyInputDownArrow

The string representing the down arrow key.

Available in iOS 7.0 and later.

Declared in UIResponder.h.

UIKeyInputLeftArrow

The string representing the left arrow key.

Available in iOS 7.0 and later.

Declared in UIResponder.h.

UIKeyInputRightArrow

The string representing the right arrow key.

Available in iOS 7.0 and later.

Declared in UIResponder.h.

UIKeyInputEscape

The string representing the Escape key.

Available in iOS 7.0 and later.

Declared in UIResponder.h.