Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Diagnostics;
- using System.Windows.Forms;
- using System.Threading;
- using System.Runtime.InteropServices;
- using System.Xml.Serialization;
- using System.IO;
- using System.Xml;
- using System.Collections;
- namespace AutoClicker
- {
- public partial class Form1 : Form
- {
- [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")]
- public static extern int SetCursorPos(int x, int y);
- #region Fields
- private const int MOUSEEVENTF_MOVE = 0x0001; /* mouse move */
- private const int MOUSEEVENTF_LEFTDOWN = 0x0002; /* left button down */
- private const int MOUSEEVENTF_LEFTUP = 0x0004; /* left button up */
- private const int MOUSEEVENTF_RIGHTDOWN = 0x0008; /* right button down */
- private const int MOUSEEVENTF_RIGHTUP = 0x0010; /* right button up */
- private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; /* middle button down */
- private const int MOUSEEVENTF_MIDDLEUP = 0x0040; /* middle button up */
- private const int MOUSEEVENTF_XDOWN = 0x0080; /* x button down */
- private const int MOUSEEVENTF_XUP = 0x0100; /* x button down */
- private const int MOUSEEVENTF_WHEEL = 0x0800; /* wheel button rolled */
- private const int MOUSEEVENTF_VIRTUALDESK = 0x4000; /* map to entire virtual desktop */
- private const int MOUSEEVENTF_ABSOLUTE = 0x8000; /* absolute move */
- #endregion
- public Form1()
- {
- InitializeComponent();
- }
- private static bool shouldClick = false;
- public static void toggleClick()
- {
- shouldClick = !shouldClick;
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- if (shouldClick)
- {
- int X = Cursor.Position.X;
- int Y = Cursor.Position.Y;
- mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
- }
- }
- private void trackBar1_Scroll(object sender, EventArgs e)
- {
- timer1.Interval = trackBar1.Value;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- InterceptMouse.RunHook();
- }
- }
- class InterceptMouse
- {
- private static LowLevelMouseProc _proc = HookCallback;
- private static IntPtr _hookID = IntPtr.Zero;
- public static void RunHook()
- {
- _hookID = SetHook(_proc);
- }
- public static void Unhook()
- {
- UnhookWindowsHookEx(_hookID);
- }
- private static IntPtr SetHook(LowLevelMouseProc proc)
- {
- using (Process curProcess = Process.GetCurrentProcess())
- using (ProcessModule curModule = curProcess.MainModule)
- {
- return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
- }
- }
- private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
- private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
- {
- if (nCode >= 0 && MouseMessages.WM_RBUTTONDOWN == (MouseMessages)wParam)
- {
- Form1.toggleClick();
- }
- return CallNextHookEx(_hookID, nCode, wParam, lParam);
- }
- private const int WH_MOUSE_LL = 14;
- private enum MouseMessages
- {
- WM_LBUTTONDOWN = 0x0201, WM_LBUTTONUP = 0x0202, WM_MOUSEMOVE = 0x0200, WM_MOUSEWHEEL = 0x020A, WM_RBUTTONDOWN = 0x0204, WM_RBUTTONUP = 0x0205
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct POINT
- {
- public int x;
- public int y;
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct MSLLHOOKSTRUCT
- {
- public POINT pt;
- public uint mouseData;
- public uint flags;
- public uint time;
- public IntPtr dwExtraInfo;
- }
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment