Sylloge
A C# helper library
code/Device/KeyboardHook.cs
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 using System;
00017 using System.Collections.Generic;
00018 using System.Text;
00019 using System.Runtime.InteropServices;
00020 using Microsoft.Win32.SafeHandles;
00021 
00022 namespace Sylloge.Device
00023 {
00027     public static class GlobalKeyHandler
00028     {
00029         #region Events
00030 
00031         public delegate void DelegateKeyPressed(int wParam, Sylloge.WinAPI.KeybdInput keyCode);
00035         public static event DelegateKeyPressed KeyPressed;
00036 
00037         public static void OnKeyPress(int wParam, Sylloge.WinAPI.KeybdInput keyCode)
00038         {
00039             if (KeyPressed != null) { KeyPressed(wParam, keyCode); }
00040         }
00041 
00042         #endregion
00043 
00044         #region Local Attributes
00045 
00046         public static bool IsInitialized = false;
00047 
00048         #endregion
00049 
00050         #region Exceptions
00051 
00052         public class UninitializedException : System.Exception
00053         {
00054             public UninitializedException()
00055                 : base("The GlobalKeyHandler low level hook has not been initialized")
00056             {
00057             }
00058         }
00059 
00060         #endregion
00061 
00062         #region External Calls (Keyboard Specific)
00063 
00064         [DllImport("User32.dll", SetLastError = true)]
00065         public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, IntPtr hmod, int dwThreadId);
00066         [DllImport("User32.dll", SetLastError = true)]
00067         public static extern IntPtr CallNextHookEx(IntPtr hHook, int nCode, int wParam, ref Sylloge.WinAPI.KeybdInput lParam);
00068 
00069         public delegate IntPtr LowLevelKeyboardProcDelegate(int nCode, int wParam, ref Sylloge.WinAPI.KeybdInput lParam);
00070 
00071         public static IntPtr HookLowLevelKeyboard;
00072 
00073         public static LowLevelKeyboardProcDelegate KeyboardDelegate;// = new LowLevelKeyboardProcDelegate(LowLevelKeyboardProc);
00074 
00075         #endregion
00076 
00077         #region Class Methods and Properties
00078 
00082         public static void Initialize()
00083         {
00084             if (IsInitialized) { return; } // Don't allow multiple initializations
00085             KeyboardDelegate += new LowLevelKeyboardProcDelegate(LowLevelKeyboardProc);
00086             HookLowLevelKeyboard = SetWindowsHookEx(Sylloge.WinAPI.Constants.WH_KEYBOARD_LL, KeyboardDelegate, System.Diagnostics.Process.GetCurrentProcess().MainModule.BaseAddress, 0);
00087             IsInitialized = true;
00088         }
00089 
00093         public static void Dispose()
00094         {
00095             Sylloge.WinAPI.User32.UnhookWindowsHookEx(HookLowLevelKeyboard);
00096             KeyboardDelegate -= new LowLevelKeyboardProcDelegate(LowLevelKeyboardProc);
00097         }
00098 
00099         private static IntPtr LowLevelKeyboardProc(int nCode, int wParam, ref Sylloge.WinAPI.KeybdInput lParam)
00100         {
00101             OnKeyPress(wParam, lParam);
00102             return CallNextHookEx(HookLowLevelKeyboard, nCode, wParam, ref lParam);
00103         }
00104 
00105         #endregion
00106     }
00107 }
 All Classes Namespaces Files Functions Variables Enumerations Properties Events