Keyboard shortcuts for standard iPadOS 15 keyboard shortcuts

My iPad app supports features such Copy (cmd-C) and Paste (cmd-V). How can I get these to show in the Edit menu when I hold down the Command key?

Undo (cmd-Z) and Redo (shift-cmd-Z) show perfectly. Looks like the system internally looks at UndoManager. Same with Hide Sidebar: system detected presence of Sidebar and is showing the keyboard shortcut to hide it.

Ramon.

Replies

Key commands use the target-action pattern, meaning they look at whether something in the responder chain implements the action method for the command.

For example, the action method for the Copy command is copy:: https://developer.apple.com/documentation/uikit/uiresponderstandardeditactions/2354191-copy

So as long as anything in the responder chain implements a copy: method with the same method signature, then the copy command will be available (and it will call that method when the key command is triggered). All UIKit text editing views, such as UITextField and UITextView, already implement the standard edit methods like copy:, paste:, cut:, and so on, so if the user is editing text, then these commands will work automatically for text editing. However, you can still implement the methods in your own views to get them to work there as well.

You can also have more fine-grained control over when the copy command is available by implementing the canPerformAction:withSender: method (in the same object that also implements the copy: method). For example, you can return NO from this method for the copy: action, if there's no content to copy. That will suppress the Copy command from working or appearing in the keyboard shortcut menu if there's nothing to copy. Do ensure you call super for unhandled cases in that method, though.