Windows Hook Library

Published Date: 2/28/2010

The WindowsHookLib is a single library to hook the mouse, keyboard and the clipboard system wide. WindowsHookLib library is written in C# and there for it uses Common Language Runtime (CLR). This means that the library can be referenced from various projects in Dot Net.
The mouse and keyboard hooks are low level so you can use the Handled property of the MouseEventArgs or the KeyboardEventArgs to prevent the windows messages be passed to the other applications. Note you need to use the dll file not the classes in your projects; otherwise they might not work correctly.

MOUSE HOOK

    The MouseHook class is designed to hook the mouse globally and fire some useful events. Since this hook is low level and low level mouse hook doesn't get MouseClick and MouseDoubleClick messages it simulates these events. The Handled property is provided in MouseDown, MouseUp, MouseWheel and MouseMove event handlers. If you decide to set the Handled property in the MouseUp event then it is recommended to set it in the MouseDown event as well for the application performance.

    Warning! If you set the Handled property in the mentioned events unconditionally than you might not be able to use the mouse. You can check the demo project examples to see how you can condition the mouse handled process.

    METHODS:

    • InstallHook – Installs the mouse hook globally (has no parameters).
    • RemoveHook – Removes the mouse hook (has no parameters).
    • Dispose – Unhooks the mouse (if it is hooked) and frees the memory used by the component (has no parameters).
    • SynthesizeMouseDown - Synthesizes a mouse down event system wide.
    • SynthesizeMouseUp - Synthesizes a mouse up event system wide.
    • SynthesizeMouseWheel - Synthesizes a mouse wheel event system wide.
    • SynthesizeMouseMove - Synthesizes a mouse move event system wide.

    PROPERTIES:

    • State – Gets the hook state.

    EVENTS:

    • StateChanged – Fires the event if the mouse hook is installed/uninstalled.
    • MouseDown – Fires the event if the mouse button is down.
    • MouseMove – Fires the event if the mouse is moved.
    • MouseUp – Fires the event if the mouse button is up.
    • MouseWheel – Fires the event if the mouse vertical/horizontal wheels are rotated.
    • MouseClick – Fires the event if the mouse is clicked.
    • MouseDoubleClick – Fires the event if the mouse is double-clicked.

KEYBOARD HOOK

    The KeyboardHook class can be use to hook the keyboard globally.

    METHODS:

    • InstallHook – Installs the keyboard hook globally (has no parameters).
    • RemoveHook – Removes the keyboard hook (has no parameters).
    • Dispose – Unhooks the keyboard (if it is hooked) and frees the memory used by the component (has no parameters).

    PROPERTIES:

    • State – Gets the hook state.
    • AltKeyDown – Gets a Boolean value indicating if the ALT key is down.
    • CtrlKeyDown – Gets a Boolean value indicating if the CTRL key is down.
    • ShiftKeyDown– Gets a Boolean value indicating if the SHIFT key is down.

    EVENTS:

    • StateChanged – Fires the event if the keyboard hook is installed/uninstalled.
    • KeyDown – Fires the event if a keyboard button is down.
    • KeyUp – Fires the event if the keyboard button is up.

CLIPBOARD HOOK

    The ClipboardHook class can be use to add a window to the clipboard chain and fire clipboard changed event.

    METHODS:

    • InstallHook – Adds a window to the clipboard chain (has no parameters).
    • RemoveHook – Removes the window from the clipboard chain (has no parameters).
    • Dispose – Removes the window from the clipboard chain and frees the memory used by the component (has no parameters).

    PROPERTIES:

    • State – Gets the hook state.
    • HWnd – Gets the hooked window handle.

    EVENTS:

    • StateChanged – Fires the event if the clipboard hook is installed/uninstalled.
    • ClipboardChanged– Fires the event when the clipboard contents is changed (redrawn).

Background

The hooks are simply wrapper classes that use some windows API methods to hook and monitor for Windows messages. The following list is the API methods that have been used in the library:

  • SetWindowsHookEx
  • UnhookWindowsHookEx
  • CallNextHookEx
  • WindowFromPoint
  • SendInput
  • SetClipboardViewer
  • ChangeClipboardChain

Code Example (VB.Net)

