From iLab Cookbook

Toolkits: SimpleHookerApplication

<< Back to the Windows Hooker page

Tutorial - Simple Hooker Application

In this tutorial we will develop our Windows Hook enabled application using Microsoft Visual Studio 2005. Before starting you will need to make sure that both Visual Studio 2005 and Windows Hooker have been installed.

In this tutorial you will learn how to

While we recommend you create this program from scratch, we also provide executables and source that you can download and try.

Download: WH-SimpleApplication.zip
Unzip all files (executables plus dependancies) into a single folder.
The executable should run just by clicking the .exe

1) Before starting (and if you have not already done so) make sure that you have registered the Windows Hooker Dll.

2) Start Microsoft Visual Studio 2005 and create a new C# project called SimpleHook

3) Right Click on the References folder in the solution window and select Add Reference. From the dialog box that appears, select the COM tab, and select the item: WindowsHooker 1.0 Type Library

4) Now Add two labels and two text boxes to your form. Rename the label text to be 'Keyboard' and 'Mouse' also note that TextBox1 is the mouse and TextBox2 is the keyboard

5) Now right click on the form and select "View Code" to open up the code view for the form. We are now ready to start coding

6) At the top of the form add the following namespace


  using WINDOWSHOOKERLib;
 

7) Inside the Form1 class add the following variable and code into the constructor, this will enable logging of the mouse and keyboard and will provide event handlers that we can use in our program. Note that the FormClosing event handler must be added to ensure that the hookmanager is properly closed.


  private HookManager hm;
  public Form1()
  {
      InitializeComponent();
      hm = new HookManager();
      hm.LogKeyboard = true;
      hm.LogMouse = true;
      hm.MouseDown += new _IHookManagerEvents_MouseDownEventHandler(hm_MouseDown);
      hm.KeyDown += new _IHookManagerEvents_KeyDownEventHandler(hm_KeyDown);
      this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
      hm.StartWindowsHook();
  }
 

8) Now we need to add that event code, we will make it show text in the title bar whenever a mouse button is pressed or a key is pressed. Also, when the form closes we will explicitly stop the windows hook (this is very important, your computer will crash if this is not present).


 void Form1_FormClosing(object sender, FormClosingEventArgs e){
     hm.StopWindowsHook();
 }
 void hm_MouseDown(int x, int y, int hWnd, int MouseMessage, int HitTestCode, int dwExtraInfo) {
     textBox1.Text = "Mouse Pressed: x " + x + " y " + y;
 }
 void hm_KeyDown(int VirtualKeyCode, int Scancode, int RepeatCount, bool KeyPressed, bool PreviousKeyWasDown, bool AltDown, bool ExtendedKey) {
     textBox2.Text += ((Keys)VirtualKeyCode).ToString();
 }
 

9) Press F5 to run your application. The result should look like below.

10) Important Note you must not break or place break points in your application, only press Alt-F4 to exist your application. Breaking will cause your computer to stop responding. If the mouse hook is stalled at a break point you will not be able to do any mouse commands. This is because the Windows Hook attaches to the end of every mouse event, it is also advisable not to put too much code in the Windows Hook call as this can really slow down your computer.

Retrieved from http://grouplab.cpsc.ucalgary.ca/cookbook/index.php/Toolkits/SimpleHookerApplication
Page last modified on February 09, 2007, at 02:13 PM