using System;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Threading;using System.Windows.Forms;
namespace GlobalHookDemo  {

/// <summary>
/// This class allows you to tap keyboard and mouse and / or to detect their activity even when an 
/// application runes in background or does not have any user interface at all. This class raises 
/// common .NET events with KeyEventArgs and MouseEventArgs so you can easily retrive any information you need.
/// </summary>
/// <res>
///  created by - Georgi
///  created on - 22.05.2004 13:08:01
/// </res>
public class UserActivityHook : object {

/// <summary>
/// Default constructor - starts hooks automatically
/// </summary>
public UserActivityHook() {
Start();
}

~UserActivityHook() { 
Stop();
}  public event MouseEventHandler OnMouseActivity;
public event KeyEventHandler KeyDown;
public event KeyPressEventHandler KeyPress;
public event KeyEventHandler KeyUp; public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam); static int hMouseHook = 0; //Declare mouse hook handle as int.
static int hKeyboardHook = 0; //Declare keyboard hook handle as int. //values from Winuser.h in Microsoft SDK.
public const int WH_MOUSE_LL  = 14; //mouse hook constant
public const int WH_KEYBOARD_LL = 13; //keyboard hook constant HookProc MouseHookProcedure; //Declare MouseHookProcedure as HookProc type.
HookProc KeyboardHookProcedure; //Declare KeyboardHookProcedure as HookProc type.

解决方案 »

  1.   

    //Declare wrapper managed POINT class.
    [StructLayout(LayoutKind.Sequential)]
    public class POINT 
    {
    public int x;
    public int y;
    } //Declare wrapper managed MouseHookStruct class.
    [StructLayout(LayoutKind.Sequential)]
    public class MouseHookStruct 
    {
    public POINT pt;
    public int hwnd;
    public int wHitTestCode;
    public int dwExtraInfo;
    } //Declare wrapper managed KeyboardHookStruct class.
    [StructLayout(LayoutKind.Sequential)]
    public class KeyboardHookStruct
    {
    public int vkCode; //Specifies a virtual-key code. The code must be a value in the range 1 to 254. 
    public int scanCode; // Specifies a hardware scan code for the key. 
    public int flags;  // Specifies the extended-key flag, event-injected flag, context code, and transition-state flag.
    public int time; // Specifies the time stamp for this message.
    public int dwExtraInfo; // Specifies extra information associated with the message. 
    }
    //Import for SetWindowsHookEx function.
    //Use this function to install a hook.
    [DllImport("user32.dll",CharSet=CharSet.Auto,
    CallingConvention=CallingConvention.StdCall)]
    public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, 
    IntPtr hInstance, int threadId); //Import for UnhookWindowsHookEx.
    //Call this function to uninstall the hook.
    [DllImport("user32.dll",CharSet=CharSet.Auto,
     CallingConvention=CallingConvention.StdCall)]
    public static extern bool UnhookWindowsHookEx(int idHook);

    //Import for CallNextHookEx.
    //Use this function to pass the hook information to next hook procedure in chain.
    [DllImport("user32.dll",CharSet=CharSet.Auto,
     CallingConvention=CallingConvention.StdCall)]
    public static extern int CallNextHookEx(int idHook, int nCode, 
    Int32 wParam, IntPtr lParam);
      

  2.   

    public void Start()
    {
    // install Mouse hook 
    if(hMouseHook == 0)
    {
    // Create an instance of HookProc.
    MouseHookProcedure = new HookProc(MouseHookProc); hMouseHook = SetWindowsHookEx( WH_MOUSE_LL,
    MouseHookProcedure, 
    Marshal.GetHINSTANCE(
    Assembly.GetExecutingAssembly().GetModules()[0]),
    0); //If SetWindowsHookEx fails.
    if(hMouseHook == 0 ) {
    Stop();
    throw new Exception("SetWindowsHookEx failed.");
    }
    }

    // install Keyboard hook 
    if(hKeyboardHook == 0)
    {
    KeyboardHookProcedure = new HookProc(KeyboardHookProc);
    hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL,
    KeyboardHookProcedure, 
    Marshal.GetHINSTANCE(
    Assembly.GetExecutingAssembly().GetModules()[0]),
    0); //If SetWindowsHookEx fails.
    if(hKeyboardHook == 0 ) {
    Stop();
    throw new Exception("SetWindowsHookEx ist failed.");
    }
    }
    } public void Stop()
    {
    bool retMouse =true;
    bool retKeyboard = true;
    if(hMouseHook != 0)
    {
    retMouse = UnhookWindowsHookEx(hMouseHook);
    hMouseHook = 0;


    if(hKeyboardHook != 0)
    {
    retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
    hKeyboardHook = 0;


    //If UnhookWindowsHookEx fails.
    if (!(retMouse && retKeyboard)) throw new Exception("UnhookWindowsHookEx failed.");
    }