-3
\$\begingroup\$

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!

\$\endgroup\$
5
  • \$\begingroup\$ A code snippet to show what you're currently doing would probably be useful. \$\endgroup\$
    – Tetrad
    Commented Feb 8, 2013 at 2:42
  • \$\begingroup\$ Tag java as well \$\endgroup\$
    – Savlon
    Commented Feb 8, 2013 at 3:30
  • \$\begingroup\$ Consider putting your solution in an answer, and mark it as the solution, so people can find this problem better. \$\endgroup\$
    – Kirbinator
    Commented Feb 8, 2013 at 4:06
  • \$\begingroup\$ 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. \$\endgroup\$
    – House
    Commented Feb 8, 2013 at 4:07
  • \$\begingroup\$ Thank you, I didn't realize I could answer it myself! \$\endgroup\$
    – Kwauhn
    Commented Feb 8, 2013 at 4:38

1 Answer 1

1
\$\begingroup\$

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;
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ 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)) \$\endgroup\$
    – Kikaimaru
    Commented Feb 8, 2013 at 13:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .