public Guid RegisterMouseHook(MouseHookCallback callback) { var hookId = default(Guid); var hookReadyEvent = new AutoResetEvent(false); var hookThread = new Thread(() => { var hook = new MouseHook(callback); var sleepEvent = new AutoResetEvent(false); hook.Attach(); hookId = hook.Id; MouseHooks[hookId] = hook; hookReadyEvent.Set(); while (true) { sleepEvent.WaitOne(); } }); hookThread.SetApartmentState(ApartmentState.STA); hookThread.IsBackground = true; hookThread.Start(); hookReadyEvent.WaitOne(); return(hookId); }
public IdleNotifier(ISyncContext syncContext, IAppSettingsService settingsService) { this.syncContext = syncContext; this.settingsService = settingsService; keyboardHookCallback = new KeyboardHookCallback(KeyboardHookProc); mouseHookCallback = new MouseHookCallback(MouseHookProc); idleTimer = new Timer(CheckIdleState, null, TIMER_DELAY, TIMER_PERIOD); }
public Form1() { InitializeComponent(); // SetWindowsHookExに渡すコールバックメソッドをフィールドで保持しておく // フィールドで保持しておかないとガベージコレクション関係のエラーが発生する _keyboardHookCallback = KeyCallback; _mouseHookCallback = MouseCallback; var hInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]); // SetWindowsHookExでフックを開始します。戻り値はフック解除に必要なのでフィールドで保持しておきます。 // キーボードフック開始 _keyboardHookHandle = Native.SetWindowsHookEx(Native.WH_KEYBOARD_LL, _keyboardHookCallback, hInstance, 0); // マウスフック開始 _mouseHookHandle = Native.SetWindowsHookEx(Native.WH_MOUSE_LL, _mouseHookCallback, hInstance, 0); }
public static extern System.IntPtr SetWindowsHookEx(int idHook, MouseHookCallback lpfn, System.IntPtr hMod, uint dwThreadId);
internal MouseHook(MouseHookCallback callback) { this.callback = callback; this.Id = Guid.NewGuid(); }
/// <summary> /// フック処理をおこないます。 /// </summary> /// <param name="nCode">ウィンドウメッセージなどを受け取ります。</param> /// <param name="wParam">パラメータを受け取ります。</param> /// <param name="lParam">パラメータを受け取ります。</param> /// <returns>CallNextHookEx 関数の戻り値を返します。</returns> private IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0) { MouseHookCallback h = null; switch ((User32.WMs)wParam) { case User32.WMs.WM_MOUSEMOVE: h = this.MouseMove; break; case User32.WMs.WM_LBUTTONDOWN: if (this._hasLeftButtonDown) { if ((uint)this._timer.MilliSeconds <= GetDoubleClickTime()) { h = this.DoubleClick; this._hasLeftButtonDown = false; } else { h = this.MouseLeftButtonDown; } } else { h = this.MouseLeftButtonDown; this._hasLeftButtonDown = true; } this._timer.Start(); break; case User32.WMs.WM_LBUTTONUP: h = this.MouseLeftButtonUp; break; case User32.WMs.WM_LBUTTONDBLCLK: h = this.DoubleClick; break; case User32.WMs.WM_RBUTTONDOWN: h = this.MouseRightButtonDown; break; case User32.WMs.WM_RBUTTONUP: h = this.MouseRightButtonUp; break; case User32.WMs.WM_MBUTTONDOWN: h = this.MouseMiddleButtonDown; break; case User32.WMs.WM_MBUTTONUP: h = this.MouseMiddleButtonUp; break; case User32.WMs.WM_MOUSEWHEEL: h = this.MouseWheel; break; } if (h != null) { h((MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT))); } } return(CallNextHookEx(this._hookID, nCode, wParam, lParam)); }
private static extern IntPtr SetWindowsHookEx(int idHook, MouseHookCallback lpfn, IntPtr hInstance, uint dwThreadId);
public static extern bool SetMouseHook(MouseHookCallback callback);