According to the official documentation, the KeyDown event on a Windows Forms control occurs only once, but it is easy to demonstrate that the event fires continually aslong as a key is held down:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
label1.Text = string.Format("{0}", globalCounter++);
}
How can you consume the event so that it fires only once?
I’m generally a VB guy, but this seems to work for me as demo code, using the form itself as the input source:
I think the logic gets a little sketchy if you’re holding down multiple keys at a time, but that seems to only fire the event from the last key that was pressed anyway, so I don’t think it becomes an issue.
I tested this in a TextBox in VB, and it worked fine. Wasn’t sure on the inheritance conventions I should follow in c#, so I left it as a straight Form for this answer.
Apologies for any gross code formatting errors, again, this isn’t my usual language.