下面是源代码  using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace WindowsFormsApplication1

    public partial class Form1 : Form
    {
        static int hHook = 1;
        const int WH_KEYBOARD = 7;
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (hHook == 0)
            {
            NativeMethods.HookProc hp = new NativeMethods.HookProc(KbHookProc);
            hHook = NativeMethods.SetWindowsHookEx(WH_KEYBOARD, hp, IntPtr.Zero, NativeMethods.FindWindow(null, "notepad").ToInt32());
            //NativeMethods.FindWindow(null,"MM").ToInt32());            if (hHook == 0)
            {
                MessageBox.Show("SetWindowsHookEx Failed");
                return;
            }
                button1.Text = "UnHook Windows Hook";
            }
            else
            {
                UnHook();
            }
        }
        void UnHook()
        {
            if (hHook > 0)
            {
                bool ret = NativeMethods.UnhookWindowsHookEx(hHook);                if (ret == false)
                {
                MessageBox.Show("UnhookWindowsHookEx Failed");
                return;
                }
                hHook = 0;
                button1.Text = "Set Windows Hook";
                this.Text = "Keyboard Hook";
            }
        }        public static int KbHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode < 0)
            {
                return NativeMethods.CallNextHookEx(hHook, nCode, wParam, lParam);
                }
                else
                {
                //  MessageBox.Show(wParam.ToString());
                int r = NativeMethods.CallNextHookEx(hHook, nCode, wParam, lParam);                if (wParam == (IntPtr)256)
                {
                }
                else
                {
                KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));                MessageBox.Show("您按下了"+MyKeyboardHookStruct.vkCode.ToString());
                }
            return r;
            }
        }
    }
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
    public  class KeyboardHookStruct
    {
        public int vkCode; //表示一个在1到254间的虚似键盘码
        public int scanCode; //表示硬件扫描码
        public int flags;
        public int time;
        public int dwExtraInfo;    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;namespace WindowsFormsApplication1
{
    internal static class NativeMethods
    {
        public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true, CharSet = CharSet.Unicode)]
        internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
    }
}

解决方案 »

  1.   

      hHook = NativeMethods.SetWindowsHookEx(WH_KEYBOARD, hp, IntPtr.Zero, NativeMethods.FindWindow(null, "notepad").ToInt32());
    用IntPtr.Zero句柄可能不对,试试
    Process curProcess = Process.GetCurrentProcess();
    ProcessModule curModule = curProcess.MainModule;
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern IntPtr GetModuleHandle(string lpModuleName);
    IntPtr.Zero----》GetModuleHandle(curModule.ModuleName)
      

  2.   

    SetWindowsHookEx(WH_KEYBOARD, hp, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
      

  3.   

    按照各位大哥的说法,试了试,可以走下去了,可是又碰到了一个问题,就是到了 void UnHook()
    这个方法里面,进了判断里面 if (ret == false)
      {
      MessageBox.Show("UnhookWindowsHookEx Failed");
      return;
      }
    就跳出来了,还是要谢谢各位大哥的热心,先把帖子接了,,