Detect shift-click in a script button?

User avatar
Midgardsormr
Fusionator
Posts: 1061
Joined: Wed Nov 26, 2014 8:04 pm
Answers: 1
Location: Los Angeles, CA, USA
Been thanked: 63 times
Contact:

Detect shift-click in a script button?

#1

Post by Midgardsormr » Thu May 16, 2019 12:00 pm

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.


User avatar
PeterLoveday
Fusioneer
Posts: 145
Joined: Sun Sep 14, 2014 6:09 pm
Answers: 6
Been thanked: 16 times

Re: Detect shift-click in a script button?

#2

Post by PeterLoveday » Thu May 16, 2019 6:19 pm

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)
  3.  
  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. })
  11.  
  12. itm = win:GetItems()
  13.  
  14. function win.On.Win.Close(ev)
  15.     disp:ExitLoop()
  16. end
  17.  
  18. -- A flag to track shift state
  19. local shiftdown = false
  20.  
  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
  28.  
  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
  36.  
  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
  45.  
  46. win:Show()
  47. disp:RunLoop()
  48. win:Hide()

User avatar
Midgardsormr
Fusionator
Posts: 1061
Joined: Wed Nov 26, 2014 8:04 pm
Answers: 1
Location: Los Angeles, CA, USA
Been thanked: 63 times
Contact:

Re: Detect shift-click in a script button?

#3

Post by Midgardsormr » Thu May 16, 2019 6:34 pm

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.


User avatar
PeterLoveday
Fusioneer
Posts: 145
Joined: Sun Sep 14, 2014 6:09 pm
Answers: 6
Been thanked: 16 times

Re: Detect shift-click in a script button?

#4

Post by PeterLoveday » Thu May 16, 2019 6:43 pm

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


User avatar
Midgardsormr
Fusionator
Posts: 1061
Joined: Wed Nov 26, 2014 8:04 pm
Answers: 1
Location: Los Angeles, CA, USA
Been thanked: 63 times
Contact:

Re: Detect shift-click in a script button?

#5

Post by Midgardsormr » Fri May 17, 2019 2:43 pm

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