大家都知道,Windows操作系统内部通讯是基于消息机制的。为了实现我们的功能,必须要能够截和处理获游戏中鼠标键盘事件的消息,常规做法是获取游戏窗体的句柄,然后加入HOOK,这样的好处是不会影响到其它正在运行的程序对鼠标键盘操作的响应。但是一般人在玩疯狂坦克的时候,应该不会同时运行Word之类的吧,也就是说,为了简单起见,我们在程序中加入的HOOK均为全局HOOK,注册到系统后会截获所有的键盘鼠标时间消息,而不管这个消息时由哪个程序激发,送往那个目标。
下面我们就用C#先写一个全局鼠标键盘HOOK库(部分代码及资料来源于CodeProject,具体URL找不到了,所以暂不列出):
HOOK抽象类 GlobalHook.cs
- using System;
- using System.Text;
- using System.Runtime.InteropServices;
- using System.Reflection;
- using System.Windows.Forms;
- namespace GlobalMouseKeyBoardHook
- {
- /// <summary>
- /// Abstract base class for Mouse and Keyboard hooks
- /// </summary>
- public abstract class GlobalHook
- {
- #region Windows API Code
- [StructLayout(LayoutKind.Sequential)]
- protected class POINT
- {
- public int x;
- public int y;
- }
- [StructLayout(LayoutKind.Sequential)]
- protected class MouseHookStruct
- {
- public POINT pt;
- public int hwnd;
- public int wHitTestCode;
- public int dwExtraInfo;
- }
- [StructLayout(LayoutKind.Sequential)]
- protected class MouseLLHookStruct
- {
- public POINT pt;
- public int mouseData;
- public int flags;
- public int time;
- public int dwExtraInfo;
- }
- [StructLayout(LayoutKind.Sequential)]
- protected class KeyboardHookStruct
- {
- public int vkCode;
- public int scanCode;
- public int flags;
- public int time;
- public int dwExtraInfo;
- }
- /// <summary>
- /// Extern Method:define Hook to Windows.
- /// </summary>
- /// <param name="idHook">Hook type,decide what message want to Hook</param>
- /// <param name="lpfn">Function Pointer</param>
- /// <param name="hMod">The dll or model include the Hook function</param>
- /// <param name="dwThreadId">Correlative thread,if 0 means the global</param>
- /// <returns>Hook id</returns>
- [DllImport("user32.dll",CharSet = CharSet.Auto,CallingConvention = CallingConvention.StdCall,SetLastError = true)]
- protected static extern int SetWindowsHookEx(int idHook,HookProc lpfn,IntPtr hMod,int dwThreadId);
- /// <summary>
- /// Extern Method:dispose the Hook.
- /// </summary>
- /// <param name="idHook">Hook id</param>
- /// <returns>ture/false</returns>
- [DllImport("user32.dll",CharSet = CharSet.Auto,CallingConvention = CallingConvention.StdCall,SetLastError = true)]
- protected static extern bool UnhookWindowsHookEx(int idHook);
- /// <summary>
- /// Extern Method:call the next hook in the hook linked list
- /// </summary>
- /// <param name="idHook">this hook id</param>
- /// <param name="nCode">the code contain the next hook need to do </param>
- /// <param name="wParam">the parameters</param>
- /// <param name="lParam">the parameters</param>
- /// <returns>The value after the next hook finished.</returns>
- [DllImport("user32.dll",CharSet = CharSet.Auto,CallingConvention = CallingConvention.StdCall)]
- protected static extern int CallNextHookEx(int idHook,int nCode,int wParam,IntPtr lParam);
- /// <summary>
- /// Extern Method:transfer the virtual keycode and keyboard state to ascii string.
- /// </summary>
- /// <param name="uVirtKey">the virtual keycode need to transfer</param>
- /// <param name="uScanCode">set the uVirtKey's transfer code</param>
- /// <param name="lpbKeyState">256 length array to show all key's state in the keyboard</param>
- /// <param name="lpwTransKey">the buffer of the transfer result</param>
- /// <param name="fuState">define a menu is actived</param>
- /// <returns>int</returns>
- [DllImport("user32")]
- protected static extern int ToAscii(int uVirtKey,int uScanCode,byte[] lpbKeyState,byte[] lpwTransKey,int fuState);
- /// <summary>
- /// Copy the 256 virtual key to he buffer
- /// </summary>
- /// <param name="pbKeyState">256 length array to show all key's state in the keyboard</param>
- /// <returns>0</returns>
- [DllImport("user32")]
- protected static extern int GetKeyboardState(byte[] pbKeyState);
- /// <summary>
- /// get a virtual key's state
- /// </summary>
- /// <param name="vKey">a virtual key</param>
- /// <returns>state</returns>
- [DllImport("user32.dll",CharSet = CharSet.Auto,CallingConvention = CallingConvention.StdCall)]
- protected static extern short GetKeyState(int vKey);
- protected delegate int HookProc(int nCode,int wParam,IntPtr lParam);
- protected const int WH_MOUSE_LL = 14;
- protected const int WH_KEYBOARD_LL = 13;
- protected const int WH_MOUSE = 7;
- protected const int WH_KEYBOARD = 2;
- protected const int WM_MOUSEMOVE = 0x200;
- protected const int WM_LBUTTONDOWN = 0x201;
- protected const int WM_RBUTTONDOWN = 0x204;
- protected const int WM_MBUTTONDOWN = 0x207;
- protected const int WM_LBUTTONUP = 0x202;
- protected const int WM_RBUTTONUP = 0x205;
- protected const int WM_MBUTTONUP = 0x208;
- protected const int WM_LBUTTONDBLCLK = 0x203;
- protected const int WM_RBUTTONDBLCLK = 0x206;
- protected const int WM_MBUTTONDBLCLK = 0x209;
- protected const int WM_MOUSEWHEEL = 0x020A;
- protected const int WM_KEYDOWN = 0x100;
- protected const int WM_KEYUP = 0x101;
- protected const int WM_SYSKEYDOWN = 0x104;
- protected const int WM_SYSKEYUP = 0x105;
- protected const byte VK_SHIFT = 0x10;
- protected const byte VK_CAPITAL = 0x14;
- protected const byte VK_NUMLOCK = 0x90;
- protected const byte VK_LSHIFT = 0xA0;</