3
votes

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

1
See this answer for a solution (but for modifier keys only). - ekhumoro
If you want to use other keys, you will have to override keyPressEvent and keyReleaseEvent, and then set/unset a flag when the key is pressed/released. - ekhumoro

1 Answers

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