0

Progressing along with my digitizing work mentioned here, I thought of an improvement to my method.

In short: using a form, I modify a project variable which is then autofilled on new features.

I would like to use a keyboard shortcut to increase or decrease that variable by 1 - same as the bt_up and bt_down buttons on said form.

Using a keyboard shortcut would significantly speed up altering the variable and thus digitizing.

Shortcuts on the form itself won't do, because they are only active when the form has the active scope, whereas, during digitizing, the qgis edit window has the active scope. activating the form first defeats the keyboard shortcut aim.

For this, it seems I need to add a project-level action, to link that later to a keyboard shortcut. a layer level action does not seem to offer that possibility.

I have yet not found where/how to add such a custom project action.

Any suggestions/pointers?

The action is simple increasing the variable by 1:

p_project = QgsProject.instance()
v_var = QgsExpressionContextUtils.projectScope( p_project).variable('DEF_Height')
QgsExpressionContextUtils.setProjectVariable( p_project, 'DEF_Height', str(int( v_var) + 1))
2
  • 1
    This answer might help you. It registers a keypress when the map canvas has focus. It could be adapted to connect to the iface.mainWindow().keyPressed signal instead.
    – Matt
    Apr 16, 2023 at 16:05
  • Thank you Matt. That worked great. I could reuse the existing functions in the keypressed function, meaning the form info is updated as well (something I was willing to live without)
    – glenndm
    Apr 16, 2023 at 17:42

1 Answer 1

1

answer as indicated by Matt

I added this to the form code to get the desired result

def cmd_key_pressed(event):
    if event.key() == 43:  # 43 = +
        cmd_bt_up()
    elif  event.key() == 47: # 47 = / as minus alternative - conflict with negative numbers
        cmd_bt_down()
    
#keypress event on map
c_canvas = iface.mapCanvas()
c_canvas.keyPressed.connect( cmd_key_pressed)

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.