Skip to content

[Android] [Google Chromecast] Unable to use software keyboard with TextField #154924

Closed

Description

@romaingyh

Steps to reproduce

  1. Copy paste the code sample
  2. Run on chromecast
  3. Use remote bottom arrow to focus textfield and make keyboard appear
  4. Try to enter some text

Expected results

We should be able to use software keyboard and enter text like any other Android TV device.

Actual results

The keyboard appears fine, but nothing happens when you use the arrow keys to navigate through the letters on the keyboard. It looks like Flutter app is keeping focus.

Code sample

From @huycozy's answer on an abandonned similar issue #125541

Code sample
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  RawKeyboard.instance.addListener((e) {
    debugPrint('${e.character} - ${e.logicalKey} - ${e.physicalKey}');
  });
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Container(
            margin: const EdgeInsets.all(20),
            child: const TextField(
              decoration: InputDecoration.collapsed(hintText: 'Enter'),
            ),
          ),
        ),
      ),
    );
  }
}

Screenshots or Video

From #125541

Screenshots / Video demonstration
IMG_1835.MOV

Logs

Logs
flutter run
Launching lib/main.dart on Chromecast HD in debug mode...
Running Gradle task 'assembleDebug'...                           2 900ms
✓ Built build/app/outputs/flutter-apk/app-debug.apk
Syncing files to device Chromecast HD...                            17ms

Flutter run key commands.
r Hot reload. 🔥🔥🔥
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

