全局键盘挂钩,不会禁止用户在表格外输入 [英] Global keyboard hook that doesn't disable user input outside of form

查看:73
本文介绍了全局键盘挂钩,不会禁止用户在表格外输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试为C#查找全局键盘挂钩.我发现了一个可以钩住键盘的类,但是它禁用了表单之外的键盘.这是当前的键盘挂钩:

So i'm trying to find a global keyboard hook for C#. I found a class that worked to hook the keyboard, but it disabled the keyboard outside of the form. This is the current keyboard hook:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Project
{

    class globalKeyboardHook
    {

        public delegate int keyboardHookProc(int code, int wParam, ref keyboardHookStruct lParam);

        public struct keyboardHookStruct
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }

        const int WH_KEYBOARD_LL = 13;
        const int WM_KEYDOWN = 0x100;
        const int WM_KEYUP = 0x101;
        const int WM_SYSKEYDOWN = 0x104;
        const int WM_SYSKEYUP = 0x105;

        public List<Keys> HookedKeys = new List<Keys>();

        IntPtr hhook = IntPtr.Zero;

        public keyboardHookProc SAFE_delegate_callback;
        public event KeyEventHandler KeyDown;

        public event KeyEventHandler KeyUp;

        public globalKeyboardHook()
        {
            hook();
        }

        ~globalKeyboardHook()
        {
            unhook();
        }

        public void hook()
        {
            IntPtr hInstance = LoadLibrary("User32");
            SAFE_delegate_callback = new keyboardHookProc(hookProc);
            hhook = SetWindowsHookEx(WH_KEYBOARD_LL, SAFE_delegate_callback, hInstance, 0);

        }

        public void unhook()
        {
            UnhookWindowsHookEx(hhook);
        }

        public int hookProc(int code, int wParam, ref keyboardHookStruct lParam)
        {

            try
            {
                if (code >= 0)
                {
                    Keys key = (Keys)lParam.vkCode;
                    if (HookedKeys.Contains(key))
                    {
                        KeyEventArgs kea = new KeyEventArgs(key);
                        if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) && (KeyDown != null))
                        {
                            KeyDown(this, kea);
                        }
                        else if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP) && (KeyUp != null))
                        {
                            KeyUp(this, kea);
                        }
                        if (kea.Handled)
                            return 1;
                    }
                }

                return CallNextHookEx(hhook, code, wParam, ref lParam);
            }
            catch { return 0;  }
        }


        [DllImport("user32.dll")]
        static extern IntPtr SetWindowsHookEx(int idHook, keyboardHookProc callback, IntPtr hInstance, uint threadId);
        [DllImport("user32.dll")]
        static extern bool UnhookWindowsHookEx(IntPtr hInstance);
        [DllImport("user32.dll")]
        static extern int CallNextHookEx(IntPtr idHook, int nCode, int wParam, ref keyboardHookStruct lParam);
        [DllImport("kernel32.dll")]
        static extern IntPtr LoadLibrary(string lpFileName);

    }
}

有人会知道如何更改现有的钩子,以便它仍然允许用户键入,或者知道执行此操作的另一个示例.

Would anyone know how to change the exisiting hook so that it still allows the user to type, or know of another example that does this.

谢谢.

推荐答案

Hans Passant发表了评论.我在这里看了这段代码:

Following a comment by Hans Passant. I looked at this bit of code here:

if (kea.Handled)
   return 1;

并将其更改为

if (!kea.Handled)
  return 1;

这篇关于全局键盘挂钩,不会禁止用户在表格外输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