繁体   English   中英

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

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

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

我在文档中找不到任何暗示这可能的方法,尽管看起来很基本。 我是否缺少某些东西,还是我们真的无法在iOS中实现?

谢谢!

您可以在ViewController中重写keyCommands以添加触发操作:

例如:

            //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

暂无
暂无

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

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