我正在编写一个 Flutter 应用程序,并希望在该应用程序的网络版本中处理键盘快捷键,例如 Ctrl+I。
小免责声明:最近关于快捷方式的 Flutter API 似乎发生了变化。我找到的在线文档已经过时了。我正在使用 Flutter 1.19.0-0.0.pre • channel dev
.
我找到了 Actions API Design感觉就像当前可用的 API 的文档。基于文档中的示例,我实现了这个简短的片段:
class MyIntent extends Intent {}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Shortcuts(
shortcuts: {
LogicalKeySet(LogicalKeyboardKey.control): MyIntent(),
},
child: Actions(
actions: {
MyIntent: CallbackAction(onInvoke: (i) {
print('Hello World!!!');
return null;
}),
},
child: Center(
child: Column(
children: <Widget>[
Text(
'Hello World',
),
],
),
),
),
),
);
}
}
但是,它不会对按下控制键使用react,也不会输出预期的消息。在调试代码时,我注意到
ShortcutManager
的实例确实处理按键事件的不包含我指定的按键组合,而是其他九个按键组合。有谁知道,如何使用
Shortcuts
和 Actions
对键盘快捷键使用react的 API?
最佳答案
在您的示例中,您的想法是正确的,但是您缺少的是 Focus
小部件。如果小部件没有焦点,则其祖先 Shortcuts
小部件不会接收关键事件来处理它。
看到这个 Dartpad example看看我的意思。当按下“Alt+N”键时,该示例添加了一个新的“家庭作业”。
您也可以使用 FocusableActionDetector结合了 Shortcuts
, Actions
, 和鼠标悬停处理到一个小部件中。
关于flutter - 如何处理键盘快捷键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61960260/