Is there a way to detect when the "Return" key is pressed from an external keyboard connected to an iOS device as opposed to a tap on the onscreen keyboard's "Return" key?
Is this possible with public APIs?
My class is serving as UITextFieldDelegate and I get the call from:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
But when it's called, textField.text returns the characters present in the text field but not the carriage return that was issued.
-textField:shouldChangeCharactersInRange:replacementString isn't invoked with the "Return" key press for both physical or virtual keyboards.
In iOS 7+, you can use UIKeyCommand to distinguish Return via hardware keyboard.
extension ViewController {
override var keyCommands: [UIKeyCommand]? {
return [
UIKeyCommand(input: "\r",
modifierFlags: [],
action: #selector(didPressReturnOnHardwareKeyboard)),
]
}
@objc
private func didPressReturnOnHardwareKeyboard() {
// Handle action.
}
}
A subclass of UITextField should work:
protocol TextFieldKeyDetectionDelegate: AnyObject {
func enterKeyWasPressed(textField: UITextField)
func shiftEnterKeyPressed(textField: UITextField)
}
class TextFieldWithKeyDetection: UITextField {
weak var keyDelegate: TextFieldKeyDetectionDelegate?
override var keyCommands: [UIKeyCommand]? {
[UIKeyCommand(input: "\r", modifierFlags: .shift, action: #selector(shiftEnterKeyPressed)),
UIKeyCommand(input: "\r", modifierFlags: [], action: #selector(enterKeyPressed))]
}
@objc func shiftEnterKeyPressed(sender: UIKeyCommand) {
keyDelegate?.shiftEnterKeyPressed(textField: self)
}
@objc func enterKeyPressed(sender: UIKeyCommand) {
keyDelegate?.enterKeyWasPressed(textField: self)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With