Page 1 of 1

vJoy toggle MIDI button

Posted: Tue Sep 01, 2020 11:18 pm
by RogerR
Hello, I have a MIDI controller with several toggle CC buttons (0 = off; 127 = on) and I would like my vJoy button to be pressed but not held like an on/off switch when vJoy get 127/0 respectively.

What I figured out so far is that this code will make the vJoy button to be held:

Code: Select all

if (midi[0].data.buffer[0] == 4) and (midi[0].data.buffer[1] == 127): 
	vJoy[0].setButton(4,True)
else:
	vJoy[0].setButton(4,False)
How can I make the vJoy button to be pressed momentarily when values 127 and 0 are received?

Thanks in advance!

Re: vJoy toggle MIDI button

Posted: Wed Sep 02, 2020 5:39 am
by Jabberwock
Try this:

Code: Select all

def update():
	
	global toggle

	if (midi[0].data.buffer[0] == 4) and (midi[0].data.buffer[1] == 127):
		toggle = 1

if starting:
	midi[0].update += update
	
	toggle = 0
	

if (toggle == 1):
	vJoy[0].setButton(4,True)
	toggle = 0
else
	vJoy[0].setButton(4,False)

Note that this makes the button press rather short (one frame), which might not be sufficient for all applications. You can solve it using timers or just make the toggle value higher and decrease it each frame until it is zero.

Re: vJoy toggle MIDI button

Posted: Wed Sep 02, 2020 9:45 am
by RogerR
Jabberwock wrote: Wed Sep 02, 2020 5:39 am Try this:

Code: Select all

def update():
	
	global toggle

	if (midi[0].data.buffer[0] == 4) and (midi[0].data.buffer[1] == 127):
		toggle = 1

if starting:
	midi[0].update += update
	
	toggle = 0
	

if (toggle == 1):
	vJoy[0].setButton(4,True)
	toggle == 0
else
	vJoy[0].setButton(4,False)

Note that this makes the button press rather short (one frame), which might not be sufficient for all applications. You can solve it using timers or just make the toggle value higher and decrease it each frame until it is zero.
Thank you Jabberwock but unfortunately that didn't work and made the button stuck, I can't turn it off.

Re: vJoy toggle MIDI button

Posted: Wed Sep 02, 2020 12:33 pm
by RogerR
I've changed setButton to setPressed and now I can get a short button press as desired but it only works for value 127 and just the first time, I can't make it activate again. Any ideas?

Re: vJoy toggle MIDI button

Posted: Thu Sep 03, 2020 2:36 am
by Jabberwock
Sorry, the code has an error: it should be toggle = 0 and not toggle == 0 in the conditional.

As for setPressed - try to debug the MIDI values and see how they change, to make sure that they are not stuck.