4

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

  • See [this answer](https://stackoverflow.com/a/8808302/984421) for a solution (but for modifier keys only). – ekhumoro Aug 18 '17 at 01:52
  • 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 Aug 18 '17 at 11:25

1 Answers1

2
def mouseMoveEvent(self, event):
    modifiers = QApplication.keyboardModifiers()
    Mmodo = QApplication.mouseButtons()
    if bool(Mmodo == QtCore.Qt.LeftButton) and (bool(modifiers == QtCore.Qt.ControlModifier)):
        print 'yup'
DJK
  • 8,924
  • 4
  • 24
  • 40