我正在构建一个应用程序,该应用程序通过长时间按下硬件按钮(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类