0

我用这些线创建了一个钩子(正常工作,除了新的 return 1 线)。我想要做的是禁用右键单击:

if (_keyboardHook == IntPtr.Zero && _mouseHook == IntPtr.Zero)
{
    IntPtr hInstance = LoadLibrary("User32");
    //IntPtr hInstance = Marshal.GetHINSTANCE(Assembly.GetCallingAssembly().GetModules()[0]);

    _keyProc = new HookProc(keyboardHookProc);
    _keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, _keyProc, hInstance, 0);

    try
    {
        _mouseProc = new HookProc(mouseHookProc);
        _mouseHook = SetWindowsHookEx(WH_MOUSE_LL, _mouseProc, hInstance, 0);

这是我的mouseProc:

private int mouseHookProc(int code, int wParam, IntPtr lParam)
{
    try
    {
        _lastHit = DateTime.Now;
        bool go = true;
        if (code >= 0)
        {
            MouseHookStruct mouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));

            //only process if button down or mouse wheel.
            if (wParam == WM_LBUTTONDOWN
             || wParam == WM_MOUSEWHEEL
             || wParam == WM_MBUTTONDOWN
             || wParam == WM_XBUTTONUP
             || wParam == WM_RBUTTONUP)
            {
                bool isWheel = false;
                bool isWheelUp = false;

                int keyCode = 0;
                switch (wParam)
                {
                    case WM_LBUTTONDOWN:
                        keyCode = 1;
                        break;

                    case WM_RBUTTONUP:
                    {
                        keyCode = 2;
                        go = false;
                        break;
                    }
    
                    case WM_XBUTTONUP:
                        var hWord = ((uint)((IntPtr)mouseHookStruct.MouseData) >> 16);
                        if (hWord == 1)
                            keyCode = 5;
                        if (hWord == 2)
                            keyCode = 6;
                        break;

                    case WM_MBUTTONDOWN:
                        keyCode = 4;
                        break;

                    case WM_MOUSEWHEEL:
                        short mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff);
                        isWheel = true;
                        isWheelUp = mouseDelta == 120;
                        break;

                    default:
                        break;
                }
    
                OnKeyArgs kea = new OnKeyArgs(keyCode)
                {
                    IsCtrlPressed = IsPressed(VK_CTRL),
                    IsAltPressed = IsPressed(VK_ALT),
                    IsShiftPressed = IsPressed(VK_SHIFT),
                    WheelInfo = new MouseWheelInfo()
                };
    
                if (isWheel)
                {
                    kea.WheelInfo.IsWheelInvolved = true;
                    kea.WheelInfo.IsWheelUp = isWheelUp;
                }
    
                KeyDown?.Invoke(this, kea);
            }
        }
    
        if (go)
            return CallNextHookEx(_mouseHook, code, wParam, ref lParam);
        else
            return 1;
    }
    catch (Exception ex)
    {
        Logger.Error("Error no mouse hook proc", ex);
        return 0;
    }
}

我还有一个运行检查以重新启动钩子,以防 Windows 将其关闭:

IntPtr hInstance = LoadLibrary("User32");

while (true)
{
    int milliseconds = MilisecondsElapsedFromNow(_lastHit);
    if (milliseconds > 6000 || milliseconds < 0)
    {
        Application.Current.Dispatcher.Invoke(() =>
        {
            UnhookWindowsHookEx(_mouseHook);
            _mouseHook = IntPtr.Zero;
            Thread.Sleep(100);
            _mouseHook = SetWindowsHookEx(WH_MOUSE_LL, _mouseProc, hInstance, 0);
            // Logger.Warning($"Mouse listening for hotkeys restored, Ptr: {_mouseHook}");
        });
    }
    
    Thread.Sleep(500);
}

它工作正常,但在几次右键单击后,整个鼠标停止在 PC 上工作(如果我删除 return 1 行,没有问题!)。

PS:即使我停止调试/执行程序,鼠标也停止工作!

4

0 回答 0