I was asked to modify a script button on one of our tools such that its behavior is modified when it was shift-clicked. It doesn't look like it's possible to detect a key press in Lua without writing a new C module. Is there anything on the Fusion UI side that would let me send that information into the script as an argument?
At the moment, the easiest solution seems to be simply adding a second button for the alternate case, but I can imagine that being able to catch a key press would be useful.
Detect shift-click in a script button?
- Midgardsormr
- Fusionator
- Posts: 2048
- Joined: Wed Nov 26, 2014 8:04 pm
- Location: Los Angeles, CA, USA
- Been thanked: 11 times
- Contact:
- PeterLoveday
- Fusioneer
- Posts: 218
- Joined: Sun Sep 14, 2014 6:09 pm
Re: Detect shift-click in a script button?
It might be nice if the modifiers were passed to all events, but they're not (I assume you need this to work in 9.0.2).
It's not as clean as that would be, but you can detect keypresses:
It's not as clean as that would be, but you can detect keypresses:
- local ui = fu.UIManager
- local disp = bmd.UIDispatcher(ui)
- win = disp:AddWindow({
- ID = 'Win',
- Geometry = { 0, 0, 200, 50 },
- WindowTitle = 'KeyEvents',
- Events = { Close = true, KeyPress = true, KeyRelease = true, },
- ui:Button{ID = "Button", Text = "Button",},
- })
- itm = win:GetItems()
- function win.On.Win.Close(ev)
- disp:ExitLoop()
- end
- -- A flag to track shift state
- local shiftdown = false
- -- If the shift key is pressed, set our flag
- function win.On.Win.KeyPress(ev)
- if ev.Key == 0x1000020 then
- shiftdown = true
- itm.Button.Text = "Shift+Button"
- end
- end
- -- If the shift key is released, reset our flag
- function win.On.Win.KeyRelease(ev)
- if ev.Key == 0x1000020 then
- shiftdown = false
- itm.Button.Text = "Button"
- end
- end
- -- Now we can use our flag to differentiate button presses
- function win.On.Button.Clicked(ev)
- if shiftdown then
- print("Shift+Button")
- else
- print("Button")
- end
- end
- win:Show()
- disp:RunLoop()
- win:Hide()
- Midgardsormr
- Fusionator
- Posts: 2048
- Joined: Wed Nov 26, 2014 8:04 pm
- Location: Los Angeles, CA, USA
- Been thanked: 11 times
- Contact:
Re: Detect shift-click in a script button?
Oh, interesting! I hadn't thought to look at Qt because I wasn't using any UI Manager in that particular script, but I think I can figure out how to use that. It's certainly more elegant than the nonsense I was considering calling Python, storing the state in the node's CustomData, and then recalling it in the Lua! That was so many levels of wrong I couldn't bring myself to even try it, although I'm reasonably sure it would work.
Thanks! I'll report back with my results.
Thanks! I'll report back with my results.
- PeterLoveday
- Fusioneer
- Posts: 218
- Joined: Sun Sep 14, 2014 6:09 pm
Re: Detect shift-click in a script button?
Ah apologies, I misunderstood, you have a BTNCS_Execute control doing this? Hmm. Not sure that can be done. I'll have a look...
- Midgardsormr
- Fusionator
- Posts: 2048
- Joined: Wed Nov 26, 2014 8:04 pm
- Location: Los Angeles, CA, USA
- Been thanked: 11 times
- Contact: