C#键盘钩子 鼠标钩子
最新对C#模拟键盘按键,鼠标操作产生了兴趣。特从网上收集了一些常用的API用来调用键盘,,鼠标操作。
class Win32API
{
#region DLL导入
/// <summary>
/// 用于设置窗口
/// </summary>
/// <param></param>
/// <param></param>
/// <param></param>
/// <param></param>
/// <param></param>
/// <param></param>
/// <param></param>
/// <returns></returns>
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter,
int X, int Y, int cx, int cy, int uFlags);
/// <summary>
/// 安装钩子
/// </summary>
/// <param></param>
/// <param></param>
/// <param></param>
/// <param></param>
/// <returns></returns>
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr SetWindowsHookEx(WH_Codes idHook, HookProc lpfn,
IntPtr pInstance, int threadId);
/// <summary>
/// 卸载钩子
/// </summary>
/// <param></param>
/// <returns></returns>
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(IntPtr pHookHandle);
/// <summary>
/// 传递钩子
/// </summary>
/// <param></param>
/// <param></param>
/// <param></param>
/// <param></param>
/// <returns></returns>
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(IntPtr pHookHandle, int nCode,
Int32 wParam, IntPtr lParam);
/// <summary>
/// 转换当前按键信息
/// </summary>
/// <param></param>
/// <param></param>
/// <param></param>
/// <param></param>
/// <param></param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern int ToAscii(UInt32 uVirtKey, UInt32 uScanCode,
byte[] lpbKeyState, byte[] lpwTransKey, UInt32 fuState);
/// <summary>
/// 获取按键状态
/// </summary>
/// <param></param>
/// <returns>非0表示成功</returns>
[DllImport("user32.dll")]
public static extern int GetKeyboardState(byte[] pbKeyState);
[DllImport("user32.dll")]
public static extern short GetKeyStates(int vKey);
/// <summary>
/// 获取当前鼠标位置
/// </summary>
/// <param></param>
/// <returns></returns>
[DllImport("user32.dll")]
public extern static int GetCursorPos(ref POINT lpPoint);
#endregion DLL导入
}
class Hocy_Hook
{
#region 私有常量
/// <summary>
/// 按键状态数组
/// </summary>
private readonly byte[] m_KeyState = new byte[ 256 ];
private string flags;
//flag=0 正常 flag=1 监控状态 flag=2 屏蔽键盘//
#endregion 私有常量
#region 私有变量
/// <summary>
/// 鼠标钩子句柄
/// </summary>
private IntPtr m_pMouseHook = IntPtr.Zero;
/// <summary>
/// 键盘钩子句柄
/// </summary>
private IntPtr m_pKeyboardHook = IntPtr.Zero;
/// <summary>
/// 鼠标钩子委托实例
/// </summary>
/// <remarks>
/// 不要试图省略此变量,否则将会导致
/// 激活 CallbackOnCollectedDelegate 托管调试助手 (MDA)。
/// 详细请参见MSDN中关于 CallbackOnCollectedDelegate 的描述
/// </remarks>
private HookProc m_MouseHookProcedure;
/// <summary>
/// 键盘钩子委托实例
/// </summary>
/// <remarks>
/// 不要试图省略此变量,否则将会导致
/// 激活 CallbackOnCollectedDelegate 托管调试助手 (MDA)。
/// 详细请参见MSDN中关于 CallbackOnCollectedDelegate 的描述
/// </remarks>
private HookProc m_KeyboardHookProcedure;
// 添加
public event MouseEventHandler OnMouseActivity;
private const byte VK_SHIFT = 0x10 ;
private const byte VK_CAPITAL = 0x14;
private const byte VK_NUMLOCK = 0x90;
#endregion 私有变量
#region 事件定义
/// <summary>
/// 鼠标更新事件
/// </summary>
/// <remarks>当鼠标移动或者滚轮滚动时触发</remarks>
public event MouseUpdateEventHandler OnMouseUpdate;
/// <summary>
/// 按键按下事件
/// </summary>
public event KeyEventHandler OnKeyDown;
/// <summary>
/// 按键按下并释放事件
/// </summary>
public event KeyPressEventHandler OnKeyPress;
/// <summary>
/// 按键释放事件
/// </summary>
public event KeyEventHandler OnKeyUp;
#endregion 事件定义
#region 私有方法
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70189.html