簡體   English   中英

iOS:如何檢測外部鍵盤上的修改鍵狀態變化?

[英]iOS: How do I detect modifier key state changes on an external keyboard?

我的應用程序具有可選的用戶體驗,可讓用戶按住外部鍵盤上的修飾鍵(Shift,Option,Command)以引發某些特定行為。 (當未連接鍵盤時,也會在屏幕上提供這種體驗。)

UIKeyCommand需要輸入字符串,並在按下組合鍵時觸發一次。 我想跟蹤隨時間推移的修飾鍵的狀態變化,因為用戶按下/釋放了它們。

是否存在iOS API,可讓我跟蹤外部鍵盤上的修改鍵的狀態?

您是否嘗試過使用空字符串作為輸入?

class ViewController: UIViewController {
    override var canBecomeFirstResponder: Bool {
        return true
    }

    override var keyCommands: [UIKeyCommand]? {
        return [
            UIKeyCommand(input: "", modifierFlags: [.shift], action: #selector(shiftPressed(key:))),
            UIKeyCommand(input: "", modifierFlags: [.alphaShift], action: #selector(capslockPressed(key:)))
        ]
    }

    @objc func shiftPressed(key: UIKeyCommand) {
        print("shift pressed \(key)")
    }

    @objc func capslockPressed(key: UIKeyCommand) {
        print("capslock pressed \(key)")
    }
}

暫無
暫無

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

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