Keyboard accelerators

In addition to tab traversal, desktop and web users are accustomed to having various keyboard shortcuts bound to specific actions. Whether it’s the Delete key for quick deletions or Control+N for a new document, be sure to consider the different accelerators your users expect. The keyboard is a powerful input tool, so try to squeeze as much efficiency from it as you can. Your users will appreciate it!

Keyboard accelerators can be accomplished in a few ways in Flutter, depending on your goals.

If you have a single widget like a TextField or a Button that already has a focus node, you can wrap it in a KeyboardListener or a Focus widget and listen for keyboard events:

 @override

 Widget build(BuildContext context) {

  return Focus(

   onKeyEvent: (node, event) {

    if (event is KeyDownEvent) {

     print(event.logicalKey);

    }

    return KeyEventResult.ignored;

   },

   child: ConstrainedBox(

    constraints: const BoxConstraints(maxWidth: 400),

    child: const TextField(

     decoration: InputDecoration(

      border: OutlineInputBorder(),

     ),

    ),

   ),

  );

 }

}

Leave a comment

Your email address will not be published. Required fields are marked *