如何使用Qt在mousePressEvent方法中获取按键?

时间:2018-03-16 08:24:44

标签: python qt key mouseevent

我正试图在窗口的mousePressEvent方法中按下哪些键。 我知道如何使用keyboardModifiers QApplication方法得到一些关键字,但它仅适用于少数几个键。 一段代码开头:

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys



class SurfViewer(QMainWindow):
    def __init__(self, parent=None):
        super(SurfViewer, self).__init__()
        self.parent = parent
        self.setFixedWidth(300)
        self.setFixedHeight(100)
        self.globallayout = QVBoxLayout()
        l_a = QLabel('A')
        self.globallayout.addWidget(l_a)
        self.setLayout(self.globallayout)

    def mousePressEvent(self, event):
        if event.buttons()== Qt.RightButton :
            print('Right click')
            #Call a function which return pressed keys (whatever the key)
        if event.buttons()== Qt.LeftButton :
            print('Left click')
            if QApplication.keyboardModifiers() == Qt.ControlModifier:
                print('Ctrl')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = SurfViewer(app)
    ex.setWindowTitle('window')
    ex.show()
    sys.exit(app.exec_( ))

所以我想念的是一种询问按下哪些键的方法,而不仅仅是keyboardModifiers可用的键。我看了QKeyEvent Class,但我不知道如何从mousePressEvent强制调用

更新

我尝试了@Changlong的答案:

Changlongfrom PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys



class SurfViewer(QMainWindow):
    def __init__(self, parent=None):
        super(SurfViewer, self).__init__()
        self.parent = parent
        self.setFixedWidth(300)
        self.setFixedHeight(100)
        self.globallayout = QVBoxLayout()
        l_a = QLabel('A')
        self.globallayout.addWidget(l_a)
        self.setLayout(self.globallayout)

        self.keys=[]

    def mousePressEvent(self, event):
        if event.buttons()== Qt.RightButton :
            print('Right click')
            self.keyPressEvent()
        if event.buttons()== Qt.LeftButton :
            print('Left click')
            if QApplication.keyboardModifiers() == Qt.ControlModifier:
                print('Ctrl')

    def keyPressEvent(self, event):
        if event.key()== Qt.Key_A :
            print('A pressed')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = SurfViewer(app)
    ex.setWindowTitle('window')
    ex.show()
    sys.exit(app.exec_( ))

但如果我按下A键,则会连续调用keyPressEvent。所以我得到了:

A pressed
....
A pressed
A pressed
Right click
A pressed
....
A pressed
A pressed

1 个答案:

答案 0 :(得分:2)

供您参考:

def keyPressEvent(self, event):
    if event.key()== Qt.Key_A :
        print('A pressed')

希望有所帮助。

结果:

$ python3 pyqtqkeyevent.py 
QWidget::setLayout: Attempting to set QLayout "" on SurfViewer "", which already has a layout
Right click
Left click
A pressed

按下鼠标左/右按钮时,您的代码正常。

更新

def mousePressEvent(self, event):
    modifierPressed = QApplication.keyboardModifiers()
    modifierName = ''
    if (modifierPressed & Qt.AltModifier) == Qt.AltModifier:
        modifierName += 'Alt '

    if (modifierPressed & Qt.ControlModifier) == Qt.ControlModifier:
        modifierName += 'Ctrl '

    if (modifierPressed & Qt.ShiftModifier) == Qt.ShiftModifier:
        modifierName += 'Shift '

    if (modifierPressed & Qt.MetaModifier) == Qt.MetaModifier:
        modifierName += 'Meta '

    if (modifierPressed & Qt.KeypadModifier) == Qt.KeypadModifier:
        modifierName += 'Keypad '

    if (modifierPressed & Qt.GroupSwitchModifier) == Qt.GroupSwitchModifier:
        modifierName += 'GroupSwitch '

    if event.buttons() == Qt.RightButton:
        print('Right Click: ',modifierName)
        #self.keyPressEvent()
    if event.buttons() == Qt.LeftButton:
        print('Left Click: ',modifierName)


def keyPressEvent(self, event):
    if event.key() == Qt.Key_A:
        print('A pressed')

def keyReleaseEvent(self, event):
    if event.key() == Qt.Key_A:
        print('A Release')

结果:

$ python3 pyqtkeymouseevent.py 
QWidget::setLayout: Attempting to set QLayout "" on SurfViewer "", which already has a layout
Left Click:  Ctrl 
Left Click:  Shift 
Left Click:  Meta 
Left Click:  
Left Click:  
Left Click:  Ctrl Shift 
Left Click:  Alt Ctrl Shift Meta 
Left Click:  Alt Meta 
Left Click:  Ctrl Meta 
Left Click:  Alt Ctrl 
Left Click:  Alt Shift 
Right Click:  
Right Click:  Alt Ctrl Shift Meta 
Right Click:  Ctrl 
Right Click:  Shift 
Right Click:  Meta 
A pressed
A Release

这是你想要的吗?

单击Alt和鼠标按钮单击,无法在我的桌面(Ubuntu Xfce)中工作,因为窗口管理器将其视为“拖动窗口操作”。