Does Blender use a state machine onMouseEvent to handle keys pressed or add handlers?

I am an avid Blender,ZBrush,Adobe,etc user and this question is within the context of systems implemented to handle (chained) shortcuts for a viewport that generally execute 2 functionalities: events responding to a shortcut press and functionality that depends on the keydown state of shortcuts.

  1. Is there any literature or documentation that discusses implementing such a shortcut handling systems?
  2. My position, is of the considered patterns below, #2 is correct, however are there any distinct advantages or explicit decision to use a state-machine like handling system as described in #1?
  3. Of the following software, please let me know if you have any insight on their implementations:

a) Blender
b) GIMP
c) Inkscape

Any other source available robust implementations that you know of.

Consider the following 2 psuedocode patterns

    viewport.addEvent(MOUSE_MOVE,mouseMoveHandler)

    function mouseMoveHandler(e) {
        if(shortCutsManager.getKey(DELETE_KEY)) {
                 doDeleteUnderMouse();
        } else if(shortCutsManager.getKey(DRAG_KEY)) {
                 dragMouseMove();
        }
    }

or something like:


foreach(k in shortCutsManager.mouseMoveKeys.keys()) {
            var keyContext=shortCutManagers.mouseMoveKeys.get(k);
    viewport.addEvent(MOUSE_MOVE,keyContext.mouseMoveHandler);
    }
    function deleteKey_mouseMoveHandler(e) {
            viewport.removeEvent(MOUSE_MOVE,currentMouseMoveHandler);
            currentMouseMoveHandler=deleteKey_mouseMoveHandler;
            doDeleteUnderMouse();
    }

My position is that the correct and effecient implementation is to maintain the explicit handler for a recurring event, eg the second implementation (#2). However, there is likely a set of developers who advocate a state-machine like implementation of #1.

You should check the python templates that come with blender (text editor menu > templates > python), specially the modal ones.
For more resources, take a read of the api documentation: https://docs.blender.org/api/current

Don’t intend to spend the entire day going through Blender source code for a question that takes 2 seconds to answer if someone knows the answer.

Its not that I feel entitled to an answer, it’s a simple fact of physics that I do not have the time to investigate Blender’s implementation through source at this time and will proceed shortly irregardless. You’re more than welcome to not answer.

You can create an event handler that checks whatever input events there are, but this is kind of weak. The correct approach (unless you’re doing something very specific), is the one used in the modal templates.

This means that when you call some of your operators (which can be assigned to any shortcut), you return {‘MODAL’} so that blender knows that the operator is not finished, and in the next loop, it will call the operator again until it’s {‘FINISHED’} or {‘CANCELLED’}. It’s up to your operator to check the events happening at each step and decide what to do with them.

Its not that I feel entitled to an answer, it’s a simple fact of physics that I do not have the time to investigate Blender’s implementation through source at this time and will proceed shortly irregardless. You’re more than welcome to not answer.

The only reason I pointed to the api documentation, is because that’s the place to look for more information about Modal operators and Events.

That is very interesting how the lifecycle is managed. Thank you