日期:2014-05-17  浏览次数:21371 次

c#全局钩子出现问题,求解答!
论坛里看到有讨论全局钩子的,代码大概一致,不过我遇到一个问题:虽然全局钩子能挂上,但是不管我按哪个键返回的值都一样,ncode返回的是0,wparam返回256,lparam返回一串不变的数字,不知道是为啥,麻烦帮我瞅瞅。不胜感激!
代码如下:
public delegate int HOOKPROC(int nCode, int wParam, int lParam);
    public enum HookType
    {
        WH_KEYBOARD = 2,//私有钩子
        WH_KEYBOARD_LL = 13//全局钩子
    }
    public abstract class hook
    {
        //设置钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(HookType idHook, HOOKPROC lpfn, IntPtr hInstance, int threadId);
        //抽调钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);

        [DllImport("kernel32")]
        public static extern int GetCurrentThreadId();

        [DllImport("kernel32.dll")]
        public static extern IntPtr GetModuleHandle(string name);

        /// <summary>
        /// 钩子处理委托
        /// </summary>
        public HOOKPROC proc;
        /// <summary>
        /// 钩子类型
        /// </summary>
        public HookType type;
        /// <summary>
        /// 钩子的句柄
        /// </summary>
        public int hHook = 0;

        public hook(HOOKPROC proc, HookType type)
        {
            this.proc = proc;
            this.type = type;
        }

        public abstract int SetWindowsHookEx();
        public virtual void UnhookWindowsHookEx()
        {
            bool retKeyboard = true;
            if (hHook != 0)
            {
          &n