Button spamming/holding button for specific amount of time?

Official forum for open source FreePIE discussion and development.
Post Reply
Canescj
One Eyed Hopeful
Posts: 21
Joined: Thu Oct 19, 2017 6:14 pm

Button spamming/holding button for specific amount of time?

Post by Canescj »

Kinda new to scripting in FreePIE, can't seem to figure out scripts for button spamming/holding button for a specific amount of time. Any help would be appreciated.

Jabberwock
Cross Eyed!
Posts: 160
Joined: Mon Mar 02, 2015 3:58 pm

Re: Button spamming/holding button for specific amount of ti

Post by Jabberwock »

One of the ways to hold buttons is to use time module and then assign the time of the press to a variable with time.clock().

For example this presses the virtual joystick button for three seconds after the key A has been pressed:

Code: Select all

import time

def update():
	global timepress

if starting:
	timepress = 0

if keyboard.getPressed(Key.A):
	timepress = time.clock()
	vJoy[0].setButton(0, 1)

if (timepress > 0 and time.clock() > timepress + 3):
	vJoy[0].setButton(0, 0)
	timepress = 0

Canescj
One Eyed Hopeful
Posts: 21
Joined: Thu Oct 19, 2017 6:14 pm

Re: Button spamming/holding button for specific amount of ti

Post by Canescj »

Thanks, I'm trying to make FreePIE my only scripting program and there's some major differences from GlovePIE. Any ideas to press a button repeatedly when you press another key or button? I'm disabled and QTEs can be almost impossible for me.

Jabberwock
Cross Eyed!
Posts: 160
Joined: Mon Mar 02, 2015 3:58 pm

Re: Button spamming/holding button for specific amount of ti

Post by Jabberwock »

The easiest way would be to alternate the state at each refresh, but it might be too quick for a game to be detected. I guess a timer again could be used - here is a modification of the code above. The button is pressed for half a second and then turned off for 0.1 s as long as key A is pressed, of course for a game the presses probably should be faster. (Another variable is used as vJoy does not allow direct reading. Of course, one could read the state from joystick[0] etc., but it might be confusing.

Code: Select all

import time

def update():
   global timeout
   global button0state
   

if starting:
   timeout = 0
   button0state = 0

if keyboard.getKeyDown(Key.A):
	if button0state == 1:
		if time.clock() > timeout + 0.5:
			timeout = time.clock()
			button0state = 0
			vJoy[0].setButton(0, 0)
	if button0state == 0:
		if time.clock() > timeout + 0.1:
			timeout = time.clock()
			button0state = 1
			vJoy[0].setButton(0, 1)
else:
	if timeout > 0:
		timeout == 0
		button0state = 0
		vJoy[0].setButton(0, 0)

diagnostics.watch(timeout)
diagnostics.watch(button0state)

Canescj
One Eyed Hopeful
Posts: 21
Joined: Thu Oct 19, 2017 6:14 pm

Re: Button spamming/holding button for specific amount of ti

Post by Canescj »

Thanks a million, you have no idea how many games are opened up to me just from those few lines of code. Now I can extrapolate the button spamming into button sequencing

Jabberwock
Cross Eyed!
Posts: 160
Joined: Mon Mar 02, 2015 3:58 pm

Re: Button spamming/holding button for specific amount of ti

Post by Jabberwock »

I thought you might want sequences. I wanted to try that based on the above, with sequences setting particular buttons, but then I have realized it would be much more convenient to have a sequence that would set the whole joystick state at once, so you could have combinations for those hadoukens. So I have written what follows:

Code: Select all

import time

test = [512, 256, 1, 2, 4, 3]
hadouken = [1024, 1024 + 256, 256 + 1]

def joystate(state):
	for i in range(8):
		vJoy[0].setButton(i, (state & (1 << i)))
	
	vJoy[0].x = ((state >> 8) & 1) * 32767 + ((state >> 8) & 2) * -32767
	vJoy[0].y = ((state >> 10) & 1) * 32767 + ((state >> 10) & 2) * -32767
	
def execute(sequence):
	global seqtrigger
	global timeout
	global step
	
	if len(sequence) > 0:
		if time.clock() > timeout + 1:
			if step < len(sequence):
				timeout = time.clock()
				joystate(sequence[step])
				step = step + 1
			else:
				step = 0
	else:
		if timeout > 0:
			timeout == 0
			step = 0
			joystate(0)

if starting:
   timeout = 0
   step = 0
   seqtrigger = 'none'

if keyboard.getKeyDown(Key.A):
	execute(test)
	seqtrigger = Key.A
else:
	if seqtrigger == Key.A:
		execute([])
		seqtrigger = 'none'


if keyboard.getKeyDown(Key.B):
	execute(hadouken)
	seqtrigger = Key.B
else:
	if seqtrigger == Key.B:
		execute([])
		seqtrigger = 'none'

The numbers in sequence specify the joystick state with particular bits. That is, the eight lowest bits specify the buttons (so the sequence number equal 1 sets the first button, the number 3 sets the first and second one etc.). The bits 9 and 10 from the right set the minimum and the maximum of the axis x, while bits 11 and 12 the axis y. So 256 moves x to the maximum while 512 to the minimum. If boths or none bits are set the axis is zero. I hope this is clear, if not, I will try to explain it differently.

In my code the sequence loops, which might not be what you want. To avoid that, just remove the else part which zeroes the step.

Canescj
One Eyed Hopeful
Posts: 21
Joined: Thu Oct 19, 2017 6:14 pm

Re: Button spamming/holding button for specific amount of ti

Post by Canescj »

Dude you are awesome, I was going to just fiddle with the spamming code you gave me lol. Thank you so so much!

Post Reply

Return to “FreePIE”