private void StartTracking() { _hook = (int x, IntPtr wParam, IntPtr lParam) => { crosshairAxisX.Top = Cursor.Position.Y - Config.DesktopTop; crosshairAxisY.Left = Cursor.Position.X - Config.DesktopLeft; return(WindowsApi.CallNextHookEx(x, WindowsApi.WH_MOUSE_LL, wParam, lParam)); }; WindowsApi.SetGlobalMouseHook(_hook); }
void Hook() { m_KeyboardHookHandle = IntPtr.Zero; m_User32Handle = IntPtr.Zero; m_KeyboardHook = KeyboardHook; m_User32Handle = WindowsApi.LoadLibrary("User32"); if (m_User32Handle != IntPtr.Zero) { m_KeyboardHookHandle = WindowsApi.SetWindowsHookEx((int)WindowsApi.WindowsHook.WH_KEYBOARD_LL, m_KeyboardHook, m_User32Handle, 0); } }
void Unhook() { if (m_KeyboardHookHandle != IntPtr.Zero) { WindowsApi.UnhookWindowsHookEx(m_KeyboardHookHandle); m_KeyboardHookHandle = IntPtr.Zero; m_KeyboardHook -= KeyboardHook; } if (m_User32Handle != IntPtr.Zero) { WindowsApi.FreeLibrary(m_User32Handle); m_User32Handle = IntPtr.Zero; } }
private static void EnsureSubscribedToGlobalKeyboardEvents() { if (_sKeyboardHookHandle != 0) { return; } _sKeyboardDelegate = KeyboardHookProc; _sKeyboardHookHandle = WindowsApi.SetWindowsHookEx(WindowsApi.WhKeyboardLl, _sKeyboardDelegate, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); if (_sKeyboardHookHandle == 0) { var errorCode = Marshal.GetLastWin32Error(); throw new Win32Exception(errorCode); } }
private static void ForceUnsunscribeFromGlobalKeyboardEvents() { if (_sKeyboardHookHandle == 0) { return; } var result = WindowsApi.UnhookWindowsHookEx(_sKeyboardHookHandle); _sKeyboardHookHandle = 0; _sKeyboardDelegate = null; if (result == 0) { var errorCode = Marshal.GetLastWin32Error(); throw new Win32Exception(errorCode); } }