Skip to content

[webview_flutter] Physical keyboard: arrow keys not working #102505

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

Open
junglerobba opened this issue Apr 25, 2022 · 28 comments
Open

[webview_flutter] Physical keyboard: arrow keys not working #102505

junglerobba opened this issue Apr 25, 2022 · 28 comments
Labels
found in release: 2.10 Found to occur in 2.10 found in release: 2.13 Found to occur in 2.13 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: webview The WebView plugin p: webview-keyboard Keyboard issues with flutter_webview plugin P2 Important issues not at the top of the work list package flutter/packages repository. See also p: labels. platform-android Android applications specifically platform-ios iOS applications specifically team-ecosystem Owned by Ecosystem team triaged-ecosystem Triaged by Ecosystem team

Comments

@junglerobba
Copy link

junglerobba commented Apr 25, 2022

Steps to Reproduce

  1. Execute flutter run on the code sample (iOS Simulator or iPad with bluetooth keyboard)
  2. Enter some text into the search field and press left arrow key

Expected results:
Cursor should move left

Actual results:
Cursor does not move

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: WebView(
        initialUrl: 'https://duckduckgo.com/',
      ),
    );
  }
}
Logs
[✓] Flutter (Channel stable, 2.10.5, on macOS 12.3.1 21E258 darwin-x64, locale en-DE)
    • Flutter version 2.10.5 at /Users/***/dev/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5464c5bac7 (7 days ago), 2022-04-18 09:55:37 -0700
    • Engine revision 57d3bac3dd
    • Dart version 2.16.2
    • DevTools version 2.9.2

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/devopsds/Library/Android/sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
    • Android Studio at /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 11.0.10+0-b96-7281165)

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

[✓] Connected device (2 available)
    • iPad (9th generation) (mobile) • 35ED2CBC-915D-4737-93BD-13CC3F249CFD • ios            • com.apple.CoreSimulator.SimRuntime.iOS-15-2 (simulator)
    • Chrome (web)                   • chrome                               • web-javascript • Google Chrome 100.0.4896.127

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
@maheshj01 maheshj01 added the in triage Presently being triaged by the triage team label Apr 26, 2022
@maheshj01
Copy link
Member

Hi @junglerobba, Thanks for filing the issue. I am able to reproduce the issue on stable and the master channel. Pressing arrow keys using the physical keyboard does not move the cursor. The issue is reproducible on both IOS and Android. Reproduces even with Hybrid composition on Android.

code sample
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

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

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    // Enable virtual display.
    if (Platform.isAndroid) WebView.platform = AndroidWebView();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: WebView(
        initialUrl: 'https://google.com/',
      ),
    );
  }
}
flutter doctor -v (mac)
[✓] Flutter (Channel stable, 2.10.5, on macOS 12.3 21E230 darwin-arm, locale en-IN)
    • Flutter version 2.10.5 at /Users/mahesh/Documents/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5464c5bac7 (12 hours ago), 2022-04-18 09:55:37 -0700
    • Engine revision 57d3bac3dd
    • Dart version 2.16.2
    • DevTools version 2.9.2

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/mahesh/Library/Android/sdk
    • Platform android-32, build-tools 31.0.0
    • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.10.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.1)
    • Android Studio at /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 11.0.11+0-b60-7772763)

[✓] IntelliJ IDEA Community Edition (version 2021.2.1)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 61.2.4
    • Dart plugin version 212.5080.8

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

[✓] Connected device (4 available)
    • Redmi K20 Pro (mobile) • 192.168.1.2:55555                    • android-arm64  • Android 11 (API 30)
    • iPhone 12 Pro (mobile) • 535317A4-921C-488F-B234-C2CC6D4AE5A1 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
    • macOS (desktop)        • macos                                • darwin-arm64   • macOS 12.3 21E230 darwin-arm
    • Chrome (web)           • chrome                               • web-javascript • Google Chrome 100.0.4896.88

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
[✓] Flutter (Channel master, 2.13.0-0.0.pre.712, on macOS 12.3 21E230 darwin-arm, locale en-IN)
    • Flutter version 2.13.0-0.0.pre.712 at /Users/mahesh/Documents/flutter_master
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 8c22f5648a (77 minutes ago), 2022-04-25 05:34:07 -0400
    • Engine revision 10efa195bf
    • Dart version 2.18.0 (build 2.18.0-44.0.dev)
    • DevTools version 2.12.2

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/mahesh/Library/Android/sdk
    • Platform android-32, build-tools 31.0.0
    • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)
    • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS (Xcode 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    ! CocoaPods 1.10.2 out of date (1.11.0 is recommended).
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that
        responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To upgrade see https://guides.cocoapods.org/using/getting-started.html#installation for
      instructions.

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.1)
    • Android Studio at /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 11.0.11+0-b60-7772763)

[✓] IntelliJ IDEA Community Edition (version 2021.2.1)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 61.2.4
    • Dart plugin version 212.5080.8

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

