确定表达式中是否按下了键(Python)(PyQT)

时间:2017-08-18 00:27:40

标签: python python-3.x user-interface pyqt pyqt4

在mouseMoveEvent方法中,我看到如下代码检查是否正在按下鼠标左键或右键(如下所示)。有没有办法检查当前是否正在按某些键盘键?理想情况下,我希望在鼠标左键单击并移动并按下某个键时执行操作。我没有看到使用keyPressEvent的方法,因为它与mouseMoveEvent分开调用(就像本例中没有使用mousePressEvent)。

def mouseMoveEvent(self, event):
    if event.buttons() & QtCore.Qt.LeftButton:
        #run this when mouse is moved with left button clicked
    elif event.buttons() & QtCore.Qt.RightButton:
        #run this when mouse is moved with right button clicked

编辑: 基于ekhumoro的评论方法现在看起来像这样。它仅适用于键修饰符:

def mouseMoveEvent(self, event):
    modifiers = QtGui.QApplication.keyboardModifiers()
    if bool(event.buttons() & QtCore.Qt.LeftButton) and (bool(modifiers == QtCore.Qt.ControlModifier)):
        #run this when mouse is moved with left button and ctrl clicked
    elif event.buttons() & QtCore.Qt.LeftButton:
        #run this when mouse is moved with left button clicked             
    elif event.buttons() & QtCore.Qt.RightButton:
        #run this when mouse is moved with Right button clicked

如果有人能够使用任何密钥,我们将非常感谢

1 个答案:

答案 0 :(得分:0)

def mouseMoveEvent(self, event):
    modifiers = QApplication.keyboardModifiers()
    Mmodo = QApplication.mouseButtons()
    if bool(Mmodo == QtCore.Qt.LeftButton) and (bool(modifiers == QtCore.Qt.ControlModifier)):
        print 'yup'