Page 1 of 1

Detect shift-click in a script button?

Posted: Thu May 16, 2019 12:00 pm
by Midgardsormr

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.

Re: Detect shift-click in a script button?

Posted: Thu May 16, 2019 6:19 pm
by PeterLoveday

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:

  1. local ui = fu.UIManager
  2. local disp = bmd.UIDispatcher(ui)
  4. win = disp:AddWindow({
  5.     ID = 'Win',
  6.     Geometry = { 0, 0, 200, 50 },
  7.     WindowTitle = 'KeyEvents',
  8.     Events = { Close = true, KeyPress = true, KeyRelease = true, },
  9.     ui:Button{ID = "Button", Text = "Button",},
  10. })
  12. itm = win:GetItems()
  14. function win.On.Win.Close(ev)
  15.     disp:ExitLoop()
  16. end
  18. -- A flag to track shift state
  19. local shiftdown = false
  21. -- If the shift key is pressed, set our flag
  22. function win.On.Win.KeyPress(ev)
  23.     if ev.Key == 0x1000020 then
  24.         shiftdown = true
  25.         itm.Button.Text = "Shift+Button"
  26.     end
  27. end
  29. -- If the shift key is released, reset our flag
  30. function win.On.Win.KeyRelease(ev)
  31.     if ev.Key == 0x1000020 then
  32.         shiftdown = false
  33.         itm.Button.Text = "Button"
  34.     end
  35. end
  37. -- Now we can use our flag to differentiate button presses
  38. function win.On.Button.Clicked(ev)
  39.     if shiftdown then
  40.         print("Shift+Button")
  41.     else
  42.         print("Button")
  43.     end
  44. end
  46. win:Show()
  47. disp:RunLoop()
  48. win:Hide()

Re: Detect shift-click in a script button?

Posted: Thu May 16, 2019 6:34 pm
by Midgardsormr

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.

Re: Detect shift-click in a script button?

Posted: Thu May 16, 2019 6:43 pm
by PeterLoveday

Ah apologies, I misunderstood, you have a BTNCS_Execute control doing this? Hmm. Not sure that can be done. I'll have a look...

Re: Detect shift-click in a script button?

Posted: Fri May 17, 2019 2:43 pm
by Midgardsormr

Alas, it looked like it would work, but it didn't.