首页 > 开发 > 综合 > 正文

C#中键盘钩子的使用(转)

2020-02-03 13:39:14
字体:
来源:转载
供稿:网友
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 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();



    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表