首页 > 解决方案 > Flutter RawKeyboardListener triggering system sounds on MacOS

问题描述

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.

标签: macosflutterdartflutter-desktop

解决方案


如果您想避免按键发出哔哔声,您应该使用FocusNode's onKey, 来实际处理事件(即返回 true),而不仅仅是监听事件的存在(如 in RawKeyboardListener

FocusNode.onKey使用小部件完成时,使用 最容易处理密钥Focus

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

这将为您管理焦点节点(根据需要插入和删除它)。


推荐阅读