How to determine button states?
-
If we do
Loop C = controls (0) If c.b then // this triggers once per millisecond endif Repeat
Is there a way to just know when the button is down or up. It currently acts like "autofire" like if you wanted to shoot a red laser once on press and then shoot a single blue laser on button up? Kind of used of having key.down key.up in my previous language.
Is there a simple solution to accomplish this with Boolean flags?
-
Wait for the up afterwards.
Or save the previous state of the button, so you only act on the "down" if the previous state was "up".
-
@Nisse5 I have spent an hour or so trying to do that. If there is a simple solution I am not thinking clearly enough today to find it.
-
The code below should perform a logical check of 3 button "states" and then perform the appropriate action per state, hope this helps!
buttonState = 0 loop c = controls(0) if c.b == 1 and buttonState != 2 then buttonState = 1 endif if buttonState == 1 then ink(red) print("red!") buttonState = 2 endif if buttonState == 2 and c.b == 0 then ink(blue) print("blue") buttonState = 0 endif update() repeat
Update: Corrected syntax and print debug test output, works well now!
-
@Tratax don't forget to check for double equals!
= isn't the same as ==
if c = 1 then // <- this will always return true!
-
@sys64738 To wait for the up afterwards:
while controls(0).a loop repeat
Or, this version would do the same, but would probably save much more battery:
while controls(0).a loop update() repeat
To track the previous state, something like:
prevstate = true // To make sure that an up exist before the first down loop c = controls(0) if c.a and !prevstate then // Do something endif prevstate = c.a update() repeat
-
Have made a new project and tried tratax suggestion with == and c = controls(0) sorted
It triggers red laser every loop while button is held down. Instead of once as needed.
Does while take all the programs focus. In my intended program it needs to still run the main loop at 60 fps
-
@sys64738 said in How to determine button states?:
Does while take all the programs focus. In my intended program it needs to still run the main loop at 60 fps
Is there any reason why you don't check the controls in the main loop? To me, streamlining control states + drawing + frame updates in the main loop seems like the most efficient use of FUZE.
-
Nisse5 your prevstate example fires a single red laser per press as requested. But how do I tell when button is released
-
@Nisse5 I am checking in main loop
-
Adding this to your code seems to have done it
Thank you tratax and nisse5
If c.a == 0 and prevstate == 1 then //blue laser End if Prevstate = c.a Update() Repeat
-
@MikeDX Thanks Mike, this was written on the train without my switch handy.. no excuse for leaving it at home I guess haha
-
Have rewritten the above suggestion for anyone else who needs this solution
-
I tackle this differently (there's always more than one way to do stuff)
I maintain a 'debounce' struct. In the main loop I look for c.a == 1 and increment debounce.a. In the else (ie, the button is no longer pressed) I say if debounce.a > 0 then do whatever it was I wanted to do and also reset debounce.a to 0 ready for detecting the next keypress.
For me this solves the problem of only doing something once per press. If instead of performing an action on keyup you really do want keydown then you can say if debounce.a == 1 (or 2 or 3 or 10 or whatever you want). If you want auto repeat you can use debounce.a % 50 or something like that.