August 21, 2010

Get keyboard hook and mouse hook in C#

Code demo:
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TestHook
{
class KeyboardAndMouseHook
{
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static LowLevelMouseProc _procMouse = HookCallbackMouse;
private static LowLevelKeyboardProc _procKey = HookCallbackKey;
private static IntPtr _hookIDMouse = IntPtr.Zero;
private static IntPtr _hookIDKey = IntPtr.Zero;

public static void Main()
{
_hookIDMouse = SetHook(_procMouse);
_hookIDKey = SetHook(_procKey);

Application.Run();

UnhookWindowsHookEx(_hookIDMouse);
UnhookWindowsHookEx(_hookIDKey);
}

//===============================================================================================================
//Import library

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);


//taluan
//================================================================================================================
//keyboard hook

private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}

private delegate IntPtr LowLevelKeyboardProc(
int nCode, IntPtr wParam, IntPtr lParam);

private static IntPtr HookCallbackKey(
int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
Console.WriteLine("key: " + (Keys)vkCode);
}

return CallNextHookEx(_hookIDKey, nCode, wParam, lParam);
}

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);



//taluan
//================================================================================================================
//Mouse hook

private static IntPtr SetHook(LowLevelMouseProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_MOUSE_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}

private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);

private static IntPtr HookCallbackMouse(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
{
MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
Console.WriteLine("mouse: " + hookStruct.pt.x + ", " + hookStruct.pt.y);
}
return CallNextHookEx(_hookIDMouse, nCode, wParam, lParam);
}


private const int WH_MOUSE_LL = 14;

private enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_MOUSEMOVE = 0x0200,
WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205
}

[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
}

[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);

}
}

0 Comment:

Post a Comment

Để chất lượng các bài viết ngày được tốt hơn, Bạn vui lòng để lại góp ý hoặc nhận xét vào khung bên dưới. Bạn có thể tự do nhận xét nhưng không trái với thuần phong mỹ tục. Khi gửi nhận xét xin vui lòng để lại: Tên, Địa chỉ mail hoặc địa chỉ Blog để tôi được biết bạn là ai. Xin cảm ơn!

Các bài liên quan




Recent Comments

Xã hội - VnExpress.net