Edge Detection
Most of the time, you don’t need to know whether a switch is on or off so much as you need to know when it changed. You want to know when it turned on or when it turned off. In other words, you want to find the edge of the transition from on to off, or vice versa. This is often called edge detection.
The basic idea is that you check not only what the state of the switch is, but what the state was the last time you checked, and compare the two. Here’s an example in PicBasic Pro: