美服疯狂坦克辅助瞄准外挂C#版开发(二)全局鼠标键盘HOOK
大家都知道,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;
- protected const byte VK_RSHIFT = 0xA1;
- protected const byte VK_LCONTROL = 0xA2;
- protected const byte VK_RCONTROL = 0x3;
- protected const byte VK_LALT = 0xA4;
- protected const byte VK_RALT = 0xA5;
- protected const byte LLKHF_ALTDOWN = 0x20;
- #endregion
- #region Private Variables
- protected int _hookType;
- protected int _handleToHook;
- protected bool _isStarted;
- protected HookProc _hookCallback;
- #endregion
- #region Properties
- public bool IsStarted
- {
- get
- {
- return _isStarted;
- }
- }
- #endregion
- #region Constructor
- public GlobalHook()
- {
- Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
- }
- #endregion
- #region Methods
- /// <summary>
- /// start a hook
- /// </summary>
- public void Start()
- {
- if(!_isStarted && _hookType != 0)
- {
- _hookCallback = new HookProc(HookCallbackProcedure);
- _handleToHook = SetWindowsHookEx(_hookType,_hookCallback,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);
- if(_handleToHook != 0)
- {
- _isStarted = true;
- }
- }
- }
- /// <summary>
- /// stop the hook
- /// </summary>
- public void Stop()
- {
- if(_isStarted)
- {
- UnhookWindowsHookEx(_handleToHook);
- _isStarted = false;
- }
- }
- /// <summary>
- /// virtual method:must be overriden by each extending hook
- /// </summary>
- /// <param name="nCode"></param>
- /// <param name="wParam"></param>
- /// <param name="lParam"></param>
- /// <returns></returns>
- protected virtual int HookCallbackProcedure(int nCode,Int32 wParam,IntPtr lParam)
- {
- return 0;
- }
- /// <summary>
- /// stop the hook when the applaction is exiting.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void Application_ApplicationExit(object sender,EventArgs e)
- {
- if(_isStarted)
- {
- Stop();
- }
- }
- #endregion
- }
- }
键盘HOOK类:KeyboardHook.cs
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- namespace GlobalMouseKeyBoardHook
- {
- /// <summary>
- /// Captures global keyboard events
- /// </summary>
- public class KeyboardHook : GlobalHook
- {
- public bool GoOnToCallNext = true;
- #region Events
- public event KeyEventHandler KeyDown;
- public event KeyEventHandler KeyUp;
- public event KeyPressEventHandler KeyPress;
- #endregion
- #region Constructor
- public KeyboardHook()
- {
- _hookType = WH_KEYBOARD_LL;
- }
- #endregion
- #region Methods
- protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
- {
- bool handled = false;
- if (nCode > -1 && (KeyDown != null || KeyUp != null || KeyPress != null))
- {
- KeyboardHookStruct keyboardHookStruct =
- (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
- // Is Control being held down?
- bool control = ((GetKeyState(VK_LCONTROL) & 0x80) != 0) ||
- ((GetKeyState(VK_RCONTROL) & 0x80) != 0);
- // Is Shift being held down?
- bool shift = ((GetKeyState(VK_LSHIFT) & 0x80) != 0) ||
- ((GetKeyState(VK_RSHIFT) & 0x80) != 0);
- // Is Alt being held down?
- bool alt = ((GetKeyState(VK_LALT) & 0x80) != 0) ||
- ((GetKeyState(VK_RALT) & 0x80) != 0);
- // Is CapsLock on?
- bool capslock = (GetKeyState(VK_CAPITAL) != 0);
- // Create event using keycode and control/shift/alt values found above
- KeyEventArgs e = new KeyEventArgs(
- (Keys)(
- keyboardHookStruct.vkCode |
- (control ? (int)Keys.Control : 0) |
- (shift ? (int)Keys.Shift : 0) |
- (alt ? (int)Keys.Alt : 0)
- ));
- // Handle KeyDown and KeyUp events
- switch (wParam)
- {
- case WM_KEYDOWN:
- case WM_SYSKEYDOWN:
- if (KeyDown != null)
- {
- KeyDown(this, e);
- handled = handled || e.Handled;
- }
- break;
- case WM_KEYUP:
- case WM_SYSKEYUP:
- if (KeyUp != null)
- {
- KeyUp(this, e);
- handled = handled || e.Handled;
- }
- break;
- }
- // Handle KeyPress event
- if (wParam == WM_KEYDOWN &
- !handled &
- !e.SuppressKeyPress &
- KeyPress != null)
- {
- byte[] keyState = new byte[256];
- byte[] inBuffer = new byte[2];
- GetKeyboardState(keyState);
- if (ToAscii(keyboardHookStruct.vkCode,
- keyboardHookStruct.scanCode,
- keyState,
- inBuffer,
- keyboardHookStruct.flags) == 1)
- {
- char key = (char)inBuffer[0];
- if ((capslock ^ shift) && Char.IsLetter(key))
- key = Char.ToUpper(key);
- KeyPressEventArgs e2 = new KeyPressEventArgs(key);
- KeyPress(this, e2);
- handled = handled || e.Handled;
- }
- }
- }
- if(handled)
- {
- return 1;
- }
- else
- {
- if(GoOnToCallNext)
- {
- return CallNextHookEx(_handleToHook,nCode,wParam,lParam);
- }
- else
- {
- return 1;
- }
- }
- }
- #endregion
- }
- }
鼠标HOOK类:MouseHook.cs
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- namespace GlobalMouseKeyBoardHook
- {
- /// <summary>
- /// Captures global mouse events
- /// </summary>
- public class MouseHook : GlobalHook
- {
- #region MouseEventType Enum
- private enum MouseEventType
- {
- None,
- MouseDown,
- MouseUp,
- DoubleClick,
- MouseWheel,
- MouseMove
- }
- #endregion
- #region Events
- public event MouseEventHandler MouseDown;
- public event MouseEventHandler MouseUp;
- public event MouseEventHandler MouseMove;
- public event MouseEventHandler MouseWheel;
- public event EventHandler Click;
- public event EventHandler DoubleClick;
- #endregion
- #region Constructor
- public MouseHook()
- {
- _hookType = WH_MOUSE_LL;
- }
- #endregion
- #region Methods
- protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
- {
- if (nCode > -1 && (MouseDown != null || MouseUp != null || MouseMove != null))
- {
- MouseLLHookStruct mouseHookStruct =
- (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));
- MouseButtons button = GetButton(wParam);
- MouseEventType eventType = GetEventType(wParam);
- MouseEventArgs e = new MouseEventArgs(
- button,
- (eventType == MouseEventType.DoubleClick ? 2 : 1),
- mouseHookStruct.pt.x,
- mouseHookStruct.pt.y,
- (eventType == MouseEventType.MouseWheel ? (int)((mouseHookStruct.mouseData >> 16) & 0xffff) : 0));
- // Prevent multiple Right Click events (this probably happens for popup menus)
- if (button == MouseButtons.Right && mouseHookStruct.flags != 0)
- {
- eventType = MouseEventType.None;
- }
- switch (eventType)
- {
- case MouseEventType.MouseDown:
- if (MouseDown != null)
- {
- MouseDown(this, e);
- }
- break;
- case MouseEventType.MouseUp:
- if (Click != null)
- {
- Click(this, new EventArgs());
- }
- if (MouseUp != null)
- {
- MouseUp(this, e);
- }
- break;
- case MouseEventType.DoubleClick:
- if (DoubleClick != null)
- {
- DoubleClick(this, new EventArgs());
- }
- break;
- case MouseEventType.MouseWheel:
- if (MouseWheel != null)
- {
- MouseWheel(this, e);
- }
- break;
- case MouseEventType.MouseMove:
- if (MouseMove != null)
- {
- MouseMove(this, e);
- }
- break;
- default:
- break;
- }
- }
- return CallNextHookEx(_handleToHook,nCode,wParam,lParam);
- }
- private MouseButtons GetButton(Int32 wParam)
- {
- switch (wParam)
- {
- case WM_LBUTTONDOWN:
- case WM_LBUTTONUP:
- case WM_LBUTTONDBLCLK:
- return MouseButtons.Left;
- case WM_RBUTTONDOWN:
- case WM_RBUTTONUP:
- case WM_RBUTTONDBLCLK:
- return MouseButtons.Right;
- case WM_MBUTTONDOWN:
- case WM_MBUTTONUP:
- case WM_MBUTTONDBLCLK:
- return MouseButtons.Middle;
- default:
- return MouseButtons.None;
- }
- }
- private MouseEventType GetEventType(Int32 wParam)
- {
- switch (wParam)
- {
- case WM_LBUTTONDOWN:
- case WM_RBUTTONDOWN:
- case WM_MBUTTONDOWN:
- return MouseEventType.MouseDown;
- case WM_LBUTTONUP:
- case WM_RBUTTONUP:
- case WM_MBUTTONUP:
- return MouseEventType.MouseUp;
- case WM_LBUTTONDBLCLK:
- case WM_RBUTTONDBLCLK:
- case WM_MBUTTONDBLCLK:
- return MouseEventType.DoubleClick;
- case WM_MOUSEWHEEL:
- return MouseEventType.MouseWheel;
- case WM_MOUSEMOVE:
- return MouseEventType.MouseMove;
- default:
- return MouseEventType.None;
- }
- }
- #endregion
- }
- }
键盘模拟器:KeyboardSimulator.cs
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- namespace GlobalMouseKeyBoardHook
- {
- /// <summary>
- /// Standard Keyboard Shortcuts used by most applications
- /// </summary>
- public enum StandardShortcut
- {
- Copy,
- Cut,
- Paste,
- SelectAll,
- Save,
- Open,
- New,
- Close,
- }
- /// <summary>
- /// Simulate keyboard key presses
- /// </summary>
- public static class KeyboardSimulator
- {
- #region Windows API Code
- const int KEYEVENTF_EXTENDEDKEY = 0x1;
- const int KEYEVENTF_KEYUP = 0x2;
- [DllImport("user32.dll")]
- static extern void keybd_event(byte key, byte scan, int flags, int extraInfo);
- //[DllImport("user32.dll")]
- //static extern int SetKeyboardState(byte[] lpKeyState);
- //[DllImport("user32.dll")]
- //static extern int GetKeyboardState(byte[] lpKeyState);
- #endregion
- #region Methods
- public static void KeyDown(Keys key)
- {
- keybd_event(ParseKey(key), 0, 0, 0);
- }
- public static void KeyUp(Keys key)
- {
- keybd_event(ParseKey(key), 0, KEYEVENTF_KEYUP, 0);
- }
- public static void KeyPress(Keys key)
- {
- KeyDown(key);
- KeyUp(key);
- }
- //public static void KeyHold(Keys key)
- //{
- // byte[] KeyBoardState = new byte[256];
- // GetKeyboardState(KeyBoardState);
- // KeyBoardState[ParseKey(key)] =1;
- // SetKeyboardState(KeyBoardState);
- //}
- public static void SimulateStandardShortcut(StandardShortcut shortcut)
- {
- switch (shortcut)
- {
- case StandardShortcut.Copy:
- KeyDown(Keys.Control);
- KeyPress(Keys.C);
- KeyUp(Keys.Control);
- break;
- case StandardShortcut.Cut:
- KeyDown(Keys.Control);
- KeyPress(Keys.X);
- KeyUp(Keys.Control);
- break;
- case StandardShortcut.Paste:
- KeyDown(Keys.Control);
- KeyPress(Keys.V);
- KeyUp(Keys.Control);
- break;
- case StandardShortcut.SelectAll:
- KeyDown(Keys.Control);
- KeyPress(Keys.A);
- KeyUp(Keys.Control);
- break;
- case StandardShortcut.Save:
- KeyDown(Keys.Control);
- KeyPress(Keys.S);
- KeyUp(Keys.Control);
- break;
- case StandardShortcut.Open:
- KeyDown(Keys.Control);
- KeyPress(Keys.O);
- KeyUp(Keys.Control);
- break;
- case StandardShortcut.New:
- KeyDown(Keys.Control);
- KeyPress(Keys.N);
- KeyUp(Keys.Control);
- break;
- case StandardShortcut.Close:
- KeyDown(Keys.Alt);
- KeyPress(Keys.F4);
- KeyUp(Keys.Alt);
- break;
- case StandardShortcut.Print:
- KeyDown(Keys.Control);
- KeyPress(Keys.P);
- KeyUp(Keys.Control);
- break;
- }
- }
- static byte ParseKey(Keys key)
- {
- // Alt, Shift, and Control need to be changed for API function to work with them
- switch (key)
- {
- case Keys.Alt:
- return (byte)18;
- case Keys.Control:
- return (byte)17;
- case Keys.Shift:
- return (byte)16;
- default:
- return (byte)key;
- }
- }
- #endregion
- }
- }
鼠标模拟器:MouseSimulator.cs
- using System;
- using System.Text;
- using System.Runtime.InteropServices;
- using System.Drawing;
- using System.Windows.Forms;
- namespace GlobalMouseKeyBoardHook
- {
- /// <summary>
- /// Mouse buttons that can be pressed
- /// </summary>
- public enum MouseButton
- {
- Left = 0x2,
- Right = 0x8,
- Middle = 0x20
- }
- /// <summary>
- /// Operations that simulate mouse events
- /// </summary>
- public static class MouseSimulator
- {
- #region Windows API Code
- [DllImport("user32.dll")]
- static extern int ShowCursor(bool show);
- [DllImport("user32.dll")]
- static extern void mouse_event(int flags, int dX, int dY, int buttons, int extraInfo);
- const int MOUSEEVENTF_MOVE = 0x1;
- const int MOUSEEVENTF_LEFTDOWN = 0x2;
- const int MOUSEEVENTF_LEFTUP = 0x4;
- const int MOUSEEVENTF_RIGHTDOWN = 0x8;
- const int MOUSEEVENTF_RIGHTUP = 0x10;
- const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
- const int MOUSEEVENTF_MIDDLEUP = 0x40;
- const int MOUSEEVENTF_WHEEL = 0x800;
- const int MOUSEEVENTF_ABSOLUTE = 0x8000;
- #endregion
- #region Properties
- /// <summary>
- /// Gets or sets a structure that represents both X and Y mouse coordinates
- /// </summary>
- public static Point Position
- {
- get
- {
- return new Point(Cursor.Position.X, Cursor.Position.Y);
- }
- set
- {
- Cursor.Position = value;
- }
- }
- /// <summary>
- /// Gets or sets only the mouse's x coordinate
- /// </summary>
- public static int X
- {
- get
- {
- return Cursor.Position.X;
- }
- set
- {
- Cursor.Position = new Point(value, Y);
- }
- }
- /// <summary>
- /// Gets or sets only the mouse's y coordinate
- /// </summary>
- public static int Y
- {
- get
- {
- return Cursor.Position.Y;
- }
- set
- {
- Cursor.Position = new Point(X, value);
- }
- }
- #endregion
- #region Methods
- /// <summary>
- /// Press a mouse button down
- /// </summary>
- /// <param name="button"></param>
- public static void MouseDown(MouseButton button)
- {
- mouse_event(((int)button), 0, 0, 0, 0);
- }
- /// <summary>
- /// Press a mouse button down
- /// </summary>
- /// <param name="button"></param>
- public static void MouseDown(MouseButtons button)
- {
- switch (button)
- {
- case MouseButtons.Left:
- MouseDown(MouseButton.Left);
- break;
- case MouseButtons.Middle:
- MouseDown(MouseButton.Middle);
- break;
- case MouseButtons.Right:
- MouseDown(MouseButton.Right);
- break;
- }
- }
- /// <summary>
- /// Let a mouse button up
- /// </summary>
- /// <param name="button"></param>
- public static void MouseUp(MouseButton button)
- {
- mouse_event(((int)button) * 2, 0, 0, 0, 0);
- }
- /// <summary>
- /// Let a mouse button up
- /// </summary>
- /// <param name="button"></param>
- public static void MouseUp(MouseButtons button)
- {
- switch (button)
- {
- case MouseButtons.Left:
- MouseUp(MouseButton.Left);
- break;
- case MouseButtons.Middle:
- MouseUp(MouseButton.Middle);
- break;
- case MouseButtons.Right:
- MouseUp(MouseButton.Right);
- break;
- }
- }
- /// <summary>
- /// Click a mouse button (down then up)
- /// </summary>
- /// <param name="button"></param>
- public static void Click(MouseButton button)
- {
- MouseDown(button);
- MouseUp(button);
- }
- /// <summary>
- /// Click a mouse button (down then up)
- /// </summary>
- /// <param name="button"></param>
- public static void Click(MouseButtons button)
- {
- switch (button)
- {
- case MouseButtons.Left:
- Click(MouseButton.Left);
- break;
- case MouseButtons.Middle:
- Click(MouseButton.Middle);
- break;
- case MouseButtons.Right:
- Click(MouseButton.Right);
- break;
- }
- }
- /// <summary>
- /// Double click a mouse button (down then up twice)
- /// </summary>
- /// <param name="button"></param>
- public static void DoubleClick(MouseButton button)
- {
- Click(button);
- Click(button);
- }
- /// <summary>
- /// Double click a mouse button (down then up twice)
- /// </summary>
- /// <param name="button"></param>
- public static void DoubleClick(MouseButtons button)
- {
- switch (button)
- {
- case MouseButtons.Left:
- DoubleClick(MouseButton.Left);
- break;
- case MouseButtons.Middle:
- DoubleClick(MouseButton.Middle);
- break;
- case MouseButtons.Right:
- DoubleClick(MouseButton.Right);
- break;
- }
- }
- /// <summary>
- /// Roll the mouse wheel. Delta of 120 wheels up once normally, -120 wheels down once normally
- /// </summary>
- /// <param name="delta"></param>
- public static void MouseWheel(int delta)
- {
- mouse_event(MOUSEEVENTF_WHEEL, 0, 0, delta, 0);
- }
- /// <summary>
- /// Show a hidden current on currently application
- /// </summary>
- public static void Show()
- {
- ShowCursor(true);
- }
- /// <summary>
- /// Hide mouse cursor only on current application's forms
- /// </summary>
- public static void Hide()
- {
- ShowCursor(false);
- }
- #endregion
- }
- }
至此,我们的全局鼠标键盘HOOK库所有源代码已经完成,直接编译输出为GlobalMouseKeyBoardHook.dll即可在其它任何需要全局HOOK的程序中直接引用。
调用DEMO:
1、在新建的WinFrom项目中引用GlobalMouseKeyBoardHook.dll。
2、Form1.cs中替换以下代码,直接编译运行。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using GlobalMouseKeyBoardHook;
- namespace WindowsApplication2
- {
- public partial class Form1:Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender,EventArgs e)
- {
- MouseHook MH = new MouseHook();
- KeyboardHook KH = new KeyboardHook();
- MH.MouseDown += new MouseEventHandler(MouseHook_MouseDown);
- MH.MouseUp += new MouseEventHandler(MouseHook_MouseUp);
- KH.KeyDown += new KeyEventHandler(KeyBoardHook_KeyDown);
- KH.KeyUp += new KeyEventHandler(KeyBoardHook_KeyUp);
- KH.KeyPress += new KeyPressEventHandler(KeyBoardHook_KeyPress);
- MH.Start();
- KH.Start();
- }
- private void MouseHook_MouseDown(object sender,MouseEventArgs e)
- {
- MessageBox.Show("当前鼠标鼠标:" + e.X.ToString() + "," + e.Y.ToString());
- }
- private void MouseHook_MouseUp(object sender,MouseEventArgs e)
- {
- MessageBox.Show("当前鼠标鼠标:" + e.X.ToString() + "," + e.Y.ToString());
- }
- private void KeyBoardHook_KeyDown(object sender,KeyEventArgs e)
- {
- MessageBox.Show(e.KeyCode.ToString());
- }
- private void KeyBoardHook_KeyUp(object sender,KeyEventArgs e)
- {
- MessageBox.Show(e.KeyCode.ToString());
- }
- private void KeyBoardHook_KeyPress(object sender,KeyPressEventArgs e)
- {
- MessageBox.Show(e.KeyChar.ToString());
- }
- }
- }
美服疯狂坦克辅助瞄准外挂C#版开发(二)全局鼠标键盘HOOK相关推荐
- 美服疯狂坦克辅助瞄准外挂C#版开发(四)程序使用说明和完成源代码及其下载
到现在为止,我们已经将疯狂坦克外挂所需相关的功能点及解决方案完成. 先说说常规的使用方法: 1.编译发布程序后,先运行程序(默认不可见),然后进入美服疯狂坦克. 2.在游戏中按(Pause)键启动各种 ...
- 美服疯狂坦克辅助瞄准外挂C#版开发(一)物理模型及弹道曲线方程
疯狂坦克弹道曲线方程式: 由上面的图表,根据初中物理知识,我们可以推导以下弹道曲线方程式: 以上物理模型和公式就已经完成了,当然这只是一个近似的公式,由于疯狂坦克游戏算法中存在同角和力点不均匀分布,所 ...
- 美服疯狂坦克辅助瞄准外挂C#版开发(三)在全屏游戏屏幕上绘制弹道曲线
要在游戏全屏模式下屏幕绘制弹道曲线,需要用到以下三个API: //获取DC [DllImport("User32.dll")] public extern static Syste ...
- 疯狂python讲义视频 百度云-疯狂Python讲义 PDF高清版附源码
内容简介 本书全面,深入地介绍了Python编程的相关内容,大致可分为四个部分.*系统部分介绍了Python的基本语法结构,函数编程,类和对象,模块和包,异常处理等: 第二部分主要介绍Python常用 ...
- java怎么给坦克上图片_Java坦克大战 (七) 之图片版
在此小易将坦克大战这个项目分为几个版本,以此对J2SE的知识进行回顾和总结,希望这样也能给刚学完J2SE的小伙伴们一点启示! 坦克大战效果图: 坦克大战V0.7图片版实现功能: 1.将方向定义为一个E ...
- 疯狂Android讲义(第2版)重印10次的超级畅销书
查看书籍详细信息: 疯狂Android讲义(第2版)(重印10次的超级畅销书-- 编辑推荐 本书第一版荣获"电子工业出版社最畅销图书奖":累计印刷10次,销售码洋二百余万,是And ...
- Java坦克大战 (七) 之图片版
本文来自:小易博客专栏.转载请注明出处:http://blog.csdn.net/oldinaction 在此小易将坦克大战这个项目分为几个版本,以此对J2SE的知识进行回顾和总结,希望这样也能给刚学 ...
- 金蝶KIS商贸版开发往来对账单明细表[无辅助属性批号]
金蝶KIS商贸版开发往来对账单明细表[无辅助属性批号] 客户需求: 金蝶商品资料如果勾选了'辅助属性',原有往来对账表中会显示出过多的销售明细行在客户对账时极为不方便,经过开发后,带有批号及辅助属性的 ...
- 全平台辅助答题(PHP版)
辅助答题(PHP版) Github 版本记录 第一个版本(QH.php) 使用PhpStorm的PHP Script运行 1.先打开菜单Run->Edit Configurations选项 2. ...
最新文章
- 网络推广专员教大家网站SEO优化中锚文本的使用技巧
- Python学习笔记:错误,测试,调试(转)
- 客户端稳定性优化实战,Crash率最高下降40%
- 蓝桥杯国赛 皮亚诺曲线距离
- c++ stl 获取最小值_如何在C ++ STL中找到向量的最小/最小元素?
- IOT(20)---2018年有哪些值得期待的物联网应用领域?
- Java Web学习总结(1)——JavaWeb开发入门
- BZOJ4868: [Shoi2017]期末考试
- 服务器上的文件如何查看,如何查看远程服务器上的文件
- 纯小白成功安装交叉编译工具arm-none-eabi-gcc
- numpy库学习总结
- Android Studio 嵌入X5WebView
- 机器学习 交叉验证与网格搜索调参
- 北京医保报销比例,范围
- 带云的计算机词语,带云字的词语和成语有哪些
- MySQL索引的详细分析和数据结构
- 字符串正则替换、点替换横杠
- 科林明伦杯”哈尔滨理工大学第十届程序设计竞赛B(减成1)
- linux 上oracle已经启动 但是客户端无法连接,Oracle 客户端连接排错
- linux gpt转mbr命令,Diskpart命令将gpt格式转换成mbr教程
热门文章
- Catia 加强板全工序DL图设计视频教程 Autoform R7工艺分析
- js操作word套红
- 鼠标光标设置——就是玩儿~
- 电子摄像头可以取代镜子吗?
- 56/14 shell脚本 后台启动 程序1 + “tail -f log“, ctrl +c 导致程序1中断
- ARM裸机的知识总结(4) ------- 利用GPIO控制LED
- 伤寒杂病论.辨少阴病脉证并治
- I-PEX线束加工 LVDS CABLE液晶屏线 笔记本驱屏线 测试线
- 人工智能与大数据就业前景_电子信息(人工智能与大数据方向)专业介绍
- 算法学习 |从无到有 刷爆LeetCode算法神器