In a previous thread concerning some inconsistencies in the NETMF docs Foekie and I went off on a tangent about good ways to handle button input events when the Button classes built into WPF are not available or are not what you need.
brett3 suggested starting a new thread to make the information easier to find in the future, which is a really good idea.
My initial response is quoted below and I’ll start my new ramblings in the next post.
[quote]
Generating the event is not too difficult. I set up an ExtendedTimer that calls this code every 100ms
currentKey = FEZ_Shields.KeypadLCD.GetKey();
if (currentKey != FEZ_Shields.KeypadLCD.Keys.None
& lastKey == FEZ_Shields.KeypadLCD.Keys.None)
{
lastKey = currentKey;
}
else if (currentKey == FEZ_Shields.KeypadLCD.Keys.None
& lastKey != FEZ_Shields.KeypadLCD.Keys.None)
{
keyArg.key = lastKey;
lastKey = FEZ_Shields.KeypadLCD.Keys.None;
OnKeyEvent(keyArg);
}
Basically this looks to see if the state of FEZ_Shields.KeypadLCD.GetKey() has changed from returning ‘None’ to returning any key pressed, this signifies a key down and we save the ID of the key that is pressed. If the state of FEZ_Shields.KeypadLCD.GetKey() has changed from a key being down to ‘None’ then we know the key was released and we fire off the OnKeyEvent. Since the update rate of the Extended timer is a relatively slow 100ms it makes a pretty good debounce (glitch) filter for the keys as well.
This set up is nice and simple but only gives you a key up event. If you wanted to emit key down events and key held events it would have to be more elaborate (and the timer would need to be a little faster.)
If you are using GPIO pins that are interrupt capable then the job is much easier as you you have events already generated for both edges of the signal and a built in glitch filter.
What is going through my mind now is a better/smarter way to handle the events once you have them. You don’t always want to do the same thing when a given event fires and we don’t have all the goodies of a larger frame work to help us out.[/quote]