-
Notifications
You must be signed in to change notification settings - Fork 26.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
On Android, KeyboardListener catches software keyboard input. #148375
Comments
Hi @darshankawar |
Thanks for the update. Check if this applies to your case as well or not. |
The Android InputConnection API allows a software keyboard to send a keypress as a KeyEvent (https://developer.android.com/reference/android/view/inputmethod/InputConnection#sendKeyEvent(android.view.KeyEvent)). Usually keyboards do that for backspace because sometimes if a text field is already empty you want to move the focus to the previous field. Flutter currently does not distinguish KeyEvents emitted by this method call and KeyEvents generated by physical keyboards. |
@darshankawar I'm not sure if I understand the problem correctly. I think the issue is that the hardware keyboard operates differently depending on the mobile device (s9 / pixel) when using RawKeyboardListener. |
@LongCatIsLooong thank you for the reply. |
Yes. Software keyboards (Gboard / Samsung Keyboard) can choose to send certain key presses as hardware key events (for reasons stated above). They can even send "A" as a key event if they so choose.
I believe this is possible on Android because they have separate call paths: https://github.com/flutter/engine/blob/c6fecf65fbf385c5e9168c5b40526c9ae998045b/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java#L285-L287 But this may not be possible on other platforms. And editors typically don't need such information.
This sounds like a relatively niche use case, could you elaborate? |
@LongCatIsLooong Sorry for the late reply. 😓
However, flutter cannot check whether the hardware keyboard is connected and |
Keeping the issue open and labeling for team's tracking. |
Is https://main-api.flutter.dev/flutter/widgets/EditableText/onEditingComplete.html what you are looking for? |
@LongCatIsLooong |
Ah indeed it does, but it's disabled for multiline: https://github.com/flutter/engine/blob/557a4c622617c6c0f66b604669d14d11b8aa1cbe/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java#L304 This is a bit strange. I don't think a hardware key-presses should trigger |
Tentatively marked this issue as P3. However, if you would like to see this issue fixed sooner, you are welcome to open a pull request to remove the logic that is causing this issue. |
Steps to reproduce
Our app has a function that operates by distinguishing between hardware (bluetooth) keyboard input and software keyboard input for
TextField
.While searching for the API provided by Flutter to implement this, I came across the
KeyboardListener
widget description below.From the above explanation, I understood that KeyboardListener only recognizes hardware keys.
We applied this to our project because we thought it suited our requirements, but the actual behavior was different from what we expected.
In fact, when we run the app with the code below using KeyboardListenerd, the backspace (if there is no string to delete) and enter (textfield property - textInputAction: newline) keys entered on the software keyboard are detected as if backspace or enter were pressed on the hardware keyboard.
Additionally, we were unable to determine whether the input was software or hardware from the detected results.
Does this mean I misunderstood the document? or is this not the widget provided with this intention?
I'm not sure if this is a bug, but if this is the intended result, it would be helpful if the documentation for KeyboardListener or KeyEvent was a bit nicer.
Also, if there is a way to implement the above requirements, please kindly let me know.
(I tested it on Samsung Keyboard and GBoard, and the results were different. I used a Korean/English keyboard, and it was also different when I typed in Korean and English. - test os: android - )
Expected results
Key input from the software keyboard is not detected in the Keyboardlistener onKeyEvent.
Actual results
Depending on the keyboard type / input language / key type, onKeyEvent is called for some software keyboard inputs.
Code sample
Screenshots or Video
Screenshots / Video demonstration
[Upload media here]
Logs
Logs
[Paste your logs here]
Flutter Doctor output
Doctor output
[Paste your output here]
The text was updated successfully, but these errors were encountered: