If you're using Windows forms .NET, add the following to your main application form:
protected override void OnControlAdded(ControlEventArgs e)
{
e.Control.Click += new EventHandler(Control_Click);
e.Control.KeyDown += new KeyEventHandler(Control_KeyDown);
base.OnControlAdded(e);
}
void Control_KeyDown(object sender, KeyEventArgs e)
{
// Hande all keydown events, sender is the control
Debug.WriteLine( sender.ToString() + " - KeyDown");
}
void Control_Click(object sender, EventArgs e)
{
// Hande all click events, sender is the control
Debug.WriteLine(sender.ToString() + " - Click");
}
protected override void OnControlRemoved(ControlEventArgs e)
{
e.Control.KeyDown -= Control_KeyDown;
e.Control.Click -= Control_Click;
base.OnControlRemoved(e);
}
Just add any extra events you require (such as KeyPress, MouseEnter, MouseDown etc) in a similar manner. This is a bit cleaner and simpler then delving into the Windows API.