iOS Swift keyCommands только чтение чисел

Пожалуйста, рассмотрите следующий код:

override var keyCommands: [UIKeyCommand]? {
    var commands:[UIKeyCommand] = []

    let numbers = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
    for number in numbers {
        let txt = number as String
        commands.append(UIKeyCommand(input: txt, modifierFlags: [], action: #selector(keyPressed(_:))))
    }
    commands.append(UIKeyCommand(input: "\r", modifierFlags: [], action: #selector(enterPressed)))
    return commands
}

По какой-то причине только цифры читаются. Это для сканера штрих-кода Bluetooth, который имитирует клавиатуру. Ключи-команды не способны читать буквенные символы или я что-то не так делаю?

1 ответ

Благодаря Эрику Айе я смог заставить это работать. Ниже мой модифицированный код. Возможно, это не самое элегантное решение, но оно работает:

 override var keyCommands: [UIKeyCommand]? {
    var commands:[UIKeyCommand] = []

    let numbers = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]

    for number in numbers {
        let txt = number as String
        if Int(txt) == nil {
            commands.append(UIKeyCommand(input: txt, modifierFlags: [UIKeyModifierFlags.Shift], action: #selector(keyPressed(_:))))
        } else {
            commands.append(UIKeyCommand(input: txt, modifierFlags: [], action: #selector(keyPressed(_:))))
        }
    }
    commands.append(UIKeyCommand(input: "\r", modifierFlags: [], action: #selector(enterPressed)))
    return commands
}
Другие вопросы по тегам