Some times I update the component and post the updated date and the assembly version number so it is recommended to check it once a while.
I created this project as a single utility to hook the mouse, keyboard and the clipboard system wide. It is a library file 'WindowsHookLib' dll that can be referenced into various projects. The mouse and keyboard hooks are low level so you can use the 'Handled' property of the 'WindowsHookLib.MouseEventArgs' or the 'WindowsHookLib.KeyEventArgs' 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.
Note, if you use any of these hooks than you MUST call their dispose methods when the application exits. The method will remove the hook and free the memory that is used by the hook component.
MOUSE HOOK
The 'LLMouseHook' class from 'WindowsHookLib' 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.
I provided the 'Handled' property to '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 'WindowsHookLib Demo Project' examples to see how you can condition the mouse handled process.
Class 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.
Class properties:
- State – Gets the hook state.
Class 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 'LLKeyboardHook' class from 'WindowsHookLib' can be use to hook the keyboard globally.
Class 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).
Class 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.
Class 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 from 'WindowsHookLib' can be use to add a window to the clipboard chain and fire clipboard changed event.
Class 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).
Class properties:
- State – Gets the hook state.
- HWnd – Gets the hooked window handle.
Class events:
- StateChanged – Fires the event if the clipboard hook is installed/uninstalled.
- ClipboardChanged– Fires the event when the clipboard contents is changed (redrawn).
The code examples are in the demo project with comments. You can run the project and test the hooks before you use them. If you need the methods' discriptions, than you need to copy the 'WindowsHookLib.xml' file into your project folder too.
By the way, the mouse hook and the clipboard hook are used in the Easy Screen Shot tool app. You can see the main use of mouse hook when you capture an object (window, control, windows shortcut menus etc.) images. The clipboard hook is used to enable the clipboard captur button.
Update History:
Assembly Version 1.1.0.7
File Version 1.0.0.1
- Some internal changes.
- The namespace WindowsHookLib is changed to WindowsHook.
Assembly Version 1.1.1.0
File Version 1.0.0.4
- Minor internal improvements.
Download the latest version here
