Developer Documentation

Introduction

The purpose of this documentation is to demonstrate to a .Net programmer how to use the classes and methods contained within the Mouse and Keyboard Hooks library. In Version 3, the process is very simple, straightforward, and easy to implement.

Before beginning it is important to know that hooking terms can and will be used interchangeably. Global hooks are also known as System, or System-Wide hooks, and will encompass the entire operating system. Application hooks are often called Local, because as the name implies, will only correspond to the single application which placed the hook (or a specific thread).

There are essentially two different techniques for implementing the input hook.

Using the MouseKeyEventProvider Component

One of the most unique features of the UserActivityMonitor is the form component. Thru it, events can easily be set, changed, removed, or the component disabled/enabled, etc. To add the component, do the following.
  1. Copy the MouseKeyboardActivityMonitor.dll to your project folder.
  2. In your Windows Forms project, view the Form Designer.
  3. Right click in the Toolbox and select Choose Items.
  4. Click Browse, navigate to your project folder and select the MouseKeyboardActivityMonitor DLL.
  5. Ensure that MouseKeyEventProvider is checked, and press OK.
  6. Locate MouseKeyEventProvider in the Toolbox and double-click it to add it to the form.

The MouseKeyEventProvider only has a few properties, including Name, Enabled, and HookType. HookType specifies which should be monitored… application or global. The component is disabled by default.

To add a handler, view the Events list in the properties of your MouseKeyEventProvider. From there, event handlers can be generated.

Pure Programmatic

For better understanding of how to use the library using code only, we recommend downloading the source code and looking at the demo project. It showcases some of the abilities of the extended events, and demonstrates the abilities of the library more thoroughly.

Below is a very simple code example of how to log any mouse button down and the System Ticks (number of milliseconds since the operating system was started) to the system console.

// First, a MouseHookListener object must exist in the class
private MouseHookListener m_mouseListener;


// Subroutine for activating the hook
public void Activate()
{
    // Note: for an application hook, use the AppHooker class instead
    m_mouseListener = new MouseHookListener(new GlobalHooker());

    // The listener is not enabled by default
    m_mouseListener.Enabled = true;

    // Set the event handler
    // recommended to use the Extended handlers, which allow input suppression among other additional information
    m_mouseListener.MouseDownExt += MouseListener_MouseDownExt;
}

public void Deactivate()
{
    m_mouseListener.Dispose();
}

private void MouseListener_MouseDownExt(object sender, MouseEventExtArgs e)
{
    // log the mouse click
    Console.WriteLine(string.Format("MouseDown: \t{0}; \t System Timestamp: \t{1}", e.Button, e.Timestamp));

    // uncommenting the following line with suppress a middle mouse button click
    // if (e.Buttons == MouseButtons.Middle) { e.Handled = true; }
}

FAQ

Q: I need to suppress mouse input or key input. Is that possible?

A: For Key input, simply set the KeyEventArgs, KeyPressEventArgs, or KeyEventExtArgs handled property to true in your event handler.
For mouse input, it's only slightly more difficult. You must use an Extended Mouse Event, such as MouseDownExt. This is because the standard MouseEventArgs class provided with Windows Forms does not have a Handled property. The MouseEventExtArgs used by the aforementioned events does however, and you simply set the Handled property to True.

Q: I'm using Windows Vista/7 64-bit, and when I build the library the AppHook Xbuttons and scroll wheel don't work.

A: Check which architecture the project is set to. If it is "Any Cpu", try changing it to x64.

Q: How can I have the UserActivityMonitor run as a service?

A: Figure it out yourself. This project will not support a background service that monitors user input (aka, keylogger).

Internal Documentation

MouseStruct

Developer documentation for version 2 can be found at http://www.codeproject.com/KB/cs/globalhook.aspx

Last edited Oct 9, 2011 at 12:04 PM by PyrrhicVictor, version 14