我使用 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();
}
}
}