Im trying to make a form for a test application. It has a heap of buttons and textboxes etc on it.
I want to capture when the uses presses a random selection of keys (about 6 different keys) and have the form respond depending on what key they pressed, and what control they currently have highlighted.
I don't know if i should be using keypress, keydown or something completely different
Any help appreciated
Cheers
Mc

Capturing Key press or Key Down Events for an entire Form
EmmaTaylor01
Hi,
As per my understanding you want to get the events generated by the controls that are located on you form and more also want to get the focused control.
If so then you can do it simply by doing that…
Set Forms KeyPrview property to true. By doing this any event raised by any control on the form will be first captured by the Form. What you have to do is just capture it on the form by going in form action and double click the event you want to capture for example KeyDown. if you capture this event in control as well the first form event will occur then control event will occur.
For example you have textbox on form then following will be events…
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Captured on form" );
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Captured on form" );
}
There are many way to get the active control. You can do it by setting flag in each controls Enter event or you can loop through the controls to find focused control as given below.
Create following method…
private void RecursivelyGetControl(Control c)
{
for (int x = 0; x < c.Controls.Count; x++)
{
if (c.Controls.Count > 0)
{
RecursivelyFormatForWinXP(c.Controls[x]);
}
}
//Get focused control
if (c.Focused)
{
MessageBox.Show(c.Name );
}
}
Call this method like this where you need to focused control …
for (int x = 0; x < this.Controls.Count; x++)
{
RecursivelyGetControl(this.Parent.Parent.Parent.Controls[x]);
}
Hope this help.
OnCallBI-DBA