[在'C'之前释放Ctrl时如何用Qt捕获Ctrl + C键事件?

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

我想在用户释放Ctrl + C时调用一些自定义复制代码。在Ctrl之前释放C时,Qt发送与QKeySequence::Copy匹配的键事件。在C之前释放Ctrl时,释放事件不匹配。

Ctrl出现按键释放事件时,是否有办法查看是否仍按住C

[当我不处理首先发布的[[Ctrl]时,该事件会传递并进行常规复制,这正是我不想发生的情况。

bool MyWidget::eventFilter(QObject* object, QEvent* event) { // the text edit box filters its events through here if (object == m_text_edit_box) { if (event->type() == QEvent::KeyPress) { QKeyEvent *key_event = static_cast<QKeyEvent*>(event); if (key_event->matches(QKeySequence::Copy)) { // don't do anything and don't pass along event return true; } } else if (event->type() == QEvent::KeyRelease) { QKeyEvent *key_event = static_cast<QKeyEvent*>(event); if (key_event->matches(QKeySequence::Copy)) { // we only get in here if 'c' is released before ctrl callCustomCopy(); return true; } } } // pass along event return false; }
c++ qt events keyboard copy-paste
1个回答
1
投票
您可以查询字母'C'和元键

Ctrl,而不依赖于key_even-> matches()。您当然可以在keydown事件存储区中将事件过滤器定位的对象中存储keydown序列与副本匹配的事实。

此(未测试的)可能对您有用,请注意,静态变量应该是包含该类的类的成员变量,在本示例的上下文中,这似乎更清楚。您要完成的操作的确切逻辑可能需要在事件之间传递更多的状态信息。

bool MyWidget::eventFilter(QObject* object, QEvent* event) { // Remember state between events static foundCopy = false; // the text edit box filters its events through here if (object == m_text_edit_box) { if (event->type() == QEvent::KeyPress) { QKeyEvent *key_event = static_cast<QKeyEvent*>(event); if (key_event->matches(QKeySequence::Copy)) { foundCopy = true; // don't do anything and don't pass along event return true; } else { foundCopy = false; // This is another sequence, ignore and pass event // Note that this will trigger with ctrl+c+a and others } } else if (event->type() == QEvent::KeyRelease) { QKeyEvent *key_event = static_cast<QKeyEvent*>(event); if (foundCopy) { callCustomCopy(); foundCopy = false; return true; } // This should keep the system copy from triggering if (key_event->matches(QKeySequence::Copy)) { return true; } } } // pass along event return false; }

另一种方法是收集当前按下的所有键的实际状态,然后在释放一个键时查看仍在按下的键。 

[从用户界面的角度,请记住,所有键盘操作都是在按下时执行的(例如,键入,粘贴窗口),通常在释放时执行操作可能会使用户感到困惑,尤其是当该操作有可见的结果时。我无法从您的示例中看出您要实现的目标。

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