In order to use WindowsHook library in your project, you should add a reference to the assembly (WindowsHookLib.dll).

Imports WindowsHookLib

Public Class Form1

    'Class level declarations
    Dim WithEvents kHook As New KeyboardHook
    Dim WithEvents mHook As New MouseHook
    Dim WithEvents cHook As New ClipboardHook

    'Alowed control handle list
    Dim alowedList As New List(Of IntPtr)

    Private Sub InstallHooksButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InstallHooksButton.Click
        'Install all hooks
        Try
            'Install the keyboard hook
            kHook.InstallHook()
        Catch ex As Exception
            MessageBox.Show("Failed to install the keyboard hook!." _
            & Environment.NewLine & ex.Message, "Hook Error!", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Try
            'Install the mouse hook
            mHook.InstallHook()
        Catch ex As Exception
            MessageBox.Show("Failed to install the mouse hook!." _
            & Environment.NewLine & ex.Message, "Hook Error!", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Try
            'Install the clipboard hook
            cHook.InstallHook(Me)
        Catch ex As Exception
            MessageBox.Show("Failed to install the clipboard hook!." _
            & Environment.NewLine & ex.Message, "Hook Error!", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub RemoveHooksButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveHooksButton.Click
        'Remove the hooks
        Try
            'Remove the keyboard hook
            kHook.RemoveHook()
        Catch ex As Exception
            MessageBox.Show("Error removing the keyboard hook!." _
            & Environment.NewLine & ex.Message, "Hook Error!", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Try
            'Remove the mouse hook
            mHook.RemoveHook()
        Catch ex As Exception
            MessageBox.Show("Error removing the mouse hook!." _
            & Environment.NewLine & ex.Message, "Hook Error!", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Try
            'Remove the clipboard hook
            cHook.RemoveHook()
        Catch ex As Exception
            MessageBox.Show("Error removing the clipboard hook!." _
            & Environment.NewLine & ex.Message, "Hook Error!", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub kHook_KeyDown(ByVal sender As Object, ByVal e As WindowsHookLib.KeyboardEventArgs) Handles kHook.KeyDown
        'Block Alt+PrintScreen key combination
        e.Handled = (e.Modifiers = Keys.Alt AndAlso e.KeyCode = Keys.PrintScreen)
    End Sub

    Private Sub mHook_MouseDown(ByVal sender As Object, ByVal e As WindowsHookLib.MouseEventArgs) Handles mHook.MouseDown
        'Set the Handled property for the mouse down event
        'to block the mouse down message for the 
        'controls that are not in the alowedList.
        e.Handled = Not Me.alowedList.Contains(CType(sender, IntPtr))
        'Do some other things here
        '...
        '...
    End Sub

    Private Sub mHook_MouseUp(ByVal sender As Object, ByVal e As WindowsHookLib.MouseEventArgs) Handles mHook.MouseUp
        'Set the Handled property for the mouse up event
        'to block the mouse up message for the 
        'controls that are not in the alowedList.
        e.Handled = Not Me.alowedList.Contains(CType(sender, IntPtr))
        'Do some other things here
        '...
        '...
    End Sub

    Private Sub cHook_ClipboardChanged(ByVal sender As Object, ByVal e As WindowsHookLib.ClipboardEventArgs) Handles cHook.ClipboardChanged
        'The clipboard content is changed, do something about it.
        MessageBox.Show("The clipboard content is changed!")
        '...
        '...
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'Dispose the keyboard hook. 
        'This will try to remove the hook first.
        kHook.Dispose()
        
        'Remove the mouse hook.
        'This will try to remove the hook first.
        mHook.Dispose()
        
        'Remove the clipboard hook.
        'This will try to remove the hook first.
        cHook.Dispose()
    End Sub

End Class

License

Everyone is permitted to copy and use the WindowsHookLib Library freely for their personal or business needs. There are no restrictions what so ever.

Info

  • Project Name: Windows Hook Library
  • Project Type: Library (dll)
  • Assembly Version: 1.1.1.3
  • File Version: 1.0.0.7
  • Author: Arman Ghazanchyan

System Requirements

  • Windows
  • Dot Net Framework

Downloads




blog comments powered by Disqus