diff -r 19c1aaf900dc -r 7acec5059fa6 Server/WindowsHook.cs
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Server/WindowsHook.cs Tue Aug 12 20:55:50 2014 +0200
@@ -0,0 +1,186 @@
+//=============================================================================
+// COPYRIGHT: Prosoft-Lanz
+//=============================================================================
+//
+// $Workfile: WindowsHook.cs $
+//
+// PROJECT : CodeProject Components
+// VERSION : 1.00
+// CREATION : 19.02.2003
+// AUTHOR : JCL
+//
+// DETAILS : This class implement the Windows hook mechanism.
+// From MSDN, Dino Esposito.
+//
+//-----------------------------------------------------------------------------
+using System;
+using System.Runtime.InteropServices;
+
+namespace CodeProject.Win32API.Hook
+{
+ ///////////////////////////////////////////////////////////////////////
+ #region Class HookEventArgs
+
+ /// Class used for hook event arguments.
+ public class HookEventArgs : EventArgs
+ {
+ /// Event code parameter.
+ public int code;
+ /// wParam parameter.
+ public IntPtr wParam;
+ /// lParam parameter.
+ public IntPtr lParam;
+
+ internal HookEventArgs(int code, IntPtr wParam, IntPtr lParam)
+ {
+ this.code = code;
+ this.wParam = wParam;
+ this.lParam = lParam;
+ }
+ }
+
+ #endregion
+
+ ///////////////////////////////////////////////////////////////////////
+ #region Enum HookType
+
+ /// Hook Types.
+ public enum HookType : int
+ {
+ /// 0
+ WH_JOURNALRECORD = 0,
+ /// 1
+ WH_JOURNALPLAYBACK = 1,
+ /// 2
+ WH_KEYBOARD = 2,
+ /// 3
+ WH_GETMESSAGE = 3,
+ /// 4
+ WH_CALLWNDPROC = 4,
+ /// 5
+ WH_CBT = 5,
+ /// 6
+ WH_SYSMSGFILTER = 6,
+ /// 7
+ WH_MOUSE = 7,
+ /// 8
+ WH_HARDWARE = 8,
+ /// 9
+ WH_DEBUG = 9,
+ /// 10
+ WH_SHELL = 10,
+ /// 11
+ WH_FOREGROUNDIDLE = 11,
+ /// 12
+ WH_CALLWNDPROCRET = 12,
+ /// 13
+ WH_KEYBOARD_LL = 13,
+ /// 14
+ WH_MOUSE_LL = 14
+ }
+ #endregion
+
+ ///////////////////////////////////////////////////////////////////////
+ #region Class WindowsHook
+
+ ///
+ /// Class to expose the windows hook mechanism.
+ ///
+ public class WindowsHook
+ {
+ ///
+ /// Hook delegate method.
+ ///
+ public delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
+
+ // internal properties
+ internal IntPtr hHook = IntPtr.Zero;
+ internal HookProc filterFunc = null;
+ internal HookType hookType;
+
+ ///
+ /// Hook delegate method.
+ ///
+ public delegate void HookEventHandler(object sender, HookEventArgs e);
+
+ ///
+ /// Hook invoke event.
+ ///
+ public event HookEventHandler HookInvoke;
+
+ internal void OnHookInvoke(HookEventArgs e)
+ {
+ if (HookInvoke != null)
+ HookInvoke(this, e);
+ }
+
+ ///
+ /// Construct a HookType hook.
+ ///
+ /// Hook type.
+ public WindowsHook(HookType hook)
+ {
+ hookType = hook;
+ filterFunc = new HookProc(this.CoreHookProc);
+ }
+ ///
+ /// Construct a HookType hook giving a hook filter delegate method.
+ ///
+ /// Hook type
+ /// Hook filter event.
+ public WindowsHook(HookType hook, HookProc func)
+ {
+ hookType = hook;
+ filterFunc = func;
+ }
+
+ // default hook filter function
+ internal int CoreHookProc(int code, IntPtr wParam, IntPtr lParam)
+ {
+ if (code < 0)
+ return CallNextHookEx(hHook, code, wParam, lParam);
+
+ // let clients determine what to do
+ HookEventArgs e = new HookEventArgs(code, wParam, lParam);
+ OnHookInvoke(e);
+
+ // yield to the next hook in the chain
+ return CallNextHookEx(hHook, code, wParam, lParam);
+ }
+
+ ///
+ /// Install the hook.
+ ///
+ public void Install()
+ {
+ hHook = SetWindowsHookEx(hookType, filterFunc, IntPtr.Zero, (int)AppDomain.GetCurrentThreadId());
+ }
+
+
+ ///
+ /// Uninstall the hook.
+ ///
+ public void Uninstall()
+ {
+ if (hHook != IntPtr.Zero)
+ {
+ UnhookWindowsHookEx(hHook);
+ hHook = IntPtr.Zero;
+ }
+ }
+
+ #region Win32 Imports
+
+ [DllImport("user32.dll")]
+ internal static extern IntPtr SetWindowsHookEx(HookType code, HookProc func, IntPtr hInstance, int threadID);
+
+ [DllImport("user32.dll")]
+ internal static extern int UnhookWindowsHookEx(IntPtr hhook);
+
+ [DllImport("user32.dll")]
+ internal static extern int CallNextHookEx(IntPtr hhook, int code, IntPtr wParam, IntPtr lParam);
+
+ #endregion
+ }
+ #endregion
+}