A Dart VM Service on Chromecast HD is available at: http://127.0.0.1:49716/M6LeYPH5OQM=/
D/ProfileInstaller(11202): Installing profile for com.example.bug_report
The Flutter DevTools debugger and profiler on Chromecast HD is available at: http://127.0.0.1:9100?uri=http://127.0.0.1:49716/M6LeYPH5OQM=/
I/flutter (11202): 
I/flutter (11202): null - LogicalKeyboardKey#4999d(keyId: "0x10000050c", keyLabel: "Select", debugName: "Select") - PhysicalKeyboardKey#c10a6(usbHidUsage: "0x00070077", debugName: "Select")
I/flutter (11202): 
D/InputMethodManager(11202): showSoftInput() view=io.flutter.embedding.android.FlutterView{d77d02b VFE...... .F...... 0,0-1920,1080 #1 aid=1073741824} flags=0 reason=SHOW_SOFT_INPUT
D/InputMethodManager(11202): showSoftInput() view=io.flutter.embedding.android.FlutterView{d77d02b VFE...... .F...... 0,0-1920,1080 #1 aid=1073741824} flags=0 reason=SHOW_SOFT_INPUT
I/flutter (11202): null - LogicalKeyboardKey#80d00(keyId: "0x100000301", keyLabel: "Arrow Down", debugName: "Arrow Down") - PhysicalKeyboardKey#556a0(usbHidUsage: "0x00070051", debugName: "Arrow Down")
W/InputMethodManager(11202): Ignoring onBind: cur seq=-1, given seq=116
D/InputConnectionAdaptor(11202): The input method toggled cursor monitoring on
D/InsetsController(11202): show(ime(), fromIme=true)
I/flutter (11202): 
I/flutter (11202): null - LogicalKeyboardKey#857a2(keyId: "0x100000303", keyLabel: "Arrow Right", debugName: "Arrow Right") - PhysicalKeyboardKey#4fbfe(usbHidUsage: "0x0007004f", debugName: "Arrow Right")
I/flutter (11202): 
I/flutter (11202): null - LogicalKeyboardKey#857a2(keyId: "0x100000303", keyLabel: "Arrow Right", debugName: "Arrow Right") - PhysicalKeyboardKey#4fbfe(usbHidUsage: "0x0007004f", debugName: "Arrow Right")
I/flutter (11202): 
I/flutter (11202): null - LogicalKeyboardKey#857a2(keyId: "0x100000303", keyLabel: "Arrow Right", debugName: "Arrow Right") - PhysicalKeyboardKey#4fbfe(usbHidUsage: "0x0007004f", debugName: "Arrow Right")
I/flutter (11202): 
I/flutter (11202): null - LogicalKeyboardKey#4999d(keyId: "0x10000050c", keyLabel: "Select", debugName: "Select") - PhysicalKeyboardKey#c10a6(usbHidUsage: "0x00070077", debugName: "Select")
I/flutter (11202): 
I/flutter (11202): null - LogicalKeyboardKey#4999d(keyId: "0x10000050c", keyLabel: "Select", debugName: "Select") - PhysicalKeyboardKey#c10a6(usbHidUsage: "0x00070077", debugName: "Select")
I/flutter (11202): 
I/flutter (11202): null - LogicalKeyboardKey#4999d(keyId: "0x10000050c", keyLabel: "Select", debugName: "Select") - PhysicalKeyboardKey#c10a6(usbHidUsage: "0x00070077", debugName: "Select")

Flutter Doctor output

Doctor output
flutter doctor -v
[✓] Flutter (Channel stable, 3.24.2, on macOS 14.6.1 23G93 darwin-arm64, locale fr-FR)
    • Flutter version 3.24.2 on channel stable at X
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 4cf269e36d (7 days ago), 2024-09-03 14:30:00 -0700
    • Engine revision a6bd3f1de1
    • Dart version 3.5.2
    • DevTools version 2.37.2

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/X/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/X/Library/Android/sdk
    • Java binary at: /Users/X/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15F31d
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • CHROME_EXECUTABLE = /Applications/Chromium.app/Contents/MacOS/Chromium

[✓] Android Studio (version 2023.3)
    • Android Studio at /Users/X/Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)

[✓] IntelliJ IDEA Ultimate Edition (version 2024.1.1)
    • IntelliJ at /Users/X/Applications/IntelliJ IDEA Ultimate.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.93.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.96.0

[✓] Connected device (6 available)
    • Chromecast HD (mobile)          • 192.168.1.33:5555         • android-arm    • Android 12 (API 31)

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Activity

  1. added
    in triage
    on Sep 11, 2024
  2. darshankawar commented on Sep 11, 2024

    @darshankawar
    Member

    @romaingyh
    Can you check if your issue is same as #147772 or not ?

  3. added
    waiting for response
    on Sep 11, 2024
  4. romaingyh commented on Sep 11, 2024

    @romaingyh
    ContributorAuthor

    @darshankawar I don't think so, I faced the #147772 previously but you can play with focus and keyEvent around textfield as a workaround.

    Another point regarding my issue : When the soft keyboard is shown, when you press return it closes the app. It really looks like the app acts as the keyboard is not visible.

  5. removed
    waiting for response
    on Sep 11, 2024
  6. darshankawar commented on Sep 12, 2024

    @darshankawar
    Member

    Thanks for the update. We currently don't have chromecast device to verify / replicate this, but based on the report and behavior occuring, keeping the issue open for team's tracking.

  7. added
    a: text input
    e: device-specific
    platform-android
    fyi-android
    team-text-input
    and removed
    in triage
    on Sep 12, 2024
  8. added
    triaged-text-input
    P2
    on Sep 12, 2024
  9. LongCatIsLooong commented on Sep 12, 2024

    @LongCatIsLooong
    Contributor

    The DefaultTextEditingShortcuts widget could be intercepting the arrow key key events so the on screen keyboard couldn't receive them.

    @romaingyh Could you try overriding the default shortcuts and remap them to DoNothingAndStopPropagationIntent and see if that fixes the issue for you?

  10. 2 remaining items

  11. removed
    waiting for response
    on Sep 13, 2024
  12. romaingyh commented on Sep 13, 2024

    @romaingyh
    ContributorAuthor

    After experiments it seems shortcuts are not the problem. Here is the updated code :

    Code sample
    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    
    void main() {
      WidgetsFlutterBinding.ensureInitialized();
      runApp(const MyApp());
    }
    
    class MyApp extends StatefulWidget {
      const MyApp({super.key});
    
      @override
      State<MyApp> createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: MediaQuery(
            data: MediaQuery.of(context).copyWith(navigationMode: NavigationMode.directional),
            child: Scaffold(
              body: Center(
                child: Container(
                  margin: const EdgeInsets.all(32),
                  child: const Shortcuts(
                    shortcuts: <ShortcutActivator, Intent>{
                      SingleActivator(LogicalKeyboardKey.arrowDown): DoNothingAndStopPropagationTextIntent(),
                      SingleActivator(LogicalKeyboardKey.arrowUp): DoNothingAndStopPropagationTextIntent(),
                      SingleActivator(LogicalKeyboardKey.arrowLeft): DoNothingAndStopPropagationTextIntent(),
                      SingleActivator(LogicalKeyboardKey.arrowRight): DoNothingAndStopPropagationTextIntent(),
                    },
                    child: TextField(
                      decoration: InputDecoration.collapsed(hintText: 'Enter'),
                    ),
                  ),
                ),
              ),
            ),
          ),
        );
      }
    }

    Behaviors are the same with or without the Shortcuts override.


    However I found something interesting. The software keyboard works after hot restart. Here is how to reproduce :

    1. Run the app (I run it on chromecast with adb connect ..., flutter run

    2. Use any arrow to focus the TextField and show the keyboard. Here you're in the bug and nothing happens when you press arrows to navigate in the keyboard.

    3. Hot restart the app and wait for full hot restart complete. Caret in TextField should be blinking again and keyboard still up.

    4. Now you can navigate on keyboard letters and enter some text. If you press return the keyboard closes and you're back to 2.


    I made some debug prints in editable_text.dart in methods like _openInputConnection, _openOrCloseInputConnectionIfNeeded or _handleFocusChanged to compare outputs beween Android TV without the bug and Chromecast with the bug but they look the same :

    Any Android TV (soft. keyboard working)
    I/flutter ( 9908): TargetPlatform.android
    I/flutter ( 9908): android (Platform.operatingSystem)
    I/flutter ( 9908): text_field.dart: _handleFocusChanged : hasFocus: true
    I/flutter ( 9908): text_field.dart: _openOrCloseInputConnectionIfNeeded
    I/flutter ( 9908): text_field.dart: _openInputConnection, _shouldCreateInputConnection: true, _hasInputConnection: false
    I/flutter ( 9908): editable_field.dart: requestKeyboard
    I/flutter ( 9908): editable_field.dart: requestKeyboard - _hasFocus: true
    I/flutter ( 9908): text_field.dart: _openInputConnection, _shouldCreateInputConnection: true, _hasInputConnection: true
    I/ImeTracker( 9908): com.example.bug_report:4c8f187f: onRequestShow at ORIGIN_CLIENT_SHOW_SOFT_INPUT reason SHOW_SOFT_INPUT
    D/InputMethodManager( 9908): showSoftInput() view=io.flutter.embedding.android.FlutterView{99536be VFE...... .F...... 0,0-1920,1080 #1 aid=1073741824} flags=0 reason=SHOW_SOFT_INPUT
    I/ImeTracker( 9908): com.example.bug_report:159313f9: onRequestShow at ORIGIN_CLIENT_SHOW_SOFT_INPUT reason SHOW_SOFT_INPUT
    D/InputMethodManager( 9908): showSoftInput() view=io.flutter.embedding.android.FlutterView{99536be VFE...... .F...... 0,0-1920,1080 #1 aid=1073741824} flags=0 reason=SHOW_SOFT_INPUT
    D/InputConnectionAdaptor( 9908): The input method toggled cursor monitoring on
    D/InsetsController( 9908): show(ime(), fromIme=true)
    I/ImeTracker( 9908): com.example.bug_report:159313f9: onShown
    D/InputConnectionAdaptor( 9908): The input method toggled cursor monitoring off
    W/WindowOnBackDispatcher( 9908): sendCancelIfRunning: isInProgress=falsecallback=ImeCallback=ImeOnBackInvokedCallback@135506776 Callback=android.window.IOnBackInvokedCallback$Stub$Proxy@80f5f96
    I/ImeTracker( 9908): com.example.bug_report:f977167b: onRequestHide at ORIGIN_CLIENT_HIDE_SOFT_INPUT reason HIDE_SOFT_INPUT_BY_INSETS_API
    I/ImeTracker( 9908): com.example.bug_report:661f90fe: onHidden
    Chromecast (soft. keyboard not working)
    I/flutter ( 5978): text_field.dart: _handleFocusChanged : hasFocus: true
    I/flutter ( 5978): text_field.dart: _openOrCloseInputConnectionIfNeeded
    I/flutter ( 5978): text_field.dart: _openInputConnection, _shouldCreateInputConnection: true, _hasInputConnection: false
    D/InputMethodManager( 5978): showSoftInput() view=io.flutter.embedding.android.FlutterView{83ed7f2 VFE...... .F...... 0,0-1920,1080 #1 aid=1073741824} flags=0 reason=SHOW_SOFT_INPUT
    I/flutter ( 5978): editable_field.dart: requestKeyboard
    I/flutter ( 5978): editable_field.dart: requestKeyboard - _hasFocus: true
    I/flutter ( 5978): text_field.dart: _openInputConnection, _shouldCreateInputConnection: true, _hasInputConnection: true
    D/InputMethodManager( 5978): showSoftInput() view=io.flutter.embedding.android.FlutterView{83ed7f2 VFE...... .F...... 0,0-1920,1080 #1 aid=1073741824} flags=0 reason=SHOW_SOFT_INPUT
    W/InputMethodManager( 5978): Ignoring onBind: cur seq=-1, given seq=21
    D/InputConnectionAdaptor( 5978): The input method toggled cursor monitoring on
    D/InsetsController( 5978): show(ime(), fromIme=true)

    If you have any clues or guidance I would be very happy to make a PR :)

  13. LongCatIsLooong commented on Sep 13, 2024

    @LongCatIsLooong
    Contributor

    I looked at the closed issue it seems the theory was that the Flutter activity has the focus over the on-screen keyboard. What is the AppLifecycleState (You can use https://main-api.flutter.dev/flutter/widgets/AppLifecycleListener-class.html to monitor that) when you try to type using the software keyboard?

    Also I suspect this is because the InputConnectionAdaptor class is trying to handle those events instead of returning false and letting the event propagate:
    https://github.com/flutter/engine/blob/715ede5967c8f8be259d5832da7416a316ea927f/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java#L289-L326

    which is called here: https://github.com/flutter/engine/blob/715ede5967c8f8be259d5832da7416a316ea927f/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java#L595

    The android team may have more insights into this.

  14. romaingyh commented on Sep 15, 2024

    @romaingyh
    ContributorAuthor

    AppLifecycleState is resumed both Chromecast device and normal AndroidTV device. I'm currently trying to edit the engine to make handleKeyEvent of InputConnectionAdaptor.java return false and see what happens. First time working on the engine I need to setup few things before.

  15. reidbaker commented on Sep 19, 2024

    @reidbaker
    Member

    From Triage: I am following up with @LongCatIsLooong out of band to see how the android team can help.

  16. LongCatIsLooong commented on Sep 19, 2024

    @LongCatIsLooong
    Contributor

    @romaingyh my suspicion is that handleHorizontalMovement et.al return true in response to arrow key events, so the system thinks the key events were consumed by the text field. If you modify the handleKeyEvent implementation to always return false, would that allow the arrow key events to be forwarded to the software keyboard? Instructions of making such changes can be found here.

  17. romaingyh commented on Sep 19, 2024

    @romaingyh
    ContributorAuthor

    @LongCatIsLooong I made these changes in InputConnectionAdaptor :

    public boolean handleKeyEvent(KeyEvent event) {
        Log.d(TAG, "InputConnectionAdaptor.handleKeyEvent : " + event.toString());
    
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
          if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
            handleHorizontalMovement(true, event.isShiftPressed());
            Log.d(TAG, "Horizontal Movement left handled, returning false");
            return false;
          } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
            handleHorizontalMovement(false, event.isShiftPressed());
            Log.d(TAG, "Horizontal Movement right handled, returning false");
            return false;
          } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
            handleVerticalMovement(true, event.isShiftPressed());
            Log.d(TAG, "Vertical Movement up handled, returning false");
            return false;
          } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
            handleVerticalMovement(false, event.isShiftPressed());
            Log.d(TAG, "Vertical Movement down handled, returning false");
            return false;
            // When the enter key is pressed on a non-multiline field, consider it a
            // submit instead of a newline.
          } else if ...
          
         .....
    }

    But result is the same software keyboard is still not working. However I noticed that all key event if my first log are action.ACTION_UP so we never enter in the first if block.

    I replaced ACTION_DOWN with ACTION_UP to debug, now we enter in if blocks, return false when arrow pressed, but keyboard still locked

  18. LongCatIsLooong commented on Sep 24, 2024

    @LongCatIsLooong
    Contributor

    However I noticed that all key event if my first log are action.ACTION_UP so we never enter in the first if block

    So only key up events can reach this method? Maybe that's because the framework (the text field) is stealing all the down events? Could you try these shortcuts again and see if you get any down events in handleKeyEvent?

  19. reidbaker commented on Sep 24, 2024

    @reidbaker
    Member

    @LongCatIsLooong and I were able to reproduce this issue using a "TV" emulator but in our investigation we found that flutter is behaving the same as youtube and the android setting app. The text input takes the directional input just like a physical keyboard would in that it moves the cursor.

  20. reidbaker commented on Sep 24, 2024

    @reidbaker
    Member

    Our current belief is that android TV's are moving away from dpad navigation of a soft keyboard, at least in the emulated and physical devices we had on hand. The issue we see with a flutter app running the example code also appears in native android apps.

    Finally android tv is not one of our officially supported targets (aka a platform with funding and devices as part of our test lab) so it is unlikely @LongCatIsLooong or I will spend more time on this issue. I personally care about tv support because I shipped flutters apps to android tv in a past job but it only gets passion project time from me.

    Closing as WAI barring other evidence and reproduction steps. We are not opposed to a pull request that fixes this issue if it gets reopened with a more specific error.

  21. added
    r: solved
    on Sep 25, 2024
  22. github-actions commented on Oct 9, 2024

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  23. locked as resolved and limited conversation to collaborators on Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2a: text inpute: device-specificfyi-androidplatform-androidr: solvedteam-text-inputtriaged-text-input

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions