0

我正在尝试通过 Windows 中任何程序上的“键盘和鼠标”来检测用户活动。

GetAsyncKeyState()user32.Dll“.

我通过以下方式检测鼠标移动System.Windows.Forms.Cursor.Position;

我正在寻找一种方法来检测Windows 中任何程序和屏幕上任何位置上的鼠标滚轮滚动。

我知道它可以通过user32.DllGetMessage(). 我从不使用像MouseWheelEvent 这样的寡妇表单控制事件,因为它只适用于当前程序,根本不适用。

谢谢。

4

1 回答 1

0

https://coderedirect.com/questions/511479/mouse-wheel-event-to-work-with-hovered-control https://gist.github.com/HamzaAnis/90d53662a1659be9d2c74f1dc490cb88 https://docs.microsoft.com/ en-gb/windows/win32/api/winuser/ns-winuser-msllhookstruct?redirectedfrom=MSDN 有用的链接

好问题

如果这有帮助,下面是一个具有大部分鼠标钩子的类,因此您可以选择 make an 来使用它,您必须分配标准点击

var mhM = new MouseHook();
mhM.SetHook();
mhM.MouseClickEvent += (object sender, MouseEventArgs e) =>
{ }; 

public class MouseHook
{
    private Point point;
    private Point Point
    {
        get { return point; }
        set
        {
            if (point != value)
            {
                point = value;
                if (MouseMoveEvent != null)
                {
                    var e = new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0);
                    MouseMoveEvent(this, e);
                    //MouseClickEvent(this, new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0));
                    //MouseDownEvent(this, new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0));
                    //MouseUpEvent(this, new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0));
                    
                }
            }
        }
    }
    private int hHook;
    private const int WM_MOUSEMOVE = 0x200;
    private const int WM_LBUTTONDOWN = 0x201;
    private const int WM_RBUTTONDOWN = 0x204;
    private const int WM_MBUTTONDOWN = 0x207;
    private const int WM_LBUTTONUP = 0x202;
    private const int WM_RBUTTONUP = 0x205;
    private const int WM_MBUTTONUP = 0x208;
    private const int WM_LBUTTONDBLCLK = 0x203;
    private const int WM_RBUTTONDBLCLK = 0x206;
    private const int WM_MBUTTONDBLCLK = 0x209;
    private const int WM_MOUSEWHEEL = 0x20A;
    public const int WH_MOUSE_LL = 14;
    public Win32Api.HookProc hProc;
    public MouseHook()
    {
        this.Point = new Point();
    }
    public int SetHook()
    {
        hProc = new Win32Api.HookProc(MouseHookProc);
        hHook = Win32Api.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0);
        return hHook;
    }
    public void UnHook()
    {
        Win32Api.UnhookWindowsHookEx(hHook);
    }
    private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)//, LPARAM Addtl_Info_L
    {
        Win32Api.MouseHookStruct MyMouseHookStruct = (Win32Api.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.MouseHookStruct));
        if (nCode < 0)
        {
            return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);
        }
        else
        {
            //Console.WriteLine("mouse" + MouseClickEvent.ToString());
            if (MouseClickEvent != null)
            {
                MouseButtons button = MouseButtons.None;
                int clickCount = 0;
                switch ((Int32)wParam)
                {
                    case WM_LBUTTONDOWN:
                        button = MouseButtons.Left;
                        clickCount = 1;
                        if (MouseDownEvent != null) MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                        break;
                    case WM_RBUTTONDOWN:
                        button = MouseButtons.Right;
                        clickCount = 1;
                        if (MouseDownEvent != null) MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                        break;
                    case WM_MBUTTONDOWN:
                        button = MouseButtons.Middle;
                        clickCount = 1;
                        if (MouseDownEvent != null) MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                        break;
                    case WM_LBUTTONUP:
                        button = MouseButtons.Left;
                        clickCount = 1;
                        if (MouseUpEvent != null) MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                        break;
                    case WM_RBUTTONUP:
                        button = MouseButtons.Right;
                        clickCount = 1;
                        if (MouseUpEvent != null) MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                        break;
                    case WM_MBUTTONUP:
                        button = MouseButtons.Middle;
                        clickCount = 1;
                        if (MouseUpEvent != null) MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                        break;
                    case WM_MOUSEWHEEL:
                        int data = GET_WHEEL_DELTA_WPARAM(lParam.ToInt32());
                        MSLLHOOKSTRUCT mouseData = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
                        int wheelMovement = GET_WHEEL_DELTA_WPARAM(mouseData.mouseData);
                        Console.WriteLine("out" + wheelMovement.ToString());
                        // int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
                        if (MousescrollEvent != null) MousescrollEvent(this, new ScrollEventArgs(ScrollEventType.SmallDecrement, wheelMovement));
                        break;


                    default:
                        Console.WriteLine(((Int32)lParam).ToString());
                        break;
                }

                

                
    
    var e = new MouseEventArgs(button, clickCount, point.X, point.Y, 0);
                MouseClickEvent(this, e);
            }
            this.Point = new Point(MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y);
            return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);
        }
    }
    [StructLayout(LayoutKind.Sequential)]
    private struct MSLLHOOKSTRUCT
    {
        public Point pt;
        public int mouseData;
        public int flags;
        public int time;
        public long dwExtraInfo;
    }
   


    private static int GET_WHEEL_DELTA_WPARAM(int wparam)
    { 
        return (int)(wparam >> 16); 
    }


    public delegate void MouseMoveHandler(object sender, MouseEventArgs e);
    public event MouseMoveHandler MouseMoveEvent;

    public delegate void MouseClickHandler(object sender, MouseEventArgs e);
    public event MouseClickHandler MouseClickEvent;

    public delegate void MouseDownHandler(object sender, MouseEventArgs e);
    public event MouseDownHandler MouseDownEvent;

    public delegate void MouseUpHandler(object sender, MouseEventArgs e);
    public event MouseUpHandler MouseUpEvent;

    public delegate void MousescrollHandler(object sender, ScrollEventArgs e);
    public event MousescrollHandler MousescrollEvent;

    // public delegate void MouseUpHandler(object sender, MouseEventArgs e);
    //public event MouseUpHandler Mow;
}

于 2022-01-05T23:39:04.663 回答