Skip to content

Ignore key events on edit control on web platform (#52656)#52661

Merged
fluttergithubbot merged 1 commit intoflutter:masterfrom
cjng96:ignore-key
Apr 1, 2020
Merged

Ignore key events on edit control on web platform (#52656)#52661
fluttergithubbot merged 1 commit intoflutter:masterfrom
cjng96:ignore-key

Conversation

@cjng96
Copy link
Copy Markdown
Contributor

@cjng96 cjng96 commented Mar 16, 2020

Description

Ignore key event from web platform to prevent twice key action by one key press.
For now It's done on Flutter Engine's keyboard.dart code. but I move that code here since we can use a key event via RawKeyListener on web platform(It's working well on Android)

After this pull-request is merged, I will issue new pull-request on Flutter Engine that allowing to send keyEvent which occurs on input element with web.

Related Issues

#52656

#44133

Tests

I added the following tests:

editable_test.dart / ignore key event from web platform

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I updated/added relevant documentation (doc comments with ///).
  • All existing and new tests are passing.
  • The analyzer (flutter analyze --flutter-repo) does not report any problems on my PR.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Did any tests fail when you ran them? Please read Handling breaking changes.

  • No, no existing tests failed, so this is not a breaking change.
  • Yes, this is a breaking change. If not, delete the remainder of this section.

@fluttergithubbot fluttergithubbot added the framework flutter/packages/flutter repository. See also f: labels. label Mar 16, 2020
@googlebot
Copy link
Copy Markdown

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@googlebot
Copy link
Copy Markdown

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of exposing this private method, I would initialize the flutter binding and uses

ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
        SystemChannels.keyEvent.name,
        SystemChannels.keyEvent.codec.encodeMessage(data),
            (ByteData data) {},
 );

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good!. Thank you. :)

Copy link
Copy Markdown
Contributor Author

@cjng96 cjng96 Mar 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you suggested, I tried to use the ServicesBinding class.

It seems to be working if I use testWidgets and EditableText for pumpWidget. ( You can see it on updated pull-request's source code)
I failed to find a other solution to use both RenderEditable and ServicesBinding together without pumpWidget.

This test code is working well. but I am not sure if this is correct implementation under flutter testing rule. :(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I forgot to mention you will need TestWidgetsFlutterBinding.ensureInitialized();
See
https://github.com/flutter/flutter/blob/master/packages/flutter/test/services/platform_channel_test.dart

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is unclear, is the key down only apply to arrow key?

Copy link
Copy Markdown
Contributor Author

@cjng96 cjng96 Mar 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code and one more pull-request(I does't issue yet) on engine repo replace flutter engine's pull-request #13699. so ignore all keyEvent from input control on web.

Arrow key(_handleMovement function) and delete key(_handleDelete function) should be ignored.

But I didn't check the keys(ctrl+c, ctrl+v, ctrl+x and ctrl+a) which be handled in _handleShortcuts yet. I am going to check those keys should be ignored or not. then renew pull-request. Thank you.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ctrl+c, ctrl+v keys are working well. so I modify the code to ignore movement keys and delete key only.
Thank you.

Copy link
Copy Markdown
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added some comments, also cc @mdebbar

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We -> we

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am surprised that uses TestWidgetsFlutterBinding.ensureInitialized(); still requires testWidget. what was the issue you were facing? If this is necessary, this test should be put in editable_text_test.dart

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without testWidgets and pumpWidget, I could't use SystemBinding. :(
I will paste error message and sample code I tried because this can be user error(I'm newbie on Flutter).

I don't like this code which uses EditableText here. It's good if I can implement without testWidgets and pumpWidget function. and in other case, I will move this test code to editable_text_test.dart as you said.

Thank you.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am surprised that uses TestWidgetsFlutterBinding.ensureInitialized(); still requires testWidget. what was the issue you were facing? If this is necessary, this test should be put in editable_text_test.dart

Sorry today I succeed to make test code without testWidgets and pumpWidget functions as you said. maybe I was a little bit confused about test framework. :(
I updated my pull-request. Thank you for your comments. :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just return code here and return null at line 115?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is copied from _getKeyCode() function, this style is there. so I follow same style.

Anyway I think the style you suggest is better too. :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets follow the same style here final int scanCode = platform == 'web' ? -1 : _getScanCode(physicalKey, platform);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good. OK. :)

@Piinks Piinks added a: text input Entering text in a text field or keyboard related problems platform-web Web applications specifically labels Mar 18, 2020
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason we decided to let the browser handle all key events for us is because it's difficult to hardcode the handling of all keys.

For example, movement keys and delete are not the only ones we should ignore here. What about backspace, home, end, page-up, page-down, enter, etc?

Could you explain your use-case a bit more? Like, why do you need to listen to keys when the user is editing a text field?

Copy link
Copy Markdown
Contributor Author

@cjng96 cjng96 Mar 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello thank you for your comment, :)

First, regarding the keys we should ignore on control,
We can ignore all keyEvent here instead of Engine level. If so, it has same result but we can use RawKeyListener with EditableText. (It's not available now due to engine level keyEvent preventation.)

About my use-case,
I need to implement short-cut key so I need RawKeyLisener for it.

For example,
When the user press ctrl+r key(any key promised), I want
(1) Popup dialog for recommanded text list,
(2) Reset to default text status,
(3) Change input dialog layout(normal user or expert user)

Future more, I want to implement powerful rich editor based on flutter platform. as far as I know, text_editing.dart service should be used for it. but I can't receive keyEvent from it. so it's impossible on web. :(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. Yeah, those use-cases make sense.

@cjng96 cjng96 force-pushed the ignore-key branch 3 times, most recently from 5dea0b3 to df97ef9 Compare March 19, 2020 14:33
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can just be

await simulateKeyDownEvent(LogicalKeyboardKey.arrowRight, platform: 'web');

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh... I didn't know there is that global function. Thank you for shorten code.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@cjng96 cjng96 force-pushed the ignore-key branch 2 times, most recently from 9b93e7b to 3e51403 Compare March 20, 2020 14:11
Copy link
Copy Markdown
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have anything else, it lgtm if it also lgt @mdebbar

Copy link
Copy Markdown
Contributor

@mdebbar mdebbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also share the engine PR so we can review both at the same time?

Comment on lines 481 to 485
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to check the type of the event. You should use the available constant kIsWeb.

And I would put this check in the beginning of the method to avoid doing unnecessary work like collapsing synonyms, etc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I modify the source to use kIsWeb. so I also modify test function. I activate my test function on web platform but omit other test functions in editable_test.dart on web. :(
I'm not sure if it's rightapproach. please confirm new pull-request.

@mdebbar
Copy link
Copy Markdown
Contributor

mdebbar commented Mar 20, 2020

Overall, the PR looks good to me at this point (except the isWeb check that I commented about). But I'll wait for the engine PR so we can review both and make sure they will work well together.

@cjng96 cjng96 force-pushed the ignore-key branch 2 times, most recently from 7b9acf5 to 3aed75d Compare March 21, 2020 01:38
@goderbauer
Copy link
Copy Markdown
Member

goderbauer commented Mar 25, 2020

(PR triage): This PR is blocked on flutter/engine#17242.

Copy link
Copy Markdown
Contributor

@mdebbar mdebbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cjng96
Copy link
Copy Markdown
Contributor Author

cjng96 commented Mar 31, 2020

I wonder when this pull-request is merged.

@mdebbar
Copy link
Copy Markdown
Contributor

mdebbar commented Mar 31, 2020

@cjng96 I can merge the PRs. I think the framework PR should be merged first, correct?

@cjng96
Copy link
Copy Markdown
Contributor Author

cjng96 commented Apr 1, 2020

@cjng96 I can merge the PRs. I think the framework PR should be merged first, correct?

Yes, framework PR should be merged first. Thank you.

@fluttergithubbot fluttergithubbot merged commit d62c7ec into flutter:master Apr 1, 2020
@cjng96 cjng96 deleted the ignore-key branch April 1, 2020 11:04
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Aug 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. platform-web Web applications specifically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants