Flutter RawKeyboardListener triggering system sounds on MacOS

Issue

I have a MacOS desktop application built with flutter.
In it I have a RawKeyboardListener widget. it is functioning as expected. It is capturing keyboard input which I could process as normal.

However whenever I press a key the OS level key input rejection sound plays.
That sound that comes up when you press a key in a place where the key specifically won’t work.

I haven’t encountered this otherwise when using the listener so I’m not even sure where to begin.

Solution

If you want to avoid beeping for keys you should use the FocusNode‘s onKey, to actually handle the event (i.e., return true), rather than just listening for the existence of the event (like in RawKeyboardListener)

Handling the key with the FocusNode.onKey is easiest when done with a Focus widget:

Widget build(BuildContext context) {
  return Focus(
    onKey: (FocusNode node, RawKeyEvent event) => true,
    child: ...
  );
}

That will manage the focus node for you (inserting and removing it as necessary).

Answered By – anirudh

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

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