A speed counter is a button that can be used to increment a value. The speed counter effectively lets the user get precise values while also being able to jump quickly to large numbers. In this case it is only used to increment a value, but a decrement button could be made just as easily. When the user first clicks the button and holds their mouse button down, the value increments slowly, but the longer they hold the button down the faster it increments. In this way they can jump to a high number very quickly just by holding the mouse button down.
The idea of a "speed counter" requires a button to respond immediately when the mouse button is pressed down over a button. With a speed counter, your variable begins slowly incrementing when you press the mouse button and increments faster the longer the button is down. To handle this, an invisible button to track that initial "mouse down" and the form's On Timer event can be used. Invisible button objects trigger the On click form event as soon as the mouse button goes down, while other buttons do not trigger until the button comes up. The form used for this example is shown here:
The variable on the left is a Real, myVar. The arrow on the right is a picture variable with an invisible button overlaying it. The code for how to acheive the "speed counter" behavior is pretty simple.
Here is the code for the invisible button, note that buttonFlag is a process variable used as a flag for when the button is down:
: (Form event=On Clicked )
: (Form event=On Mouse Leave )
In the On Clicked form event the button flag and Form Timer are set. When the user mouses out of the button, then both are turned off, in the On Mouse Leave form event. In the On Mouse Leave form event the count Process variable is also set to zero. We will see from the Form Method that this resets the speed that the count is performed at.
The following code is from the Form Method:
|Case of |
: (Form event=On Load )
: (Form event=On Timer )
If (($MouseState=1) & (buttonFlag))
The On Load form event is basically only there for declaring and initializing variables. The On Timer form even handles the speed counter. GET MOUSE is used to make sure that the user still has the mouse button down and then the buttonFlag variable is used to make sure that the invisible button has been pressed. If either of these are false then the Timer is reset and the flag and count are reset as well. If both are true then count and myVar are incremented. Based on the amount of times count has been incremented (which is effectively how long the mouse button has been down) the timer can be reset to be faster as the button is down longer.