public const int WH_KEYBOARD = 13;
public const int WH_MOUSE_LL = 14;
public struct KeyboardMSG
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
public int VK_CONTROL;
public int VK_MENU;
public int VK_DELETE;
}
调用钩子函数。参照如下详细代码:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Text;
namespace OpenLogHook
{
/// <summary>
/// Class1
/// </summary>
public class Class1
{
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
//Declare hook handle as int.
static int hHook = 0;
//Declare mouse hook constant.
//For other hook types, you can obtain these values from Winuser.h in Microsoft SDK.
public const int WH_MOUSE = 7;
//public string temp;
public IntPtr glhLogWnd;
public delegate uint aw();
//Declare MouseHookProcedure as HookProc type.
HookProc MouseHookProcedure;
//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 IntPtr hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}
//Declare wrapper managed MouseHookStruct class.
[StructLayout(LayoutKind.Sequential)]
public class COPYDATASTRUCT
{
public int dwData;
public int cbData;
public string lpData;
}
public struct Mystruct//mousehook struct
{
public string szCaption;
public string szHwnd;
public string szOperation;
public string szPtx;
public string szPty;
}
//public delegate int COPYDATASTRUCT();
public const int WM_MOUSEMOVE= 0x0200;
public const int WM_LBUTTONDOWN= 0x0201;
public const int WM_LBUTTONUP= 0x0202;
public const int WM_RBUTTONDOWN= 0x0204;
public const int WM_RBUTTONUP= 0x0205;
public const int WM_COPYDATA= 0x004A;
public const int WM_USER= 0x0400;
public const int WM_SENDDATA= WM_USER + 101;
//Import for SetWindowsHookEx function.
//Use this function to install thread-specific 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,
IntPtr wParam, IntPtr lParam);