鼠标键钩应用程序在启动时滞后,无法关闭



我用MouseKeyHook编写了一个简单的自动点击器。

我使用MouseKeyHook的原因是在整个Windows操作系统中检测全局左键点击。

尽管如此,当我运行.exe时,软件在点击时会滞后,过了一段时间,我似乎再也无法点击"X"按钮,应用程序会导致我的窗口崩溃或一切都变慢。

我正在制作这个软件来测试我们的游戏,但这并没有真正的帮助:p

该软件的代码可以在下面找到:

using Gma.System.MouseKeyHook;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Rapid_Fire
{
public partial class Form1 : Form
{
    #region structs
    /// <summary>
    /// Structure for SendInput function holding relevant mouse coordinates and information
    /// </summary>
    public struct INPUT
    {
        public uint type;
        public MOUSEINPUT mi;
    };
    /// <summary>
    /// Structure for SendInput function holding coordinates of the click and other information
    /// </summary>
    public struct MOUSEINPUT
    {
        public int dx;
        public int dy;
        public int mouseData;
        public int dwFlags;
        public int time;
        public IntPtr dwExtraInfo;
    };
    #endregion
    // Constants for use in SendInput and mouse_event
    public const int INPUT_MOUSE = 0x0000;
    public const int MOUSEEVENTF_LEFTDOWN = 0x0002;
    public const int MOUSEEVENTF_LEFTUP = 0x0004;
    private const int INTERVAL = 10;
    private bool m_fire = false;
    private bool m_close = false;
    private int m_counter = INTERVAL;
    private INPUT m_input = new INPUT();
    private IKeyboardMouseEvents m_GlobalHook;
    public Form1()
    {
        InitializeComponent();
        Subscribe();
        InitAutoClick();
        this.FormClosed += new FormClosedEventHandler(formClosed);
    }
    private void Subscribe()
    {
        m_GlobalHook = Hook.GlobalEvents();
        m_GlobalHook.KeyPress += GlobalHookKeyPress;
        m_GlobalHook.MouseDownExt += GlobalHookMouseDownExt;
    }
    private void GlobalHookKeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == 'q') m_fire = false;
    }
    private void GlobalHookMouseDownExt(object sender, MouseEventExtArgs e)
    {
        m_fire = true;
        RapidFire();
    }
    private void InitAutoClick()
    {
        m_input.type = INPUT_MOUSE;
        m_input.mi.dx = 0;
        m_input.mi.dy = 0;
        m_input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
        m_input.mi.dwExtraInfo = IntPtr.Zero;
        m_input.mi.mouseData = 0;
        m_input.mi.time = 0;
    }
    private void RapidFire()
    {
        while (m_fire && !m_close)
        {
            if (m_counter <= 0)
            {
                // ClickLeftMouseButtonSendInput();
                m_counter = INTERVAL;
            }
            m_counter--;
        }
    }
    private void ClickLeftMouseButtonSendInput()
    {
        // Send a left click down followed by a left click up to simulate a full left click
        SendInput(1, ref m_input, Marshal.SizeOf(m_input));
        m_input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
        SendInput(1, ref m_input, Marshal.SizeOf(m_input));
    }
    private void formClosed(object sender, FormClosedEventArgs e)
    {
        m_fire = false;
        m_close = true;
        Unsubscribe();
    }
    public void Unsubscribe()
    {
        m_GlobalHook.MouseDownExt -= GlobalHookMouseDownExt;
        m_GlobalHook.KeyPress -= GlobalHookKeyPress;
        m_GlobalHook.Dispose();
    }
}
}

通过另一种方式修复了脚本。原来我不得不把它附加到一个鼠标左键按下事件上:D

这里的脚本是:

using Gma.System.MouseKeyHook;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Rapid_Fire
{
public partial class Form1 : Form
{
    [DllImport("user32.dll", SetLastError = true)]
    public static extern int SendInput(int nInputs, ref INPUT pInputs, int cbSize);
    #region structs
    /// <summary>
    /// Structure for SendInput function holding relevant mouse coordinates and information
    /// </summary>
    public struct INPUT
    {
        public uint type;
        public MOUSEINPUT mi;
    };
    /// <summary>
    /// Structure for SendInput function holding coordinates of the click and other information
    /// </summary>
    public struct MOUSEINPUT
    {
        public int dx;
        public int dy;
        public int mouseData;
        public int dwFlags;
        public int time;
        public IntPtr dwExtraInfo;
    };
    #endregion
    public const int INPUT_MOUSE = 0x0000;
    public const int MOUSEEVENTF_LEFTDOWN = 0x0002;
    public const int MOUSEEVENTF_LEFTUP = 0x0004;
    private const int TIMES_CLICK_FIRE = 25;
    private bool m_fire = false;
    private INPUT m_input = new INPUT();
    private IKeyboardMouseEvents m_Events;
    public Form1()
    {
        InitializeComponent();
        SubscribeGlobal();
        InitAutoClick();
        this.FormClosed += new FormClosedEventHandler(formClosed);
    }
    private void SubscribeGlobal()
    {
        Unsubscribe();
        Subscribe(Hook.GlobalEvents());
    }
    private void Subscribe(IKeyboardMouseEvents events)
    {
        m_Events = events;
        m_Events.MouseUp += OnMouseUp;
        m_Events.MouseDown += OnMouseDown;
    }   
    private void InitAutoClick()
    {
        m_input.type = INPUT_MOUSE;
        m_input.mi.dx = 0;
        m_input.mi.dy = 0;
        m_input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
        m_input.mi.dwExtraInfo = IntPtr.Zero;
        m_input.mi.mouseData = 0;
        m_input.mi.time = 0;
    }
    private void Log(string text)
    {
        if (IsDisposed) return;
        Console.WriteLine(text);
    }
    private void OnMouseDown(object sender, MouseEventArgs e)
    {
        m_fire = true;
        Thread thread = new Thread(RapidFire);
        thread.Start();
    }
    private void RapidFire()
    {
        while (m_fire)
        {
            for (; ; )
            {
                ClickLeftMouseButtonSendInput();
            }
        }
    }
    private void OnMouseUp(object sender, MouseEventArgs e)
    {
        m_fire = false;
    }
    private void ClickLeftMouseButtonSendInput()
    {
        SendInput(1, ref m_input, Marshal.SizeOf(m_input));
        m_input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
        SendInput(1, ref m_input, Marshal.SizeOf(m_input));
    }
    private void formClosed(object sender, FormClosedEventArgs e)
    {
        Unsubscribe();
    }
    private void Unsubscribe()
    {
        if (m_Events == null) return;
        m_Events.MouseUp -= OnMouseUp;
        m_Events.MouseDown -= OnMouseDown;
        m_Events.Dispose();
        m_Events = null;
    }
}
}

相关内容

最新更新