在C#中設置和使用全局鉤子通常涉及到使用Windows API函數,這需要P/Invoke
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class GlobalHook
{
public const int WH_KEYBOARD_LL = 13;
public const int WM_KEYDOWN = 0x0100;
public const int WM_KEYUP = 0x0101;
public delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, GlobalHook.LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[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);
public class GlobalHook
{
// ... 其他代碼 ...
private IntPtr _hookID = IntPtr.Zero;
private GlobalHook.LowLevelKeyboardProc _proc;
public void SetHook()
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
_proc = HookCallback;
_hookID = SetWindowsHookEx(WH_KEYBOARD_LL, _proc, GetModuleHandle(curModule.ModuleName), 0);
}
}
public void UnsetHook()
{
UnhookWindowsHookEx(_hookID);
}
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_KEYUP))
{
int vkCode = Marshal.ReadInt32(lParam);
Console.WriteLine((Keys)vkCode);
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
}
public static void Main()
{
GlobalHook globalHook = new GlobalHook();
globalHook.SetHook();
Application.Run(); // 保持程序運行,直到用戶關閉它
globalHook.UnsetHook();
}
請注意,全局鉤子可能會影響系統性能,因此請謹慎使用。此外,這種方法可能不適用于所有場景,特別是在某些安全策略嚴格的環境中。在實際應用中,請確保遵循最佳實踐和適當的安全措施。