Stop Modal Operator Whenever Initial Button Is Released

Hello, Blender Artists, hope You’re all doing fine and stay healthy!

Lately, my right hand feels really weird:

  • this maybe kind of Repetitive Strain Injury, but not that pain
  • this makes me feel more comfortable using my tablet instead of my mouse
  • I just know that I must stop this before it’s too late

So right now:

  • I am trying to create a script to be able to FULLY navigate around the scene without the MMB
  • honestly, such thing can simply be done by:
    – creating a new shortcut with LMB and – let say – ‘C’ (as key modifier)
    – and then signing ‘view3d.rotate’ operator to it
    – but this can’t work in certain mode like GP Draw Mode or any Sculpt Mode, etc.
  • my solution to this problem is to create a modal where when the chosen button is pressed:
    – I go into rotation mode until the same button is released.
    – LMB will execute ‘ops.view3d.rotate’
  • So, here is what I’ve got so far:
class PENTAB_OT_VIEW3D_ROTATE(Operator):
    bl_label = "Pentab Rotate"
    bl_idname = 'pentab.rotate'
    bl_space_type = 'VIEW_3D'

#    
    def execute(self, context):
        bpy.ops.view3d.rotate('INVOKE_DEFAULT')
    
    def modal(self, context, event):
        # FINISH THE MODAL IF 'C' IS RELEASED
        if (event.type == 'C' and event.value == 'RELEASE'):
            return {'FINISHED'}
        # EXECUTE ROTATE WHEN 'LEFTMOUSE' IS PRESSED
        elif (event.type == 'LEFTMOUSE' and event.value == 'PRESS'):
            self.execute(self)
        return {'RUNNING_MODAL'}
        
    def invoke(self, context, event):
        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}

But, the problem with the code is:

  • when ‘C’ is released but ‘LMB’ is still pressed, the modal won’t stop
  • and it won’t even stop until I press ‘C’ again (so Blender capture the ‘RELEASE’ event).

So here is my question: How can I stop the modal whenever ‘C’ is released, whether the LMB is pressed or not?