简体   繁体   English

C ++ Qt QShortcut with numpad key

[英]C++ Qt QShortcut with numpad key

QShortcut makes it easy to connect a QShortcutEvent (Key press, combination or sequence) to a slot method, eg: QShortcut可以轻松地将QShortcutEvent(按键,组合或序列)连接到插槽方法,例如:

QShortcut *shortcut = new QShortcut( QKeySequence(Qt::Key_7), this, 0, 0, Qt::ApplicationShortcut);

(Hint: for number keys, a QSignalMapper can be used to map the QShortcut's activated() signal to a Slot with int parameter). (提示:对于数字键,可以使用QSignalMapper将QShortcut的activated()信号映射到带有int参数的Slot)。

However, in this example, with NumLock ( numpad enabled), both '7' keys will trigger the Shortcut's activated() signal. 但是,在此示例中,使用NumLock(启用了numpad ),两个“7”键都将触发Shortcut的activated()信号。

Is there a way to detect the different keys other than filtering or reimplementing a widget's keyPressEvent and check QKeyEvent::modifiers() for Qt::KeypadModifier ? 除了过滤或重新实现小部件的keyPressEvent以及检查Qt :: KeypadModifier的 QKeyEvent :: modifiers()之外 ,有没有办法检测不同的键?

Digging further, I found 我发现,进一步挖掘

QTBUG-20191 Qt::KeypadModifier does not work with setShortcut linking to a patch that has been merged into 4.8 in Sept. 2012 and which comes with a test case using QTBUG-20191 Qt :: KeypadModifier不能与setShortcut链接到2012年9月合并到4.8补丁 一起使用,后者附带一个测试用例

button2->setShortcut(Qt::Key_5 + Qt::KeypadModifier);

which does not work for my QShortcut on Qt 4.8.1, ie neither of the '7' keys are recognized using (adding) the modifier flag. 这对Qt 4.8.1上的QShortcut不起作用,即使用(添加)修饰符标志都不能识别'7'键。

So I guess the quickest way would be installing a filter to detect the modifier and let all other keyEvents be handled by the default implementation to be useable with QShortcut? 所以我想最快的方法是安装一个过滤器来检测修饰符,并让默认实现处理所有其他的keyEvent,以便与QShortcut一起使用?

For this you can use keyReleaseEvent (QKeyEvent *event) For example 为此,您可以使用keyReleaseEvent (QKeyEvent * event)例如

void Form::keyReleaseEvent(QKeyEvent *event)    {
    int key = event->nativeScanCode();

    if( key == 79 ) //value for numpad 7
    {
       //your statement   
    }


}

You can use Qt.KeypadModifier , For example [Python]: 您可以使用Qt.KeypadModifier ,例如[Python]:

def keyPressEvent(self, event):
    numpad_mod = int(event.modifiers()) & QtCore.Qt.KeypadModifier
    if event.key() == QtCore.Qt.Key5 and numpad_mod:
        #Numpad 5 clicked

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM