iosswiftuipasteboarduikeycommand

How to detect a Command C in my app (Swift)?


I have a calculator app written in Swift, and it's fully compatible with custom keyboard shortcuts. I'm trying call a method when the user presses Command + C, however, whenever I try to assign the following custom shortcut, it just doesn't work:

UIKeyCommand(input: "c", modifierFlags: [.command], action: #selector(commandCAction))

The commandCAction method is never called, unlike the other shortcuts. I assume this is due to Apple reserving this shortcut. Are there any observers or methods that I can use to detect when the user presses Command + C on a physical keyboard?


Solution

  • I found the issue, for some reason this doesn't work on my simulator:

    UIKeyCommand(input: "c", modifierFlags: [.command], action: #selector(commandCAction))
    

    However, when I tried on a physical device, it worked! So that's a solution for someone with the same issue as me.