[C#] Mouse/Keyboard Hook


Class File: Download

Guide:

• Declare:
UserActivityHook actHook;

• Create An Instance:
actHook = new UserActivityHook();

• Hang on Events:
actHook.OnMouseActivity += new MouseEventHandler(MouseMoved);
actHook.KeyDown += new KeyEventHandler(MyKeyDown);
actHook.KeyPress += new KeyPressEventHandler(MyKeyPress);
actHook.KeyUp += new KeyEventHandler(MyKeyUp);

• Example to Process an Events:
public void MyKeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyData == Keys.Space)
     MessageBox.Show("Spaced");
}
public void MouseMoved(object sender, MouseEventArgs e)
{
   lbl_MousePosition.Text = String.Format("x={0} y={1}", e.X, e.Y);
   if (e.Clicks > 0)
     LogWrite("MouseButton - " + e.Button.ToString());
}
source: Code Project