MouseKeyHook 应用程序在启动时滞后并且无法关闭
MouseKeyHook application lags on boot and can't be closed
我使用 MouseKeyHook 编写了一个简单的自动答题器。
我使用 MouseKeyHook 的原因是在整个 Windows OS.
中检测全局左键单击
虽然,当我 运行 .exe 时,软件在点击时滞后,过了一会儿我似乎无法再点击 'X' 按钮并且应用程序崩溃了我的 windows 或减慢一切。
我正在制作这个软件来测试我们的游戏,但这并没有真正帮助 :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;
}
}
}
我使用 MouseKeyHook 编写了一个简单的自动答题器。
我使用 MouseKeyHook 的原因是在整个 Windows OS.
中检测全局左键单击虽然,当我 运行 .exe 时,软件在点击时滞后,过了一会儿我似乎无法再点击 'X' 按钮并且应用程序崩溃了我的 windows 或减慢一切。
我正在制作这个软件来测试我们的游戏,但这并没有真正帮助 :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;
}
}
}