0
$\begingroup$

I would like to detect the state of some keys (shift, ctrl...) when the user is changing the value of a property in my addon in order to perform different actions. This would allow me to considerably extend the features available in the UI while keeping a limited set of graphic components.

My current use case is this: I have a FloatProperty exposed on my addon to control the start time of an event in the scene. When the user changes its value the event starts earlier or later, at the frame corresponding to this value. I would like to check if the Ctrl button is pressed when the user changes the value, and if it is the case I would like to force this value to be the current time (by using a set function for this property).

All I can find so far is related to the use of a modal operator and an Event. In my case here I strongly doubt I need to set the operator as modal.

Do you know how the state of the keyboard keys can be detected (in the Invoke or Execute function of my operator very likely)?

$\endgroup$
1
$\begingroup$

Oki, I eventually found how to get the modifier keys status and how to use the Event structure in a non-modal operator, this thanks to this post: Python: Ctrl+Click for buttons (capture invocation event)

The answer is pretty straightforward and easy to use once we know it: the invoke function of an operator has an even argument that can easily be queried.

Quick example from the above link:

def invoke(self, context, event):
    wm = context.window_manager

    ev = []
    if event.ctrl:
        ev.append("Ctrl")
    if event.shift:
        ev.append("Shift")
    if event.alt:
        ev.append("Alt")
    if event.oskey:
        ev.append("OS")
    ev.append("Click")

    self.report({'INFO'}, "+".join(ev))

    return wm.invoke_props_dialog(self)
$\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.