简体   繁体   English

如何检查PyQt中是否同时按下了CTRL和SHIFT?

[英]How to check if CTRL and SHIFT are pressed simultaneously in PyQt?

I found a lot of examples how to get key modifiers like this one . 我发现了很多示例,如何获得像这样的关键修饰符。 But I don't get it working for checking if they are pressed and hold at the same time. 但是我无法检查它们是否同时按住。

This should be rather easy, as CTRL + SHIFT are standard key combinations. 这应该相当容易,因为CTRL + SHIFT是标准的组合键。

From the above linked example: 从上面的链接示例中:

modifiers = QtGui.QApplication.keyboardModifiers()
if modifiers == QtCore.Qt.ShiftModifier:
    print('Shift+Click')
elif modifiers == QtCore.Qt.ControlModifier:
    print('Control+Click')
else:
    print('Click')

We see that either CTRL or SHIFT are tested for. 我们看到CTRLSHIFT都经过测试。 But I need a check which finds if both are pressed at the same time. 但是我需要检查一下是否同时按下两个按钮。

I tested lots of variants like this one: 我测试了很多这样的变体:

if modifiers == (QtCore.Qt.ControlModifier and QtCore.Qt.ShiftModifier):

I found something written in C but I don't seem to be able to translate it. 我找到了一些用C编写的东西,但似乎无法翻译。

My Python is rusty but try if (modifiers & QtCore.Qt.ControlModifier) and (modifiers & QtCore.Qt.ShiftModifier): 我的Python生锈了,但尝试if (modifiers & QtCore.Qt.ControlModifier) and (modifiers & QtCore.Qt.ShiftModifier):

This checks if both Control and Shift bitfields are set in modifiers . 这将检查是否在modifiers同时设置了Control和Shift位域。

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

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