Reputation: 533
I am trying to detect if the backspace key has been pressed on the iPhone keyboard. The logic I wrote works perfectly on android, but not on iPhone or the iPhone simulator.
this is my code:
Padding(
padding: const EdgeInsets.all(28.0),
child: RawKeyboardListener(
onKey: (RawKeyEvent event) {
if(event.runtimeType == RawKeyDownEvent){
if (event.logicalKey == LogicalKeyboardKey.backspace || event.physicalKey == PhysicalKeyboardKey.backspace) {
_focusNodes[i]?.unfocus();
if (i != 0) {
_focusNodes[i - 1]?.requestFocus();
}
}
}
},
focusNode: FocusNode(),
child: TextField(
key: Key('PinInput'),
controller: TextEditingController(),
focusNode: FocusNode(),
),
),
),
This piece of code works perfectly in android, but the onKey
callback in the RawKeyboardListener is not being called when I hit a key in an iPhone or iPhone simulator.
How do I handle this?
Upvotes: 2
Views: 1306
Reputation: 1313
According to the flutter api, LogicalKeyboardKeys like you are using in your code are not supported on iOS. Learn more here.
You can use the map to "translate" the iOS key codes to your expected LogicalKeyboardKey. In this map you will find that the iOS key code 42 is what you are looking for: 42: LogicalKeyboardKey.backspace,
To use the constant, import package:flutter/services.dart.
Then access the top-level constant kIosToLogicalKey in your if check like this:
if (event.logicalKey == LogicalKeyboardKey.backspace ||
// TODO: Check if iOS key code is 42 or kIosToLogicalKey[42] ||
event.physicalKey == PhysicalKeyboardKey.backspace) {
FYI: I know that this is not the complete solution, however, I do not have a mac to test whether it is working anyways, but I hope this gives you the right direction to look at and go from there.
Upvotes: 1