views:

30

answers:

3

hi

How can i capture all user raised events in my application, is there any specific event which is parrent for all of user based(raised) events ?

eg :

mouse click, key press, etc., these all events are raised by user can i able to capture it under one method???

+1  A: 

Use the SetWindowsHookEx API to set up a thread-local WH_CALLWNDPROC hook. See the MSDN documentation for full details.

That will call a single function in your application for every message you receive from Windows.

RichieHindle
A: 

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.

Ash
+1  A: 

In one single method: yes.

You have to implement the DefWindowProc function, which handles all events and messages. Unfortunately, not just user generated events but all. But you should be able to filter those out easily.

Stefan