# How to toggle something using key releases

I'm making an application using lwjgl and I'm having troubles with toggling using the same key. What I want is for the boolean to be toggled when I release the key. I have a method that returns the toggle state and checks for any changes at the same time, but this is proving kind of difficult and overly complex. Is there any way I can make this easier? If not, could someone tell me how to go about toggling with the same key?

Any help is appreciated, thanks in advance!

• A code snippet to show what you're currently doing would probably be useful. – Tetrad Feb 8 '13 at 2:42
• Tag java as well – Savlon Feb 8 '13 at 3:30
• Consider putting your solution in an answer, and mark it as the solution, so people can find this problem better. – Kirbinator Feb 8 '13 at 4:06
• I've rolled back the changes so you can answer your own question. The question area is for questions, use the answer area to answer. – MichaelHouse Feb 8 '13 at 4:07
• Thank you, I didn't realize I could answer it myself! – Kwauhn Feb 8 '13 at 4:38

I figured it out myself, here's the code:

public class Input{
private static boolean escDownOnLast;
private static boolean escToggled;

public static boolean isToggledOn(){
if(Keyboard.isKeyDown(Keyboard.KEY_R)){
escDownOnLast = true;
}else if(!Keyboard.isKeyDown(Keyboard.KEY_R) && escDownOnLast && !escToggled){
escDownOnLast = false;
escToggled = true;
return escToggled;
}else if(!Keyboard.isKeyDown(Keyboard.KEY_R) && escDownOnLast && escToggled){
escDownOnLast = false;
escToggled = false;
return escToggled;
}
return escToggled;
}
}

• You should probably store last state of keyboard (from previous frame), and current state of keyboard and then compare. You are probably doing that with this piece of code, but its more complex than it has to be it should be something like if (lastState.isKeyDown(key) && currentState.isKeyUp(key)) – Kikaimaru Feb 8 '13 at 13:46