Flutter-在鼠标单击未关注的小部件中,如何检测键握持(CTRL)?

发布于 2025-02-05 15:50:24 字数 1065 浏览 3 评论 0原文

我可以使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光瘦了 2025-02-12 15:50:25

使用以下操作:

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

在我对Linux桌面应用程序的实验中,两个Ctrl键中的任何一个都可以使LogicalKeyboardKey.controlleftlogicalKeyboardKey.controlright中存在。值得注意的是,只有logicalKeyboardKey.control在集合中找不到。

Use this:

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

In my experiment with Linux Desktop app, any of the two Ctrl keys make both LogicalKeyboardKey.controlLeft and LogicalKeyboardKey.controlRight present in the set. Notably, just LogicalKeyboardKey.control is never found in the set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文