簡體   English   中英

處理外部鍵盤按鍵而不在 iOS 應用程序的屏幕上顯示鍵盤

[英]Handling external keyboard presses without showing keyboard on screen on iOS app

我有一個私人 iOS 應用程序,我想使用通過藍牙連接的外部鍵盤進行控制。 我不希望我的應用程序中有任何UITextFieldUITextView ,或者至少我不希望鍵盤一直顯示。 有沒有辦法讓我從物理鍵盤上收聽這些鍵盤事件? 謝謝。

UIKeyCommands

要從鍵盤監聽按鍵,您可以覆蓋 ViewController 上的keyCommands變量(它是一個數組,因此您可以放置​​許多快捷方式):

override var keyCommands: [UIKeyCommand]? {
    return [
        UIKeyCommand(input: "Q",
                     modifierFlags: [],
                     action: #selector(self.close),
                     discoverabilityTitle: "Close app")
    ]
}

func close() {
    exit(0)
}

input是使用的鍵,例如CMD+Q(只放Q)。 Action 是要調用的選擇器,discoverabilityTitle 是快捷方式的標題,例如“關閉應用程序”。 當用戶按住 CMD 鍵時,它將顯示在 iPad 中。

它僅適用於 CMD 鍵和其他特殊鍵:

UIKeyInputUpArrow
UIKeyInputDownArrow
UIKeyInputLeftArrow
UIKeyInputRightArrow
UIKeyInputEscape

這對我很有幫助: https : //www.avanderlee.com/swift/uikeycommand-keyboard-shortcuts/

我必須添加 canBecomeFirstResponder 覆蓋才能使其工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM