C# 键盘钩子

前端之家收集整理的这篇文章主要介绍了C# 键盘钩子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

        // 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);
        }

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

猜你在找的C#相关文章