static IntPtr LowLevelKeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam) { var khs = (HookAPI.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(HookAPI.KeyboardHookStruct)); if (nCode < 0) { return(HookAPI.CallNextHookEx(m_hHook, nCode, wParam, lParam)); } else { if (mode == 1) { if ((khs.Flags & 128) != 0) { current_pressed = true; } GCHandle gch = GCHandle.Alloc(m_hHook); return(HookAPI.CallNextHookEx(m_hHook, nCode, wParam, lParam)); } else { if (mode == 2) { current_pressed = true; } IntPtr val = new IntPtr(1); return(val); } } }
static void Unhook() { HookAPI.UnhookWindowsHookEx(m_hHook); gc_h.Free(); }
static void SetHook() { m_callback = new HookAPI.LowLevelKeyboardProcDelegate(LowLevelKeyboardHookProc); gc_h = GCHandle.Alloc(m_callback); m_hHook = HookAPI.SetWindowsHookEx(WH_KEYBOARD_LL, m_callback, HookAPI.GetModuleHandle(IntPtr.Zero), 0); }