0
$\begingroup$

How would I detect a Blender keyboard shortcut in edit mode? I'm trying to detect the merge operation (Alt+M), but I don't want to change the shortcut of anything, just execute a function at the same time that the operation is called by the key combination. I thought about the draw function which constantly listens, but that doesn't seem ideal.

$\endgroup$
-1
$\begingroup$

Found it through the modal operator. There were other posts touching on this, but basically it will keep Blender's keymaps but still execute your custom code.

import bpy

class ModalOperator(bpy.types.Operator):
    bl_idname = "object.modal_operator"
    bl_label = "Simple Modal Operator"

    def execute(self, context):
        print("This is the modal operator")
        return {'FINISHED'}

    def modal(self, context, event):
        if event.type == 'ESC':  # Confirm
            print("This is finished")
            return {'FINISHED'}
        elif event.alt:
            if event.type == 'M':  # Confirm
                print("This is a Blender Shortcut key")
            return {'PASS_THROUGH'}

        return {'PASS_THROUGH'}

    def invoke(self, context, event):
        print("This is the invoker")

        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}

bpy.utils.register_class(ModalOperator)

bpy.ops.object.modal_operator('INVOKE_DEFAULT')
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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