View Single Post
  #5   Spotlight this post!  
Unread 23-01-2013, 22:19
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,089
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Toggling Buttons in Java


Two different ways to turn a momentary-contact button into a toggle:

Code:
if ( buttonIsPressed() )              // test button state
    {
    if (!buttonWasPressed) toggle();  // toggle if state changed from "not pressed" to "pressed"
    buttonWasPressed=1;               // remember button state for next iteration
    }
else buttonWasPressed=0;              // remember button state for next iteration

I like this one better:

Code:
IsPressed = buttonIsPressed();          // get new button state

if (IsPressed && !WasPressed) toggle(); // toggle if state changed from "not pressed" to "pressed"

WasPressed=IsPressed;                   // remember button state for next iteration
Reply With Quote