如何检测抖动中的硬件按钮按下



我正在构建一个应用程序,该应用程序通过长时间按下硬件按钮(Android/ios音量增大按钮(启动,但我找不到任何方法,有什么办法吗?

更新的答案:

适用于iOS/Android:

看看这个包裹:

  • Volume Watcher软件包

对于桌面/Web:

您可以使用Focus小部件或RawKeyboardListener小部件来监听键盘事件。

这里有一个简单的例子:

@override
Widget build(BuildContext context) {
return Focus(
autofocus: true,
onKeyEvent: (node, event) {
if (event.physicalKey == PhysicalKeyboardKey.keyA) {
if (event is KeyDownEvent) {
// the user started pressing the key A
} else if (event is KeyRepeatEvent) {
// the user is pressing the key A
} else if (event is KeyUpEvent) {
// the user stopped pressing the key A
}
// if you handled the event (prevent propagating the events further)
return KeyEventResult.handled;
}
// otherwise return this (propagates the events further to be handled elsewhere)
return KeyEventResult.ignored;
},
child: Container(),
);
}

我使用的是带触摸条的macbook,所以我无法确认音量增大,但你可以用PhysicalKeyboardKey.audioVolumeUp替换PhysicalKeyboardKey.keyA

此外,您可以使用FocusNode并将其传递给Focus小部件,以控制小部件何时具有焦点(即,它应该在何时侦听事件(,而不是使用autofocus

参考文献:

  • 所有物理键的列表(只需转到IDE上PhysicalKeyboardKey的定义即可查看列表(:keyboard_key.dart

  • 带有DartPad示例的渲染文档页面:PhysicalKeyboardKey类

最新更新