加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C# 键盘钩子

发布时间:2020-12-15 17:52:17 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 // Win32 keyboard hook. public const int WH_KEYBOARD_LL = 13; public const int NULL = 0; public delegate int HookProc(int code,int wParam,KB

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

        // Win32 keyboard hook.
        public const int WH_KEYBOARD_LL = 13;
        public const int NULL = 0;

        public delegate int HookProc(int code,int wParam,KBDLLHOOKSTRUCT lParam);

        [DllImport("user32.dll",SetLastError = true)]
        public static extern int SetWindowsHookEx(int hookType,HookProc lpfn,int hMod,int dwThreadId);

        [DllImport("User32.dll",SetLastError = true)]
        public extern static int CallNextHookEx(int handle,int code,KBDLLHOOKSTRUCT lParam);

        [StructLayout(LayoutKind.Sequential)]
        public class KBDLLHOOKSTRUCT
        {
            public uint vkCode;
            public uint scanCode;
            public KBDLLHOOKSTRUCT flags;
            public uint time;
            public UIntPtr dwExtraInfo;
        }

        [Flags]
        public enum KBDLLHOOKSTRUCT : uint
        {
            LLKHF_EXTENDED = 0x01,LLKHF_INJECTED = 0x10,LLKHF_ALTDOWN = 0x20,LLKHF_UP = 0x80,}

        public volatile int hHook;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            // 安装全局键盘钩子
            if ((this.hHook = SetWindowsHookEx(WH_KEYBOARD_LL,this.KeyBoardProc,NULL,NULL)) == NULL)
                Console.WriteLine("Unable to establish a keyboard hook.");
        }

        protected int KeyBoardProc(int code,KBDLLHOOKSTRUCT lParam)
        {
            if (lParam.vkCode == (int)Keys.A)
                return 1; // <span style="font-family: arial,宋体,sans-serif;font-size:18px; line-height: 24px; text-indent: 28px;">返回1表示拦截消息,返回0表示放行</span>
            return CallNextHookEx(hHook,code,wParam,lParam);
        }
监控系统内所有进程键盘消息:
? ? ? ? SetWindowsHookEx(WH_KEYBOARD_LL,KeyBoardProc,NULL)
<pre name="code" class="csharp">        // Win32 keyboard hook.
        public const int WH_KEYBOARD = 2;
        public const int NULL = 0;

        public delegate int HookProc(int code,int lParam);

        [DllImport("kernel32.dll",SetLastError = true)]
        public static extern int GetCurrentThreadId();

        [DllImport("user32.dll",int lParam);

        public volatile int hHook;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
<pre name="code" class="csharp" style="line-height: 24px;font-size:18px;">            // 安装键盘钩子
if ((this.hHook = SetWindowsHookEx(WH_KEYBOARD,GetCurrentThreadId())) == NULL) Console.WriteLine("Unable to establish a keyboard hook."); }
 
        protected int KeyBoardQueue(int code,int lParam)
        {
            if (wParam == (int)Keys.A)
                return 1; <span style="font-family: arial,sans-serif;">// </span><span style="font-family: arial,sans-serif;">返回1表示拦截消息,返回0表示放行</span>
            return CallNextHookEx(hHook,lParam);
        }
 监控本进程的所有键盘消息: 
 
 
 
     protected int KeyBoardProc(int code,int lParam)
        {
            if (wParam == (int)Keys.A)
                return 1;
            return CallNextHookEx(hHook,lParam);
        }

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读