-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
214 lines (185 loc) · 7.59 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Input;
namespace AutoClicker
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
public static extern int SetCursorPos(int x, int y);
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
//Mouse actions
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
System.Timers.Timer timer = new System.Timers.Timer();
Thread keyboardThread;
private int minutesValue;
private int secondsValue;
private int milisecondsValue;
private int calculatedValue;
public Form1()
{
InitializeComponent();
//Create the initial calculated value for the timer interval by calling the textChangedBox function
textChangedInBox(null, null);
//Start the thread searching for keyboard presses
keyboardThread = new Thread(KeyboardListener);
keyboardThread.SetApartmentState(ApartmentState.STA);
keyboardThread.Start();
//Add TextChanged listeners becasue by default, NumericalUpDowns do not have this event
minutesBox.TextChanged += textChangedInBox;
secondsBox.TextChanged += textChangedInBox;
milisecondsBox.TextChanged += textChangedInBox;
}
//Repeat this finction periodically to check if a new key was pressed in the background
private void KeyboardListener(){
while (true) {
Thread.Sleep(40);
//If F1 is pressed in background, stop autoclicking
if ((Keyboard.GetKeyStates(Key.F1) & KeyStates.Down) > 0)
{
//Check if any values in the form are empty
if (minutesValue != 0 || secondsValue != 0 || milisecondsValue != 0)
{
startAutoClick();
}
else
{
MessageBox.Show("There must be an interval value to begin the autoclicker.");
//Restart the thread becasue it will suspend after the messagebox (unknown why this happens)
keyboardThread = new Thread(KeyboardListener);
keyboardThread.SetApartmentState(ApartmentState.STA);
keyboardThread.Start();
}
}
//If F2 is pressed in background, stop autoclicking
else if ((Keyboard.GetKeyStates(Key.F2) & KeyStates.Down) > 0)
{
stopAutoClick();
}
}
}
//Function for virtually clicking the mouse
private void clickMouse(object source, EventArgs e)
{
SetCursorPos(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y);
//Check which clicking mode will be used
if (leftCheckbox.Checked)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
if (rightCheckbox.Checked)
{
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
}
}
//Run when the start button is clicked
private void startButton_Click(object sender, EventArgs e)
{
//Check if a value other than 0 exists
if (minutesValue != 0 || secondsValue != 0 || milisecondsValue != 0)
{
startAutoClick();
}
else
{
MessageBox.Show("There must be an interval value to begin the autoclicker.");
}
}
//Run when the stop button is clicked
private void stopButton_Click(object sender, EventArgs e)
{
startButton.Enabled = true;
stopButton.Enabled = false;
stopAutoClick();
}
//Stops the autoclick from occurring
private void stopAutoClick()
{
//Must invoke on the UIThread due to using another custom thread
Invoke((MethodInvoker)delegate {
//Enable and disable buttons
startButton.Enabled = true;
stopButton.Enabled = false;
//Enable the input boxes
minutesBox.Enabled = true;
secondsBox.Enabled = true;
milisecondsBox.Enabled = true;
//Enable the checkboxes
leftCheckbox.Enabled = true;
rightCheckbox.Enabled = true;
});
//Stop the timer from doing anything
timer.Enabled = false;
timer.Stop();
}
private void startAutoClick()
{
Invoke((MethodInvoker)delegate
{
//Enable and disable buttons
startButton.Enabled = false;
stopButton.Enabled = true;
//Disable the input boxes
minutesBox.Enabled = false;
secondsBox.Enabled = false;
milisecondsBox.Enabled = false;
//Disable the checkboxes
leftCheckbox.Enabled = false;
rightCheckbox.Enabled = false;
});
//Reset all timer values
timer.AutoReset = true;
timer.Enabled = true;
timer.Elapsed += clickMouse;
timer.Interval = calculatedValue;
timer.Start();
}
private void textChangedInBox(object sender, EventArgs e)
{
//Update the integer values with the text values in the textbox
//No need to check data type, numerical up down already handles number inpout only
minutesValue = int.Parse(minutesBox.Value.ToString());
secondsValue = int.Parse(secondsBox.Value.ToString());
milisecondsValue = int.Parse(milisecondsBox.Value.ToString());
//Create the calculated value for the timer interval
calculatedValue = milisecondsValue + (secondsValue * 1000) + (minutesValue * 10000);
}
//Just opens the help window when the help icon is clicked
private void pictureBox1_Click(object sender, EventArgs e)
{
HelpForm helpform = new HelpForm();
helpform.Show();
}
private void exitProgram(object sender, FormClosingEventArgs e)
{
Environment.Exit(0);
}
//These functions run whenever the corresponding checkbox is clicked
private void checkboxChangedLeft(object sender, EventArgs e)
{
Invoke((MethodInvoker)delegate
{
leftCheckbox.Checked = true;
rightCheckbox.Checked = false;
});
}
private void checkboxChangedRight(object sender, EventArgs e)
{
Invoke((MethodInvoker)delegate
{
leftCheckbox.Checked = false;
rightCheckbox.Checked = true;
});
}
}
}