Read from an external bluetooth keyboard without UITextField

1.8k views Asked by At

I need to read text from an external bluetooth keyboard (an Inateck barcode scanner) on iOS. I'm searching for a solution without using an hidden UITextField like a listener ?

2

There are 2 answers

3
Paulw11 On BEST ANSWER

Unfortunately there is no lower-level API to intercept keystrokes on iOS. If your barcode scanner emulates a keyboard (HID Profile) then your only option is to pair it as a keyboard and direct input into a UITextField. This text field can be hidden as you point out.

3
Tuan Nguyen On

I had the same problem with an external keyboard (although read text from BUSICOM Desktop 1D/2D/QR Code Reader through a cable instead of Bluetooth).

My solution is to implement UIKeyInput for the UIViewController.

Swift 5.1:

// MARK: - UIKeyInput
let keyboardHiderView = UIView()
extension QRScannerViewController: UIKeyInput {
    override var canBecomeFirstResponder: Bool { true }
    var hasText: Bool { false }

    override var inputView: UIView? {
        return keyboardHiderView
    }

    func insertText(_ text: String) {
        // Character by character input in here
    }

    func deleteBackward() {}
}

Hoping to help someone.