Flutter-在鼠标单击未关注的小部件中,如何检测键握持(CTRL)?
我可以使用focus(例如focusnode
)识别CTRL何时按下CTRL。但是为此,小部件需要首先集中精力。有谁有任何提示,即当小部件不集中或甚至无法获得焦点时如何检测此CTRL?
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Center(
child: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
spacing: 16,
children: [
_button(1),
_button(2),
GestureDetector(
onTap: () => print('Tap!'),
child: Container(
color: Colors.yellow,
padding: const EdgeInsets.all(6),
child: const Text('Tap with CTRL')))
]));
}
Widget _button(int number) {
return ElevatedButton(
onPressed: () => print('button $number'),
child: Text('Button $number'));
}
}
I can identify when CTRL is pressed using focus like FocusNode
. But for that, the widget needs to be focused first. Does anyone have any tips on how to detect this CTRL when the widget is not focused or when it can't even gain focus?
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Center(
child: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
spacing: 16,
children: [
_button(1),
_button(2),
GestureDetector(
onTap: () => print('Tap!'),
child: Container(
color: Colors.yellow,
padding: const EdgeInsets.all(6),
child: const Text('Tap with CTRL')))
]));
}
Widget _button(int number) {
return ElevatedButton(
onPressed: () => print('button $number'),
child: Text('Button $number'));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用以下操作:
在我对Linux桌面应用程序的实验中,两个Ctrl键中的任何一个都可以使
LogicalKeyboardKey.controlleft
和logicalKeyboardKey.controlright
中存在。值得注意的是,只有logicalKeyboardKey.control
在集合中找不到。Use this:
In my experiment with Linux Desktop app, any of the two Ctrl keys make both
LogicalKeyboardKey.controlLeft
andLogicalKeyboardKey.controlRight
present in the set. Notably, justLogicalKeyboardKey.control
is never found in the set.