c# - 从屏幕和/或外部键盘检测 iOS 中的按键

标签 c# ios objective-c swift xamarin.ios

是否有任何可能的方法来检测 iOS 屏幕键盘和/或外部键盘(例如 these)上的所有按键?我知道捕获键入文本的“可接受”方法是使用不可见的文本框,但这似乎只能捕获“文本”,而不是实际的按键(没有 Ctrl、Alts、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

关于c# - 从屏幕和/或外部键盘检测 iOS 中的按键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46852404/

相关文章:

ios - 如何在 UITableView 中设置分隔符的全宽

c# - .NET Framework - 有什么方法可以让 Dictionary<> 更快一点?

c# - Request.QueryString 没有吗?或&可能吗?

c# - 应用内购买时出现 android.app.Service 连接泄漏错误

ios - 如何确定比赛得分是否已提交

ios - 在 View Controller 之间共享 NSURLsession 委托(delegate)实现

c# - On Error catch block 没有调用

ios - 核心数据(添加唯一约束后): annotation: repairing missing delete propagation for to-many relationship

objective-c - UIDeviceOrientationDidChangeNotification 与 UIApplicationDidChangeStatusBarOrientationNotification

objective-c - NSDictionary 无法正确保存数据