pyqgis – Capturing CTRL+Key in KeyPressEvent for PyQGIS QgsMapTool

eventspyqgispyqt

I created a Line with help of QgsMapTool and I'm able to capture the single key in keyPressEvent like this:

class buildingEditTool(QgsMapToolEdit):

    def keyPressEvent(self, event):
        
        if event.key() == Qt.Key_Backspace:
            ''' Do Something'''

then for a key combination I tried

class buildingEditTool(QgsMapToolEdit):

    def keyPressEvent(self, event):
        
        if event.matchs(QKeySequence(ctrl+k)):
            ''' Do Something'''

but it did not work.

How do I capture combination keys in keyPressEvent?

Best Answer

QKeyEvent has a method modifiers(). You should use it.

def keyPressEvent(self, event):

    if event.modifiers() & Qt.ControlModifier:
        if event.key() == Qt.Key_R:
            print("Ctrl + R")
Related Question