简体   繁体   English

通过屏幕和/或外部键盘检测iOS中的按键

[英]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 ? 是否有任何可能的方法来检测屏幕键盘和/或此类外部键盘上iOS上的所有按键? 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.) 我知道捕获键入文本的“可接受”方式是使用不可见的文本框,但这似乎仅捕获“文本”,而不是实际按键(没有Ctrl,Alt,Shift等)。

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? 我是否缺少某些东西,还是我们真的无法在iOS中实现?

Thanks! 谢谢!

You can override the keyCommands in ViewController to add the trigger action: 您可以在ViewController中重写keyCommands以添加触发操作:

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 https://developer.apple.com/documentation/uikit/uiresponder/1621141-keycommands

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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