【问题标题】:How to distinguish a physical mouse click from a code one?如何区分物理鼠标点击和代码点击?
【发布时间】:2022-12-18 06:19:34
【问题描述】:

我现在正在写一个自动点击器。以我的知识水平,我有一个很难解决的问题。 我有低级挂钩来检测鼠标 KeyDown 和 KeyUp。 `

private bool LeftButtonStatus;    
private void AutoClicker_Load(object sender, EventArgs e)
        {      
            core.mouseHook.LeftButtonDown += new MouseHook.MouseHookCallback(mouseHook_LeftKeyDown);
            core.mouseHook.LeftButtonUp += new MouseHook.MouseHookCallback(mouseHook_LeftKeyUp);
        }
     private void mouseHook_LeftKeyDown(MouseHook.MSLLHOOKSTRUCT ma)
        {
        LeftButtonStatus = true;
        StartClicking();
        }
     private void mouseHook_LeftKeyUp(KeyboardHook.VKeys key)
        {
        LeftButtonStatus = false;
        StartClicking();    
        }
     private void StartClicking()
        {
            if (LeftButtonStatus)
                LeftButtonTimer.Start();
            else
                LeftButtonTimer.Stop();      
        }
      private void LeftButtonTimer_Tick(object sender, EventArgs e)
        {
            Core.LeftClick();
        }

my Click Method in Core Class Look like this:

[DllImport("user32.dll")]
        private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
   public static void LeftClick()
        {
            mouse_event(((int)KeyStates.LeftDown) | ((int)KeyStates.LeftDown), 0, 0, 0, 0);
        }

` 问题是当我调用 Core.Left Click();我的钩子检测到它 并停止定时器。 如何做 Core.Left click();被 mouseHook_LeftKeyUp 和 mouseHook_LeftKeyDown 方法忽略了吗?

感谢您花时间回答我的问题 祝你有美好的一天。

我尝试了我想到的一切,但没有任何效果。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    您可以尝试的一种方法是使用全局标志来指示鼠标按钮当前是被用户按下还是被您的代码按下。您可以在 mouseHook_LeftKeyDown 和 mouseHook_LeftKeyUp 方法中设置标志以跟踪鼠标按钮的当前状态,然后检查 StartClicking() 方法中的标志以确定是启动还是停止计时器。

    以下是如何实现此功能的示例:

    // Global flag to track the current state of the mouse button
    private bool isMouseDown = false;
    
    // Flag to indicate whether the mouse button is currently being pressed by the user or by the code
    private bool isUserMouseDown = false;
    
    private void AutoClicker_Load(object sender, EventArgs e)
    {
        // Register event handlers for mouse button down and up events
        core.mouseHook.LeftButtonDown += new MouseHook.MouseHookCallback(mouseHook_LeftKeyDown);
        core.mouseHook.LeftButtonUp += new MouseHook.MouseHookCallback(mouseHook_LeftKeyUp);
    }
    
    // Event handler for mouse button down events
    private void mouseHook_LeftKeyDown(MouseHook.MSLLHOOKSTRUCT ma)
    {
        // Set the isMouseDown flag to true and the isUserMouseDown flag to true
        isMouseDown = true;
        isUserMouseDown = true;
    
        // Start the clicking timer
        StartClicking();
    }
    
    // Event handler for mouse button up events
    private void mouseHook_LeftKeyUp(KeyboardHook.VKeys key)
    {
        // Set the isMouseDown flag to false and the isUserMouseDown flag to false
        isMouseDown = false;
        isUserMouseDown = false;
    
        // Stop the clicking timer
        StartClicking();
    }
    
    // Method to start or stop the clicking timer based on the current state of the mouse button
    private void StartClicking()
    {
        // If the mouse is currently down and the mouse button is being pressed by the user, start the timer
        if (isMouseDown && isUserMouseDown)
        {
            LeftButtonTimer.Start();
        }
        else
        {
            // Otherwise, stop the timer
            LeftButtonTimer.Stop();
        }
    }
    
    // Timer event handler that triggers a left mouse click
    private void LeftButtonTimer_Tick(object sender, EventArgs e)
    {
        // Set the isMouseDown flag to true (to prevent the timer from being stopped) and the isUserMouseDown flag to false
        // (to indicate that the mouse button is being pressed by the code)
        isMouseDown = true;
        isUserMouseDown = false;
    
        // Trigger a left mouse click using the Core.LeftClick() method
        Core.LeftClick();
    }
    

    在此示例中,StartClicking() 方法检查 isMouseDown 和 isUserMouseDown 标志以确定是启动还是停止计时器。 isMouseDown 标志跟踪鼠标按钮的当前状态,isUserMouseDown 标志指示鼠标按钮当前是被用户按下还是被代码按下。

    当从 mouseHook_LeftKeyDown 和 mouseHook_LeftKeyUp 事件处理程序调用 StartClicking() 方法时,isUserMouseDown 标志设置为 true 或 false,具体取决于鼠标按钮是按下还是释放。这允许 StartClicking() 方法确定鼠标按钮当前是否被用户按下。

    当调用 LeftButtonTimer_Tick 事件处理程序以触发鼠标左键单击时,它将 isMouseDown 标志设置为 true 并将 isUserMouseDown 标志设置为 false 以指示代码当前正在按下鼠标按钮。这可以防止 StartClicking() 方法在执行 LeftButtonTimer_Tick 事件处理程序时停止计时器。

    通过使用这些标志,您可以确保 StartClicking() 方法仅根据用户输入启动和停止计时器,而不是根据您的代码生成的鼠标左键单击。

    【讨论】:

      猜你喜欢
      • 2011-08-27
      • 1970-01-01
      • 2011-04-20
      • 2013-07-13
      • 1970-01-01
      • 2010-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多