[✓] Connected device (3 available)
    • Redmi K20 Pro (mobile) • 192.168.1.4:5533 • android-arm64  • Android 11 (API 30)
    • macOS (desktop)        • macos            • darwin-arm64   • macOS 12.3 21E230 darwin-arm
    • Chrome (web)           • chrome           • web-javascript • Google Chrome 100.0.4896.127

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.

@maheshj01 maheshj01 added plugin p: webview The WebView plugin p: webview-keyboard Keyboard issues with flutter_webview plugin has reproducible steps The issue has been confirmed reproducible and is ready to work on found in release: 2.10 Found to occur in 2.10 found in release: 2.13 Found to occur in 2.13 and removed in triage Presently being triaged by the triage team labels Apr 26, 2022
@maheshj01 maheshj01 changed the title [webview_flutter] iOS Hardware keyboard: arrow keys not working [webview_flutter] Physical keyboard: arrow keys not working Apr 26, 2022
@stuartmorgan-g stuartmorgan-g added the P2 Important issues not at the top of the work list label Apr 28, 2022
@blasten blasten added the platform-ios iOS applications specifically label May 17, 2022
@blasten
Copy link

blasten commented May 17, 2022

cc @cyanglaz

@cyanglaz cyanglaz added the platform-android Android applications specifically label May 17, 2022
@cyanglaz
Copy link
Contributor

cyanglaz commented May 18, 2022

On iOS:
Looks like there are some custom handling for the keyboard keys for iOS> 13.4 *, which never takes the super path, it leads to arrow keys being ignored on PlatformView. The arrow keys were handled in the flutter app for selecting elements. See below video where I'm hitting arrow keys and a highlight is moving between the Flutter Widgets.
See: https://github.com/flutter/engine/blame/main/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm#L1318-L1326

Screen.Recording.2022-05-18.at.10.35.32.AM.mov

cc @gspencergoog

@cyanglaz
Copy link
Contributor

I create a quick prototype to demo a potential fix ( flutter/engine#33458). I'm not familiar with how the keyboard keys are handled in other cases so this fix might missing corner cases. There should be a more elegant solution.

cc @hellohuanlin This could be related to the focus tree work you are current working on.

@gspencergoog
Copy link
Contributor

cc @dkwingsmt

@dkwingsmt
Copy link
Contributor

@cyanglaz Thanks for the analysis! Although, the current code does take the super path in the callback, which is invoked if the framework decides not to handle key event. It seems that the framework did not dispatch the event to the WebView first but handled on its own. I'm reading WebView's source and see if I can find a better way.

@dkwingsmt
Copy link
Contributor

dkwingsmt commented May 18, 2022

@gspencergoog Have we ever tested keyboard on platform views? It seems to me that no platform views contain any focus at all. They can't be focused, and there are no ways to delegate key events to their native component.

@gspencergoog
Copy link
Contributor

No, I don't think we've ever done much with platform views and keyboard events.

@cyanglaz
Copy link
Contributor

cyanglaz commented May 18, 2022

@gspencergoog Have we ever tested keyboard on platform views? It seems to me that no platform views contain any focus at all. They can't be focused, and there are no ways to delegate key events to their native component.

Sounds like if PlatformView participants in the focus tree, this should be straightforward to wire up?
For example, if the framework knows the PlatformView is the focus, it can give up all the special handling and call super?

@hellohuanlin Is recently working on adding PlatformViews to the focus three. #34187.

Meanwhile, Android PlatformView already participants in the Focus Tree. So Android PlatformView is unblocked.

@dkwingsmt
Copy link
Contributor

dkwingsmt commented May 18, 2022

Kind of. There are three remaining points as far as I can think of:

  1. Check if it is possible to insert keyboard events to platform views.
  2. Ideally store the original system event for insertion.
  3. Change the framework focus API to allow async keyboard handling.

I saw @hellohuanlin 's work (flutter/engine#33093 if I didn't misunderstand). It seems that they had to add code specially for text input. If that's required we'll also need to figure out a way to incorporate text input and keyboard.

@hellohuanlin
Copy link
Contributor

@dkwingsmt oh that engine PR is not complete change yet. A missing part is to detect platform view focus and send channel message to the framework.

@dkwingsmt
Copy link
Contributor

Yeah I'm aware. I'm just curious what it takes on the engine side to make it work. Let me know your progress!

@hellohuanlin
Copy link
Contributor

hellohuanlin commented May 18, 2022

My understanding is that, in my change that allows platform view to participate in the focus tree, when the platform view gets focused, then framework will unfocus the previously focused widget (seems to be the navigation bar in this case).

So my guess is that after my changes land, this problem will just get resolved automatically (though i'm not 100% sure since I'm still new to this code).

@hellohuanlin
Copy link
Contributor

@tilemoon can you update to the latest version and see if it's fixed?

@hellohuanlin
Copy link
Contributor

hellohuanlin commented Aug 2, 2022

