我可以使用焦点来识别何时按下 CTRL,如
FocusNode
。但为此,需要首先关注小部件。有谁有关于如何在小部件未聚焦或什至无法获得焦点时检测此 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'));
}
}
使用这个:
onTap: () {
if (!RawKeyboard.instance.keysPressed
.contains(LogicalKeyboardKey.controlLeft)) {
print('Clicked with Control');
}
}
在我对 Linux 桌面应用程序的实验中,任意两个 Ctrl 键都会使
LogicalKeyboardKey.controlLeft
和 LogicalKeyboardKey.controlRight
出现在集合中。值得注意的是,只有 LogicalKeyboardKey.control
在集合中从未被发现。