Skip to content

The escape key should close the right click menu on desktop #98163

Description

@justinmc

When the right click menu is open, pressing the escape key should close it. I confirmed that this is the native behavior on Mac and Windows. I assume Linux is the same but I haven't tried.

Steps to reproduce

  1. Run any Flutter app with a text field on a desktop platform (example given below).
  2. Right click on the text field. The context menu shows.
  3. Press the escape key.

Expected: The context menu closes.
Actual: Nothing happens and the context menu remains on the screen.

Solution

We could add a text editing shortcut for this and it should work for now, but if we allow context menus outside of EditableText (as mentioned in #90563), then the Shortcuts widget won't receive the escape when nothing is focused and it won't always work.

@gspencergoog Is it possible to receive a Shortcut when nothing is focused?

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

void main() => runApp(const MyApp());

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter Demo'),
        ),
        body: const Center(
          child: TextField(),
        ),
      ),
    );
  }
}

Activity

  1. added
    a: text inputEntering text in a text field or keyboard related problems
    on Feb 10, 2022
  2. Seirdy commented on Feb 11, 2022

    @Seirdy

    This could be considered a component of #94965, since it doesn't look like this is the only type of "de-selection" that the ESC key should handle in Flutter's built-in widgets.

  3. justinmc commented on Feb 11, 2022

    @justinmc
    ContributorAuthor

    Good point. I left a comment on that issue. If that issue doesn't have the same focus problem that this one does, then it will be even easier to fix.

  4. markusaksli-nc commented on Feb 16, 2022

    @markusaksli-nc
    Contributor

    Reverted in #98600

  5. gspencergoog commented on Feb 17, 2022

    @gspencergoog
    Contributor

    @justinmc There's no way to get a shortcut when nothing is focused, but you can listen for key events globally through a handler onHardwareKeyboard, and if escape is pressed while the context menu is open, close the menu.

  6. added
    P2Important issues not at the top of the work list
    on Feb 17, 2022
  7. github-actions commented on Mar 16, 2022

    @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.

  8. locked as resolved and limited conversation to collaborators on Mar 16, 2022
  9. moved this to Done (PR merged) in Nevercodeon Jul 18, 2022
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

    P2Important issues not at the top of the work lista: desktopRunning on desktopa: text inputEntering text in a text field or keyboard related problems

    Type

    No type

    Projects

    • Status
      Done (PR merged)

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions