Flutter Global Keyboard Shortcuts
Asked Answered
D

0

8

I am writing an application that should support searching some content. For this I implemented a search TextField. Now I would like to focus said TextField when the key combination ctrl + F is pressed.

In order to implement this, I wrapped my view in both a Shortcuts and Actions component as follows:

return Scaffold(
  body: Shortcuts(
    shortcuts: {
      LogicalKeySet(LogicalKeyboardKey.keyF, LogicalKeyboardKey.control): MTCSearchIntent(),
    },
    child: Actions(
      actions: {
        MTCSearchIntent: MTCFocusAction(null),
      },
      child: ...
    ),
  ),
);

This implementation works if the search bar is already focused, but that kind of defeats the purpose of focusing it.. So I was wondering, how can I apply the Shortcut listener to the entire screen? Or asked differently, what determines the scope of the interactions, the Shortcut component, the Action component, something related to the FocusNode?

The example on flutter.dev don't seem to work globally either. In fact they don't seem to work at all for me.

I reproduced the example using a minimal example here: https://dartpad.dev/?id=55902a179be137d9025ea02170d749a7

When the TextField is focused, the ArrowUpand ArrowDown actions work as intended. However, if any other part of the application is highlighted, the Intent is never triggered.

Dank answered 12/9, 2022 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.