Qt多键组合事件 [英] Qt multiple key combo event

查看:848
本文介绍了Qt多键组合事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Qt 4.6,我想对正在按住的多键组合(例如Key_Q + Key_W)做出反应。因此,当你按住一个键组合,事件应该一直被调用,就像使用单键事件一样。我试图使用QShortcuts和启用自动重复为他们,但不起作用:

I'm using Qt 4.6 and I'd like to react to multi-key combos (e.g. Key_Q+Key_W) that are being held down. So when you hold down a key combo, the event should be called all the time, just the same way as it works with single key events. I tried to use QShortcuts and enable autorepeat for them, but that didn't work:

keyCombos_.push_back(new QShortcut(QKeySequence(Qt::Key_W, Qt::Key_D), this));
connect(keyCombos_[0], SIGNAL(activated()), SLOT(keySequenceEvent_WD()));
setShortcutAutoRepeat(keyCombos_[0]->id(), true);

当使用这种方法时,我也有一个问题,我不能捕获单个Key_W

When using this approach I also have the problem that I can't catch single Key_W (or whatever the first Key in the keysequence is) strokes anymore.

谢谢,
Thomas

Thanks, Thomas

推荐答案

QShortcut不支持您要查找的功能。您只能使用Shift,Ctrl,Alt和Meta等修改键进行组合。

QShortcut does not support the functionality you're looking for. You can only make combinations with modifier keys like Shift, Ctrl, Alt and Meta.

您的代码所做的是创建一个快捷方式当用户首先按下W然后D时。这也是为什么它会与只响应W的其他快捷方式冲突的原因。

What your code does is to make a shortcut that responds when the user first presses W and then D. This is also why it will conflict with other shortcuts that respond to just W.

当你想做W和D同时按 ,您必须重写QWidget的 keyPressEvent keyReleaseEvent 方法,以便跟踪它们的按下状态,并且一旦按下它们就手动调用你的处理函数。如果你没有一个合适的QWidget子类在使用中,你不得不介绍它,或者安装一个事件过滤器在正确的地方使用 QObject :: installEventFilter ,如果它应该是一个全局快捷方式,可能在您的应用程序对象上。

When you want to do something when both W and D are pressed at the same time, you'll have to override QWidget's keyPressEvent and keyReleaseEvent methods in order to keep track of their pressed state, and manually call your handler function once they are both pressed. If you don't have a suitable QWidget subclass in use you'd either have to introduce it, or install an event filter in the right place using QObject::installEventFilter, possibly on your application object if it's supposed to be a global shortcut.

这篇关于Qt多键组合事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