全局键盘钩子
来源:互联网 发布:淘宝nba旗舰店真假 编辑:程序博客网 时间:2024/05/17 05:11
使用时,只需实例化Hook类的一个实例,然后调用start()安装钩子,stop()拆除钩子.
如 Hook hk = new Hook();
hk.start();
hk.stop();
//--------------全局键盘钩子类Hook------------------
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace SmartDeviceProject1
{
public class Hook
{
public delegate int HookKeyProc(int code, IntPtr wParam, IntPtr lParam);
private HookKeyProc hookKeyDeleg;
public Hook()
{ }
#region public methods
//安装钩子
public void Start()
{
if (hHookKey != 0)
{
//Unhook the previouse one
this.Stop();
}
hookKeyDeleg = new HookKeyProc(HookKeyProcedure);
hHookKey = SetWindowsHookEx(WH_KEYBOARD_LL, hookKeyDeleg, GetModuleHandle(null), 0);
if (hHookKey == 0)
{
throw new SystemException("Failed acquiring of the hook.");
}
}
//拆除钩子
public void Stop()
{
UnhookWindowsHookEx(hHookKey);
}
#endregion
#region protected and private methods
private int HookKeyProcedure(int code, IntPtr wParam, IntPtr lParam)
{
KBDLLHOOKSTRUCT hookStruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
if (code < 0)
return CallNextHookEx(hookKeyDeleg, code, wParam, lParam);
if (hookStruct.vkCode == 0x5B)
{
//如果捕捉到VK_LWIN按键
//......处理......
return -1; //返回-1表示已经处理了,不再往下传递
}
if (hookStruct.vkCode == 0x25)
{
//如果捕捉到VK_LEFT按键
//......处理......
return -1;
}
// 没处理的键的消息往下传递
return CallNextHookEx(hookKeyDeleg, code, wParam, lParam);
}
#endregion
#region P/Invoke declarations
[DllImport("coredll.dll")]
private static extern int SetWindowsHookEx(int type, HookKeyProc HookKeyProc, IntPtr hInstance, int m);
//private static extern int SetWindowsHookEx(int type, HookMouseProc HookMouseProc, IntPtr hInstance, int m);
[DllImport("coredll.dll")]
private static extern IntPtr GetModuleHandle(string mod);
[DllImport("coredll.dll")]
private static extern int CallNextHookEx(
HookKeyProc hhk,
int nCode,
IntPtr wParam,
IntPtr lParam
);
[DllImport("coredll.dll")]
private static extern int GetCurrentThreadId();
[DllImport("coredll.dll", SetLastError = true)]
private static extern int UnhookWindowsHookEx(int idHook);
private struct KBDLLHOOKSTRUCT
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public IntPtr dwExtraInfo;
}
const int WH_KEYBOARD_LL = 20;
public class KeyBoardInfo
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
}
#endregion
}
}
- HOOK全局键盘钩子
- VC全局键盘钩子
- 全局键盘钩子
- 全局键盘钩子
- 全局键盘钩子(WH_KEYBOARD)
- 全局键盘钩子
- 全局低级键盘钩子
- C#全局键盘钩子
- C#全局键盘钩子
- c#-全局键盘钩子
- 设置全局键盘钩子和消息钩子
- .NET的全局键盘钩子
- 键盘"全局钩子"的使用 ..
- 全局键盘钩子开发实例
- HOOK使用:全局键盘钩子
- HOOK使用:全局键盘钩子
- SetWindowsHookEx设置全局键盘钩子
- C#做的全局鼠标/键盘钩子
- 钩子函数
- 在这条路上越走越远
- Install Ubuntu 10.04 LTS on a USB Flash Memory
- 【django 学习笔记】15-集成的子框架
- VS2005中编译出错:Error 1 error C3872: '0x3000': this character is not allowed in an identifier
- 全局键盘钩子
- 【django 学习笔记】16-中间件
- delphi动态调用DLL的方法与技巧
- 【django 学习笔记】17-集成已有的数据库和应用
- 【django 学习笔记】18-国际化
- SqlServer分发订阅报无法将架构脚本传播到订阅服务器错误解决方法
- 【django 学习笔记】19-安全
- Struts 2中实现文件下载(修正中文问题)
- 关于23种设计模式的有趣见解