Flutter - 如何在未聚焦的小部件中单击鼠标时检测按住键(CTRL)?

问题描述 投票:0回答:1

我可以使用焦点来识别何时按下 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'));
  }
}
flutter click key mouse
1个回答
0
投票

使用这个:

onTap: () {
  if (!RawKeyboard.instance.keysPressed
      .contains(LogicalKeyboardKey.controlLeft)) {
    print('Clicked with Control');
  }
}

在我对 Linux 桌面应用程序的实验中,任意两个 Ctrl 键都会使

LogicalKeyboardKey.controlLeft
LogicalKeyboardKey.controlRight
出现在集合中。值得注意的是,只有
LogicalKeyboardKey.control
在集合中从未被发现。

© www.soinside.com 2019 - 2024. All rights reserved.