所以我试图在我的React Native iOS应用中添加键盘快捷键。这样,用户可以在iPad上使用外部键盘来利用这些快捷方式。
我已经创建了一个AppDelegate.swift和RootViewController.swift以仅在我的React Native应用程序中与swift一起使用。到目前为止,我的基本应用程序正在运行。它还可以使用添加到RootViewController的KeyCommands的代码进行编译,但是在按cmd + s时不会触发操作,或者在iPad上按住cmd键时不会显示可用命令。
AppDelegate.swift
import Foundation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
var rootView: RootViewController = RootViewController()
self.window = UIWindow(frame: UIScreen.main.bounds)
var rootViewController = UIViewController()
rootViewController = rootView
self.window!.rootViewController = rootViewController;
self.window!.makeKeyAndVisible()
return true
}
}
RootViewController.swift
import Foundation
import UIKit
class RootViewController: UIViewController {
var bridge: RCTBridge!
override func viewDidLoad() {
super.viewDidLoad()
let jsCodeLocation = NSURL(string: "http://localhost:8081/index.bundle?platform=ios&dev=true")
let rootView = RCTRootView(bundleURL:jsCodeLocation as URL!, moduleName: "tinyWhaleApp", initialProperties: nil)
self.bridge = rootView?.bridge
self.view = rootView
}
override func becomeFirstResponder() -> Bool {
return true
}
override var keyCommands: [UIKeyCommand]? {
return [
UIKeyCommand(input: "s", modifierFlags: [.command], action: "saveFile:", discoverabilityTitle: "Save"),
]
}
func saveFile(sender: UIKeyCommand) {
print("saving file")
}
}
如果有人可以帮助我使用外接键盘在iPad上使用键盘快捷键,那就太好了。