136 lines
4.4 KiB
C#
136 lines
4.4 KiB
C#
|
using System.Drawing;
|
|||
|
using System.Runtime.InteropServices;
|
|||
|
using System.Text;
|
|||
|
using System.Windows;
|
|||
|
using System.Windows.Controls;
|
|||
|
using System.Windows.Data;
|
|||
|
using System.Windows.Documents;
|
|||
|
using System.Windows.Input;
|
|||
|
using System.Windows.Media;
|
|||
|
using System.Windows.Media.Imaging;
|
|||
|
using System.Windows.Navigation;
|
|||
|
using System.Windows.Shapes;
|
|||
|
|
|||
|
namespace 鼠标连点器
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Interaction logic for MainWindow.xaml
|
|||
|
/// </summary>
|
|||
|
public partial class MainWindow : Window
|
|||
|
{
|
|||
|
// Windows API constants
|
|||
|
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
|
|||
|
private const int MOUSEEVENTF_LEFTUP = 0x04;
|
|||
|
private const int WH_KEYBOARD_LL = 13;
|
|||
|
private const int WM_KEYDOWN = 0x0100;
|
|||
|
private const int VK_F8 = 0x77; // Virtual key code for F8
|
|||
|
|
|||
|
// Importing necessary Windows API functions
|
|||
|
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
|||
|
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
|
|||
|
|
|||
|
[DllImport("user32.dll")]
|
|||
|
static extern bool GetCursorPos(out POINT lpPoint);
|
|||
|
|
|||
|
[StructLayout(LayoutKind.Sequential)]
|
|||
|
public struct POINT
|
|||
|
{
|
|||
|
public int X;
|
|||
|
public int Y;
|
|||
|
}
|
|||
|
|
|||
|
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|||
|
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
|
|||
|
|
|||
|
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|||
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|||
|
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
|
|||
|
|
|||
|
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|||
|
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
|
|||
|
|
|||
|
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|||
|
private static extern IntPtr GetModuleHandle(string lpModuleName);
|
|||
|
|
|||
|
// Delegate for low-level keyboard proc
|
|||
|
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
|
|||
|
|
|||
|
private LowLevelKeyboardProc _proc;
|
|||
|
private IntPtr _hookID = IntPtr.Zero;
|
|||
|
|
|||
|
// Timer for clicking
|
|||
|
private Timer clickTimer;
|
|||
|
private bool isClicking = false;
|
|||
|
|
|||
|
public MainWindow()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
_proc = HookCallback;
|
|||
|
_hookID = SetHook(_proc);
|
|||
|
}
|
|||
|
|
|||
|
private IntPtr SetHook(LowLevelKeyboardProc proc)
|
|||
|
{
|
|||
|
using (var curProcess = System.Diagnostics.Process.GetCurrentProcess())
|
|||
|
using (var curModule = curProcess.MainModule)
|
|||
|
{
|
|||
|
return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
|
|||
|
{
|
|||
|
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
|
|||
|
{
|
|||
|
int vkCode = Marshal.ReadInt32(lParam);
|
|||
|
|
|||
|
if (vkCode == VK_F8)
|
|||
|
{
|
|||
|
if (isClicking)
|
|||
|
{
|
|||
|
StopClicking();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
StartClicking();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return CallNextHookEx(_hookID, nCode, wParam, lParam);
|
|||
|
}
|
|||
|
|
|||
|
private void StartClicking()
|
|||
|
{
|
|||
|
isClicking = true;
|
|||
|
clickTimer = new Timer(ClickMouse, null, 0, 50); // Click every 100 ms (adjust as needed)
|
|||
|
}
|
|||
|
|
|||
|
private void StopClicking()
|
|||
|
{
|
|||
|
isClicking = false;
|
|||
|
if (clickTimer != null)
|
|||
|
{
|
|||
|
clickTimer.Dispose();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void ClickMouse(object state)
|
|||
|
{
|
|||
|
GetCursorPos(out POINT cursorPos);
|
|||
|
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, cursorPos.X, cursorPos.Y, 0, 0);
|
|||
|
}
|
|||
|
|
|||
|
protected override void OnClosed(EventArgs e)
|
|||
|
{
|
|||
|
UnhookWindowsHookEx(_hookID);
|
|||
|
base.OnClosed(e);
|
|||
|
}
|
|||
|
int i = 0;
|
|||
|
private void 按钮_Click(object sender, RoutedEventArgs e)
|
|||
|
{
|
|||
|
(sender as Button).Content = i.ToString();
|
|||
|
i++;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|