@tilemoon Gotcha. Looks like this issue turned out not related to my other fix related to platform view focus then. cc: @dkwingsmt

@kerberjg
Copy link
Contributor

I managed to get it working! #74044 (comment)

@nawbc
Copy link

nawbc commented Dec 1, 2022

Any update for Android?

@razfazz
Copy link

razfazz commented Apr 26, 2023

any updates?

@hellohuanlin
Copy link
Contributor

I managed to get it working! #74044 (comment)

From your workaround, it's simply overriding FlutterViewController's pressesBegan/Changed/Ended calls and removes the FlutterUIPressProxy part.


- (void)pressesBegan:(NSSet<UIPress*>*)presses
           withEvent:(UIPressesEvent*)event API_AVAILABLE(ios(9.0)) {
  if (@available(iOS 13.4, *)) {
    for (UIPress* press in presses) {
      [self handlePressEvent:[[[FlutterUIPressProxy alloc] initWithPress:press
                                                               withEvent:event] autorelease]
                  nextAction:^() {
                    [super pressesBegan:[NSSet setWithObject:press] withEvent:event];
                  }];
    }
  } else {
    [super pressesBegan:presses withEvent:event];
  }
}

I'm not familiar with this part, but from git history, @gspencergoog probably has some context.

@flutter-triage-bot flutter-triage-bot bot added the package flutter/packages repository. See also p: labels. label Jul 5, 2023
@Hixie Hixie removed the plugin label Jul 6, 2023
@flutter-triage-bot flutter-triage-bot bot added multiteam-retriage-candidate team-ecosystem Owned by Ecosystem team triaged-ecosystem Triaged by Ecosystem team labels Jul 8, 2023
@Frank3K
Copy link

Frank3K commented Jul 27, 2023

We managed to come up with a work-around that works on both Android (including Chromebook) and iOS. The solution does not require editing platform-specific code (like was done in #74044 (comment)).

Issue and solution

Apparently the arrow keys from the hardware keyboard get "eaten" by Flutter and never reach the included webview. By listening to the arrow keys and instructing Flutter to return them to the platform the arrow keys function again. The work-around does this by returning KeyEventResult.skipRemainingHandlers for the arrow keys. From the KeyEventResult.skipRemainingHandlers docs:

It will be returned to the platform embedding to be propagated to text fields and non-Flutter key event handlers on the platform.

Work-around code

We've created a new repository containing an updated code sample (the code in the first comment used on old version of webview_flutter) to demonstrate the original issue. After that, we have updated the repository with our work-around. The relevant file containing the work-around is lib/main.dart.

Remarks

Please note that on Android and Chromebook it is possible in input fields to move the cursor beyond the last character. When moving one character back (using the left arrow key), the entire text becomes selected. This behaviour is different to what happens when you choose Web as a build target and open the app in (for example) Chrome.

@Vanilla1999
Copy link

This solution (#102505 (comment)) will only work after double clicking on any of the arrows. Is there an alternative solution ?

@Frank3K
Copy link

Frank3K commented Sep 15, 2023

This solution (#102505 (comment)) will only work after double clicking on any of the arrows.

In our app no double clicking is required; it works as expected.

@Vanilla1999
Copy link

I tested it on an emulator and a physical device, the behavior is the same. At the first start of the program, after clicking on the down arrow, only 1 ArrowDown RawKeyUpEvent event is generated, without RawKeyDownEvent. After that, everything is fine.
image

@Frank3K
Copy link

Frank3K commented Oct 20, 2023

I tested it on an emulator and a physical device, the behavior is the same. At the first start of the program, after clicking on the down arrow, only 1 ArrowDown RawKeyUpEvent event is generated, without RawKeyDownEvent. After that, everything is fine.

I've just retested this and you're right. The very first event is missed unfortunately. We currently don't know a way how to work-around this.

@liranzairi
Copy link
Contributor

liranzairi commented Nov 24, 2023

Any updates on this please? Maybe it's worth raising the priority because allowing the use of physical keyboards is part of accessibility requirements.

@Lukiya
Copy link

Lukiya commented Mar 20, 2024

Same issue.

@rekire
Copy link
Contributor

rekire commented May 8, 2024

Might be #147844 is also affected by this issue. My issue is that when I press enter or space and another "visual focused" widgets consume the space and enter pressed on a physical (bluetooth) keyboard. I'll check if the workaround mentioned here could fix my issue too.

@ltOgt
Copy link

ltOgt commented May 21, 2025

The mentioned workaround also helped with web platform view HTMLInputElement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
found in release: 2.10 Found to occur in 2.10 found in release: 2.13 Found to occur in 2.13 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: webview The WebView plugin p: webview-keyboard Keyboard issues with flutter_webview plugin P2 Important issues not at the top of the work list package flutter/packages repository. See also p: labels. platform-android Android applications specifically platform-ios iOS applications specifically team-ecosystem Owned by Ecosystem team triaged-ecosystem Triaged by Ecosystem team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy