除了低级钩子和原始输入,还有其他方法可以检测全局鼠标钩子吗?

时间:2019-06-06 09:08:57

标签: c# .net

我试图检测全局鼠标单击并使用C#在Windows桌面应用程序中移动。

我通过使用低级鼠标挂钩成功地做到了这一点。但是,它本身会默默地脱钩,我无法再次将其重新钩住。

RawInput函数也不适用。

那么,除了低级鼠标钩子和rawinput函数之外,还有其他方法可以检测全局鼠标钩子吗?

   private const int WH_MOUSE_LL = 14;

   private static Win32Interop.LowLevelHookProc _proc = HookCallback;

    private static IntPtr _hookID = IntPtr.Zero;

    public static void Install()
    {
        _hookID = SetHook(_proc);
        StartRehookTimer();
    }

    private static IntPtr SetHook(Win32Interop.LowLevelHookProc proc)
    {
        return Win32Interop.SetWindowsHookEx(WH_MOUSE_LL, proc, IntPtr.Zero, 0);
    }

    private static void StartRehookTimer()
    {
        Timer _timerClock = new Timer
        {
            Interval = 300000
        };
        _timerClock.Elapsed += OnTimer;
        _timerClock.Enabled = true;
    }

    private static void OnTimer(object sender, ElapsedEventArgs e)
    {
        bool unhookSuccess = false;
        if (_hookID != IntPtr.Zero)
        {
            // uninstall system-wide hook

            unhookSuccess = Win32Interop.UnhookWindowsHookEx(_hookID);

            _hookID = IntPtr.Zero;
        }
        _hookID = SetHook(_proc);
        Debug.WriteLine("Unhook: Calling UnhookWindowsHookEx" + unhookSuccess + _hookID);
    }

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 &&
            MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
        {
            Debug.WriteLine("Mouse Down-----");
        }

        return Win32Interop.CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

0 个答案:

没有答案