12

Is it possible to assign a keyboard shortcut to a (custom or not...) script of the Processing Toolbox in QGIS?

I have not found any information about it so far.

2
  • You can assign shortcut keys to plugins which is described here: How to call a method by a key shortcut. Perhaps this can be modified to work within a script?
    – Joseph
    Commented Nov 2, 2015 at 13:35
  • Thx Joseph ... i'm gonna check this !
    – Snaileater
    Commented Nov 2, 2015 at 17:37

1 Answer 1

16

This is an example of how to open the Processing "Join attributes" algorithm by pressing Ctrl + Shift + 1 (you can copy and paste it into the QGIS Python console):

For QGIS v3.x

# Function to open the "Join attributes" algorithm's UI
def openAlgorithm():
    processing.execAlgorithmDialog("native:joinattributestable")

# Assign "Ctrl + Shift + 1" to openAlgorithm()
from qgis.PyQt.QtGui import QKeySequence
from qgis.PyQt.QtWidgets import QShortcut
from qgis.PyQt.QtCore import Qt
shortcut = QShortcut(QKeySequence(Qt.ControlModifier + Qt.ShiftModifier + Qt.Key_1), iface.mainWindow())
shortcut.setContext(Qt.ApplicationShortcut)
shortcut.activated.connect(openAlgorithm)

That's it! If you press Ctrl + Shift + 1 the Join Attributes UI will open:

enter image description here


Note 1.1: Get a readable list of algorithm ids and names in this way:

for alg in QgsApplication.processingRegistry().algorithms():
    print("{}:{} --> {}".format(alg.provider().name(), alg.name(), alg.displayName()))

Note 1.2: See Qt5 docs for a comprehensive list of keys.

For QGIS v2.x

# Function to open the "Join attributes" algorithm's UI
# See http://gis.stackexchange.com/questions/156633/how-to-launch-processing-tool-user-interface-using-pyqgis
from processing.core.Processing import Processing
from processing.gui.CommanderWindow import CommanderWindow
cw = CommanderWindow(iface.mainWindow(), iface.mapCanvas())
def openAlgorithm():
    alg = Processing.getAlgorithm("qgis:joinattributestable")
    if alg is not None:
        cw.runAlgorithm(alg)

# Assign "Ctrl+Shift+1" to openAlgorithm()
from PyQt4.QtWidgets import QShortcut
from PyQt4.QtGui import QKeySequence
from PyQt4.QtCore import Qt
shortcut = QShortcut(QKeySequence(Qt.ControlModifier + Qt.ShiftModifier + Qt.Key_1), iface.mainWindow())
shortcut.setContext(Qt.ApplicationShortcut)
shortcut.activated.connect(openAlgorithm)

That's it! If you press Ctrl + Shift + 1 the Join Attributes UI will open:

enter image description here

Note 2.1: You can get the names of the available algorithms by entering these lines in the QGIS Python console:

import processing
processing.alglist()

Note 2.2: See Qt4 docs for a comprehensive list of keys.

Note 2.3: You can call shortcut.activated.disconnect(openAlgorithm) to finish the association between the shortcut and your algorithm's UI.

4
  • 1
    It does for sure ...
    – Snaileater
    Commented Nov 3, 2015 at 17:36
  • 2
    Thank you. For some reason I see in Qt documentation QShortcut under QtWidgets and not QtGui (both Qt4 and Qt5). In my case for QGIS3 and PyQt5 I had to do: from PyQt5.QtWidgets import QShortcut
    – Miro
    Commented Jul 22, 2019 at 23:03
  • It's now fixed Miro! Commented Jun 6, 2021 at 3:05
  • How do I need to change this in Qgis v3.x to load my own project model instead of a native process? Can anyone help? Commented Apr 19, 2023 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.