public class win32hook
{
[dllimport("kernel32")]
public static extern int getcurrentthreadid();
[dllimport( "user32",
charset=charset.auto,callingconvention=callingconvention.stdcall)]
public static extern int setwindowshookex(
hooktype idhook,
hookproc lpfn,
int hmod,
int dwthreadid);
public enum hooktype
{
wh_keyboard = 2
}
public delegate int hookproc(int ncode, int wparam, int lparam);
public void sethook()
{
// set the keyboard hook
setwindowshookex(hooktype.wh_keyboard,
new hookproc(this.mykeyboardproc),
0,
getcurrentthreadid());
}
public int mykeyboardproc(int ncode, int wparam, int lparam)
{
//在这里放置你的处理代码 return 0;
}
}
使用方法
可以在form的构造函数里放入
win32hook hook = new win32hook();
hook.sethook();
