I am working on an iOS 13+ project which includes a view that can be used as calculator. The number block used to input numbers and operators (+ - * / ...) is build using simple UIButtons
. The current value is shown in a UILabel
.
Now I would like to extend the existing code to also accept input from connected hardware keyboards. I have extended UILabel
to handle the UIResponder
method pressesBegan:withEvent
:
override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
guard let key = presses.first?.key else { return }
switch key.keyCode {
case .keyboard1:
case .keypad1:
insertNumber(1)
case .keyboard2:
case .keypad2:
insertNumber(2)
...
case .keypadPlus:
add()
...
default:
super.pressesBegan(presses, with: event)
}
}
This works fine when using a keyboard with a numberblock. However, I would like to also support keyboard without a numberblock. In this case the *
sign for example can only be entered using two keys Shift +
(on a Keyboard with German layout).
However, when typing Shift +
the presses:with:
method is called twice. Once for the Shift
key and once for the +
key. I am not sure why presses is Set
when each keystroke is handled separately.
So, how to detect multi key combinations?