###前提・実現したいこと
目的としては、Keyboard Extentionにてキーボードを押下したことをシステムから横取り(例えば文字キーを押した時に画面に文字キーの入力が直接されずに,ソフト側で取得して,ソフトで生成した文字列を入力する)して取得することです。
###発生している問題・エラーメッセージ
下記コードを実行し、Bluetoothキーボードを接続すると、iOS画面のキーボードエリアが消え、func performCommandにBreakを張っていても、ここのコードが実行されません。
Keybord Extentionでなく、ViewControllerにて同様のコードを実行すると、”Q””W””E”の物理キーを押下すると、DebugAreaにそれぞれのキーが押されたことをPrintにて出力します。
下記のコードは最低限と思う部分を切り出しています。
下記のコードでは,KeyboardExtensionではキーボードの入力が拾えません。
直すべきところや、改善するためのヒントとなるものなど、何でもいいので教えていただけないでしょうか。
よろしくお願いいたします。
###該当のソースコード
import UIKit
class KeyboardViewController: UIInputViewController {
@IBOutlet var nextKeyboardButton: UIButton! private enum InputKey: String { case Key_Q = "Q" case Key_W = "W" case KEY_E = "E" } override func updateViewConstraints() { super.updateViewConstraints() // Add custom view sizing constraints here } override func viewDidLoad() { super.viewDidLoad() // Perform custom UI setup here self.nextKeyboardButton = UIButton(type: .system) self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), for: []) self.nextKeyboardButton.sizeToFit() self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false self.nextKeyboardButton.addTarget(self, action: #selector(handleInputModeList(from:with:)), for: .allTouchEvents) self.view.addSubview(self.nextKeyboardButton) self.nextKeyboardButton.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true self.nextKeyboardButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated } override func textWillChange(_ textInput: UITextInput?) { // The app is about to change the document's contents. Perform any preparation here. } override func textDidChange(_ textInput: UITextInput?) { // The app has just changed the document's contents, the document context has been updated. var textColor: UIColor let proxy = self.textDocumentProxy if proxy.keyboardAppearance == UIKeyboardAppearance.dark { textColor = UIColor.white } else { textColor = UIColor.black } self.nextKeyboardButton.setTitleColor(textColor, for: []) } override var keyCommands: [UIKeyCommand]? { return [ UIKeyCommand(input: InputKey.Key_W.rawValue, modifierFlags: .init(rawValue: 0), action: #selector(self.performCommand(sender:))), UIKeyCommand(input: InputKey.Key_Q.rawValue, modifierFlags: .init(rawValue: 0), action: #selector(self.performCommand(sender:))), UIKeyCommand(input: InputKey.KEY_E.rawValue, modifierFlags: .init(rawValue: 0), action: #selector(self.performCommand(sender:))) ] } func performCommand(sender: UIKeyCommand) { guard let key = InputKey(rawValue: sender.input) else { return } switch key { case .Key_Q: print ("Q") return case .Key_W: print ("W") return case .KEY_E: print ("E") return } }
}
###試したこと
また、テスト時にoverride var canBecomeFirstResponder: Bool { get { return true } }というコードが必要かと思い、追加しましたが、キーボードの入力が拾えません。
###補足情報(言語/FW/ツール等のバージョンなど)
iOS10 XCODE8.1 SWIFT3
あなたの回答
tips
プレビュー