How do I toggle a widget button using a key on my keyboard as shortcut?

Hi all,
I am trying to get a widget button to toggle when pressing a key on my laptop keyboard.

What I am trying to achieve is to have a toggle button in my UI (let’s say Play/Pause for example), which I can also toggle via keyboard shortcut (i.e. by pressing shift+2)

Normally I would use a Button comp in my UI, and have a script in a keyboardinDAT callsback with a op(‘button1’).click() somewhere in my script.
I have tried with the button Comp and it works, but having trouble to get my head around the widget logic, as op(‘widget1’).click() does not appear to achieve the outcome I would expect.
I have looked up the wiki documentation and found something about click() but couldn’t get it to work.

How do I toggle a widget button using a key on my keyboard as shortcut?

Thanks in advance!

Instead of using a click kind of methodology, instead target the custom parameter on the widget.

For example, the momentary button widget has a parameter Value0.
A little python logic would let you change that value of that button to 0 if it’s 1 or 1 if it’s 0. Something like:

myVal = op('myWidget').par.Value0.val
if myVal == 0:
    op('myWidget').par.Value0 = 1
else:
    op('myWidget').par.Value0 = 0

Does that make sense?

Thanks Matthew!
it does make sense, and I follow the logic, thank you for taking the time to look into this and explaining.

However, I’ve just tried, it seems that I might be on the right path, but not quite yet.
It works well on a momentary button widget (in my case a Reset button), but it appears not to be working on a toggle button widget (in my case to toggle Play/Pause on a audiofilein CHOP).

When I say it doesn’t work I mean that the toggle button still behaves like a momentary button!

I have attached a simplified version of the file I am working on.
shift + 1 (aka !) is the keyboard shortcut for reset
shift + 2 (aka @) is the keyboard shortcut to toggle between play and pause

Widget toggle.toe (22.4 KB)

Fausto

I think I have found a workaround.
I am using a keyboardin CHOP instead, added a count CHOP for the toggle and a couple of CHOP execute to deal with the widgets values.

File attached for whoever else might need this.

Thanks Matthew, you’ve put me on the right track!

Widget toggle.toe (22.1 KB)

Hey @FaustoB - in your first example you’re very close. The condition to be aware of is state is passed along with your key press. When pressing down state goes to 1 and when you release state goes to 0.

You just need an additional conditional statement:

myVal = op('myWidget').par.Value0.val

if state:
    if myVal == 0:
        op('myWidget').par.Value0 = 1
    else:
        op('myWidget').par.Value0 = 0
1 Like

Hey Matthew,
thank you again for your help and for being so generous with your time and knowledge.

I have just tried my first example using the if state: python code you suggested, and indeed it worked straight away.

I had no doubts about it, however I cannot say that I understand the logic behind using state.
Would you be ok to explain or provide a documentation link for me to look up?

in particular, what I don’t understand is:

what is the reason for checking the state in a widget, and what is it doing?
I have tried to put down a Panel Chop to reverse engineer your solution, but I cannot see any state when connected to the widget…
I know that state is part of the Button COMP (state, rollover, select), but I cannot see or find these in the widget.

No problem if you are busy, your code works and I’m very happy, I’m just trying to understand the logic.
“give a man a fish…” :slight_smile:

Thanks again for your help!

Fausto

hey @FaustoB - I’d be happy to give you a quick example.

So state in this case is actually coming from the keyboardin DAT. State is actually the state of the keyboard button. The logic here is that you not only want to know when you’ve pressed a key down, you want to know when you’ve let go of the same key. From an interaction standpoint, it’s a question of firing a script when you’ve pressed a button, or released it.

Here’s a quick example that does a few things:

  • First we check to see if the key matches
  • Next we print the current state. We should expect to see True when we press the key and False when we release the key.
  • Next we use some of the same logic so if state is true, then we print a little helper to let us know that we’ve pressed the key down, and if state is false we give ourselves another helper message to know that we’ve let go of the key

keyboardStateExample.toe (4.0 KB)

Okay - so how does that relate to your first question. So we want to run a script to change a value in your UI based on pressing the key on the keyboard down. To make that work we need to use the state from your keyboard in DAT to know when we’ve pressed the key, then do the next pieces of logic to toggle the parameter.

Without checking for state, you run the script both when you press the key down, and when you release it. You can see this working in your first example by holding down the @ key after you press it. The script toggles on as you press down, and off as you release the key. In your particular use case that means we need an extra logical step, but in other cases this is exactly that kind of behavior we might want - say a virtual button that stays “on” as long as we’re pressing a physical one, and changes to “off” when we release it.

Hope that helps - I’m happy to give you another example if this doesn’t make sense.

Cheers :slight_smile:

Oh wow thank you Matthew!
It’s so clear that it hurts :slight_smile:
I get it, and it totally makes sense now that you explained it.
Thank you again!

1 Like

glad it helped :slight_smile:

Please, how do I set a button in perform mode, to press key “1” on my keyboard and how do I set a button to Play/Pause perform mode playback? Thanks