简体   繁体   中英

Detecting Keypresses in iOS from On Screen and/or External Keyboard

Is there any possible way to detect ALL key presses on the iOS on screen keyboard and/or on an external keyboard like these ? I know the "accepted" way to catch typed text is to use an invisible textbox, but that seems to only catch the "text", not actual key presses (no Ctrl, Alts, Shift, etc.)

I can't find anything in the docs that suggests this is possible, though it seems pretty basic. Am I missing something, or can we really not achieve this in iOS?

Thanks!

You can override the keyCommands in ViewController to add the trigger action:

For example:

            //MyViewController

          override var keyCommands: [UIKeyCommand]?{

            //Get UIKeyCommand

                var keys = [UIKeyCommand]()

            //Add triggle for Num only

                    for num in "123456789".characters {

                        keys.append(

     UIKeyCommand(input:String(num),modifierFlags: 
     UIKeyModifierFlags(rawValue: 0), action:
     #selector(MyViewController.keyCommand(_:)), discoverabilityTitle:
      NSLocalizedString("keydiscover_number", 
      comment: "KeyDiscover      Number")))


                        }
        }



 //The Triggle 

func keyCommand(_ sender: UIKeyCommand) {
                        print("Number Clicked");
                    }

https://developer.apple.com/documentation/uikit/uiresponder/1621141-keycommands

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM