Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining if key is pressed in expression (Python) (PyQT)

In the mouseMoveEvent method I've seen code like below to check if either the left or right mouse buttons are being pushed (as shown below). Is there a way to check if certain keyboard keys are currently being pressed? Ideally I would like to have the action performed when the mouse is leftclicked and moved and a certain key is being pushed. I don't see a way to use keyPressEvent as this is called separately from the mouseMoveEvent (just like mousePressEvent is not used in this example).

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

Edit: Based on ekhumoro's comment method now looks like this. It only works for key modifiers:

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

If anyone is able to get this to work with any key a response would be greatly appreciated

like image 374
Jake Verburgt Avatar asked Sep 11 '25 11:09

Jake Verburgt


1 Answers

def mouseMoveEvent(self, event):
    modifiers = QApplication.keyboardModifiers()
    Mmodo = QApplication.mouseButtons()
    if bool(Mmodo == QtCore.Qt.LeftButton) and (bool(modifiers == QtCore.Qt.ControlModifier)):
        print 'yup'
like image 111
DJK Avatar answered Sep 13 '25 01:09

DJK