用按压键盘键检测点击

发布于 2025-01-22 22:13:22 字数 127 浏览 0 评论 0原文

我正在尝试实现桌面应用程序的列表视图,该应用程序能够多选择项目。在桌面上,我们通过单击项目或单击并持有控制键来做到这一点。要选择一个项目,您只需添加墨水或eSturerocgognizer,但是如何在单击时检测到也有控制键?我找不到任何建议

I am trying to implement a ListView for Desktop applications, which is able to multiselect the items. On Desktop we do this by either clicking an item, or clicking and holding the control key. To select an item, you simply can add an Inkwell or GestureRecognizer, but how do I detect on click that there is also the control key pressed? I couldn't find any suggestions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

反话 2025-01-29 22:13:22

您可以玩这个小部件。确保以桌面模式运行。

  • 我们需要听键盘事件。为此,我正在使用rawKeyboardListener

  • 保持跟踪ctrl事件

  • 通过清除以前的选定项目在正常点击中进行单个选择,但是_isctrlpressed不要清除所选项目

onTap: () {
  if (!_isCTRLPressed) _selectedIndex.clear();
  _onTap(index);
}
演示小部件
class ItemSelection extends StatefulWidget {
  const ItemSelection({Key? key}) : super(key: key);

  @override
  State<ItemSelection> createState() => _ItemSelectionState();
}

class _ItemSelectionState extends State<ItemSelection> {
  List<int> _selectedIndex = [];

  void _onTap(index) {
    if (_selectedIndex.contains(index)) {
      _selectedIndex.remove(index);
    } else {
      _selectedIndex.add(index);
    }
    setState(() {});
  }

  final fc = FocusNode();
  // you can use list for multi-purpose
  bool _isCTRLPressed = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: RawKeyboardListener(
        focusNode: fc,
        autofocus: true,
        onKey: (event) {
          if (event.isKeyPressed(LogicalKeyboardKey.controlLeft)) {
            if (event is RawKeyDownEvent) {
              _isCTRLPressed = true;
            }
          } else {
            _isCTRLPressed = false;
          }
        },
        child: GridView.count(
          crossAxisCount: 6,
          mainAxisSpacing: 2,
          crossAxisSpacing: 2,
          children: List.generate(
            55,
            (index) => GestureDetector(
              onTap: () {
                if (!_isCTRLPressed) _selectedIndex.clear();
                _onTap(index);

                debugPrint("ctrlPressed $_isCTRLPressed");
              },
              child: Container(
                color: _selectedIndex.contains(index)
                    ? Colors.cyanAccent
                    : Colors.grey,
                alignment: Alignment.center,
                child: Text(index.toString()),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

You can play with this widget. Make sure to run as desktop mode.

  • we need to listen keyboard event. For that I am using RawKeyboardListener.

  • keep track ctrl event

  • single selection happen on normal tap by clearing previous selected item, but while _isCTRLPressed don't clear the selected items

onTap: () {
  if (!_isCTRLPressed) _selectedIndex.clear();
  _onTap(index);
}
Demo widget
class ItemSelection extends StatefulWidget {
  const ItemSelection({Key? key}) : super(key: key);

  @override
  State<ItemSelection> createState() => _ItemSelectionState();
}

class _ItemSelectionState extends State<ItemSelection> {
  List<int> _selectedIndex = [];

  void _onTap(index) {
    if (_selectedIndex.contains(index)) {
      _selectedIndex.remove(index);
    } else {
      _selectedIndex.add(index);
    }
    setState(() {});
  }

  final fc = FocusNode();
  // you can use list for multi-purpose
  bool _isCTRLPressed = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: RawKeyboardListener(
        focusNode: fc,
        autofocus: true,
        onKey: (event) {
          if (event.isKeyPressed(LogicalKeyboardKey.controlLeft)) {
            if (event is RawKeyDownEvent) {
              _isCTRLPressed = true;
            }
          } else {
            _isCTRLPressed = false;
          }
        },
        child: GridView.count(
          crossAxisCount: 6,
          mainAxisSpacing: 2,
          crossAxisSpacing: 2,
          children: List.generate(
            55,
            (index) => GestureDetector(
              onTap: () {
                if (!_isCTRLPressed) _selectedIndex.clear();
                _onTap(index);

                debugPrint("ctrlPressed $_isCTRLPressed");
              },
              child: Container(
                color: _selectedIndex.contains(index)
                    ? Colors.cyanAccent
                    : Colors.grey,
                alignment: Alignment.center,
                child: Text(index.toString()),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文