Sure:
if keystate(key) > 0 and hold = 0
hold = 1
keystate(key) detects if the key is pressed.
key is a number that represents a key from your keyboard. (you can search the forums for a map of the numbers).
and hold = 0 is used to prevent that the toggle goes very fast. If you press the key, and it all repeats (when looping) then that key would be still pressed (almost nobody can press a key for 1 loop), and the variable will be swapped again almost directly.
hold = 1 is used for this above.
If you add this, you will press a key, and hold = 1. But the next loop, hold = 1, andthus the program will not swap the variable again.
if TheThingThatHasToBeHandled = 0
TheThingThatHasToBeHandled = 1
else
TheThingThatHasToBeHandled = 0
endif
There's nothing much to say about this. If the variable = 0, then make it 1, else (if the variable = 1) then reset it to 0.
if scancode() = 0 then hold = 0
Ofcourse, if we put a "lock" (hold) on the key, we have to be able to unlock it.
scancode() returns the key number of any key pressed. (Kinda like the opposite of keystate(key)), so if no key is pressed, unlock the key for another toggle.
if TheThingThatHasToBeHandled = 1
HandleThing()
endif
This if statements checks if the variable is toggled on (1), and if so, then it has to do something. HandleThing() is not a command or anything, it's just to show that that's the part that you have to code the action.
An example:
sync on : sync rate 100
`Reset the toggle variable
toggle = 0
do
`If the key with number 32 is pressed (the letter q). Toggle
if keystate(16) > 0 and hold = 0
hold = 1
if toggle = 0 then toggle = 1 else toggle = 0
endif
if scancode() = 0 then hold = 0
`Print the toggle value
cls
print toggle
sync
loop
And this is what happens when you leave out the "hold" part:
sync on : sync rate 100
`Reset the toggle variable
toggle = 0
do
`If the key with number 32 is pressed (the letter q). Toggle
if keystate(16) > 0
if toggle = 0 then toggle = 1 else toggle = 0
endif
`Print the toggle value
cls
print toggle
sync
loop
I hope it's a bit clearer now.
It's the programmer's life:
Have a problem, solve the problem, and have a new problem to solve.