trigg with keypress


Im trying to do something that should be really simple. I want to toggle between on and off (1 and 0) with a keyclick. I know I can use the keyboard in chop to get key inputs. But it only get the value when the key is pressed.

How do I get this simple task to work? I guess I can also do this with a chop execute but I cannot get the script to work.

Would be great with example how to buld this simple toggle both with chops and with script!


Hey Arco,

Here’s a break down of how to solve this problem with a combination of CHOPs and DATs (example1), just CHOPs (example 2), and just DATs (example 3).
base_simple_toggle_example.tox (1.9 KB)

Which approach to use really depends on what you’re doing and what’s most comfortable for you to work with. The one thing to be careful about is the CHOP only approach. Because the Count CHOP always cooks it means that everything down stream will cook as well - all the time. You can work around this by using a null and setting it’s cook type to selective.

Hope that helps.


Thanks! And thanks for all the effort with your great tutorials!

Hi, sorry for reviving a 4 year old post… I’m interested in using the CHOPs + DATs example in your file… but being a total beginner in Python, I can’t seem to change the “CHOP execute” so that instead of changing the text that appears in the “Table”, it changes the Play button in a “moviefilein”…

I tried changing op(‘table1’) to op(‘moviefilein1’)
and then changing “Toggle On” and “Toggle Off” to “1” and “0” or “on” and “off”

but nothing seems to work… if anybody can help it would be greatly appreciated.

Try the following expression.

	if state: op("moviefilein1") = not op("moviefilein1")

First, we are checking if the button gets pressed (state is True when the key goes down and False when going up).
If this is the case, we simply inverse the value of the parameter using the not keyword.
So if the value is True, it will go to false, if it is false, it will go to True.

Hi @alphamoonbase , thanks for trying to help.
I’m not sure where in the “chop execute” I should add this code…

Right now the code looks like this:

def onOffToOn(channel, sampleIndex, val, prev):
	target 		= op('moviefilein1')

	if target[0,0].val 		== "True":
		target[0,0] 		= "False"

		target[0,0] 		= "True"


def whileOn(channel, sampleIndex, val, prev):

def onOnToOff(channel, sampleIndex, val, prev):

def whileOff(channel, sampleIndex, val, prev):

def onValueChange(channel, sampleIndex, val, prev):

And when I run it, it doesn’t work, and the error says

Error: Traceback (most recent call last):
File “/project1/SCENES/scene1/chopexec1”, line 16, in onOffToOn
TypeError: ‘td.Par’ object is not subscriptable

Hope that sounds comprehensible to you… :slight_smile:
Thank you

Can you try to explain to me what exactly you are trying to do with your code?
So target is your parameter that you want to toggle on or off.
What are you aiming to do with the [0,0] in this case?

I see two solutions here, and I think they go in tandem:

  1. Take some time to dive into the basics of python in touchdesigner to understand how to acces and change a parameter, how to use callbacks etc. Check and the introduction to python.
    Introduction to Python Tutorial - Derivative
    Python Tips - Derivative

  2. Use only CHOPs to handle everything.
    Use a keyboardInCHOP. Then, use a selectCHOP to select the channel corrosponding to your key you want to press.
    Then download my ToggleCHOP, place it in your project, feed the output of the selectCHOP into the toggleCHOP and the output of the toggleCHOP into a nullCHOP. You can now export the channel onto your parameter.
    OLIB - Toggle Chop

Also, you can delete all the other functions (whileOn, onOnToOff, etc) so it is easier to read.

Hi alphamoonbase,

I was trying to mess with the code made by raganmd (above) to be able to trigger the “play” button of a “moviefilein” with keyboard (later I will do it with a midi controller)… but couldn’t figure out the python… You’re right, I should dive into the introduction to Python, I will. I usually prefer trying to figure out specific problems and learning along the way (it worked out well for some html and php back in the day)… but maybe the learning curve to python is too steep for that approach.

Anyway, I tried your OLIB - Toggle Chop, and it is working perfectly for what I need! Thank you!
I will take look inside it to try to understand what is going on.

It’s a bit frustrating, because I need to build a whole live system that allows me to switch from one project/tox to another easily (not always in the same order), and then allow me to interact with that tox before going to another… And I need to make sure I can manage to create this system before even starting having fun creating visuals…

Anyway, thanks again for your help, and sorry for the noob questions.

No worries.
I asked you to explain to me what the code should do so you can better understand what is going on exactly. At least for me it helps alot explaining (and if it is to myself) step by step what every line of code is supposed to do.

Regarding your system: Check out the SceneChanger!

I understand, thanks!

Yes, yes, I’m working with the SceneChanger, but I’m trying to be able to interact with it with my keyboard (and later midi controller), hence all the questions to try to program what each key does!
I’m getting there…

Have a nice one.

One very easy solution to obtain a toggle with keyboard is to use a count Chop with:

  • Limit : Loop Min/Max
  • Limit Minimum = 0
  • Limit Maximum = 1
    I hope its whats you need.

The Logic CHOP has a Channel Pre Op menu that you can set to Toggle, which flips the channel off-on-off-on each time it gets a pulse on the input… see Snippet

Hi jacqueshoepffner and greg,

Thank you for your input, I’ve found a solution with alphamoonbase’s method, but I’ll look into your suggestions to see if it could also work out (and also help me explore different possibilities, and learn)!


Hi @jacqueshoepffner and @greg ,

I tried both of your solutions, and they do work just as claimed. However, it seems that when I use a Count Chop or a Logic Chop, the “dotted line” connected to the Top with the toggle (moviefilein) seems to be always cooking.
When I use the Toggle Chop created by @alphamoonbase , that same line is only cooking when I press the key.

Being still at the beginning of this TouchDesigner journey, I haven’t experienced yet just how important managing things cooking or not is. But from everything I read on those forums, it seems to be a pretty big deal once a network becomes complex.

So I guess my question is an “abstract” one, but which method do you all advise me to use ? Isn’t alphamoonbase’s method better, since nothing is continually cooking ?

Thanks again for all your help!

dont be affraid bby the dotted line, its not this cook who can slow your process! If you are disturbed by it, you can add a null Chop after the count Chop with cook type to “selective”

Thanks Jacques.
I imagine one cooking Count wouldn’t be a problem, I’m just trying to think ahead of my project which will have about 15 to 20 different .tox/projetcs inside of it, all with different interactions.

So I try to optimize as much as I can (and understand) from the start.