0
我正在使用SetWindowsHookEx()創建鍵盤鉤子。創造似乎是成功的,但註冊過程永遠不會被調用。有什麼我做錯了嗎?SetWindowsHookEx WH_KEYBOARD_LL沒有得到事件
#region Windows API Functions Declarations
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern bool UnhookWindowsHookEx(int idHook);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
#endregion
=
private void CreateHook()
{
int id_hook = (int)HookType.WH_KEYBOARD_LL;
HookProc lpfn = new HookProc(this.KeyboardHookProc);
using (ProcessModule curModule = Process.GetCurrentProcess().MainModule)
hHook = SetWindowsHookEx(id_hook, lpfn, GetModuleHandle(curModule.ModuleName), 0);
if (hHook == 0)
throw new Exception("could not start monitoring mouse events");
}
=
private int KeyboardHookProc(int code, IntPtr wParam, IntPtr lParam)
{
if (code >= 0)
Console.WriteLine((Keys)wParam.ToInt32());
return CallNextHookEx(0, code, wParam, lParam);
}
=
的功能似乎在做一些事情,因爲當函數連接到鍵盤循環,鍵盤輸入將被推遲了一段時間,並附着在鼠標循環時,鼠標輸入一段時間會緩慢。但是KeyboardHookProc似乎沒有受到影響。順便說一句,我正在使用.NET 3.5。 – Nippysaurus 2009-11-17 05:19:28