-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainWindow.xaml.cs
261 lines (229 loc) · 8.61 KB
/
MainWindow.xaml.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using System;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Windows.Interop;
namespace Cookie_Clicker
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public int SleepTimeMillis { get; set; }
public int InitialDelay { get; set; }
public bool IsFixPosition { get; set; }
public double XPosition { get; set; }
public double YPosition { get; set; }
bool canAutoClickerStart = false;
bool canSetGoldenCookiePosition = false;
bool toggleAutoClickerState = false;
private IntPtr _windowHandle;
private HwndSource _source;
private const int HOTKEY_ID = 9000;
private const uint MOD_NONE = 0x0000; // (none)
private const uint MOD_ALT = 0x0001; //ALT
private const uint MOD_CONTROL = 0x0002; //CTRL
private const uint MOD_SHIFT = 0x0004; //SHIFT
private const uint MOD_WIN = 0x0008; //WINDOWS
public const int MOUSEEVENTF_LEFTDOWN = 0x02; // Mouse left click down
public const int MOUSEEVENTF_LEFTUP = 0x04; // Mouse left click up
Thread Clicker;
bool isKeyToggleAllowed = false;
Key toggleKey = Key.RightCtrl; // default key Right Control
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);
[DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetCursorPos(ref Win32Point pt);
[StructLayout(LayoutKind.Sequential)]
internal struct Win32Point
{
public Int32 X;
public Int32 Y;
};
// MAIN
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
SleepTimeMillis = 20;
InitialDelay = 200;
IsFixPosition = false;
XPosition = 300;
YPosition = 400;
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
_windowHandle = new WindowInteropHelper(this).Handle;
_source = HwndSource.FromHwnd(_windowHandle);
_source.AddHook(HwndHook);
RegisterHotKey(_windowHandle, HOTKEY_ID, MOD_NONE, (uint) KeyInterop.VirtualKeyFromKey(toggleKey));
canAutoClickerStart = true;
}
protected override void OnClosed(EventArgs e)
{
canAutoClickerStart = false;
_source.RemoveHook(HwndHook);
UnregisterHotKey(_windowHandle, HOTKEY_ID);
base.OnClosed(e);
}
// The magic function
private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
const int WM_HOTKEY = 0x0312;
switch (msg)
{
case WM_HOTKEY:
switch (wParam.ToInt32())
{
case HOTKEY_ID:
int vkey = ((int)lParam >> 16) & 0xFFFF;
// Stop clicker if Windows key press (Fail safe)
// Dont work? >>>>> Investigate <<<<<<
if (vkey == KeyInterop.VirtualKeyFromKey(Key.LWin))
{
Stop();
clearKeyToggle(null, null);
}
else if (vkey == KeyInterop.VirtualKeyFromKey(toggleKey))
{
// Key Press Logic
if (canAutoClickerStart)
{
if (toggleAutoClickerState)
{
// if clicker status is running (true), toggle to false to stop it
Stop();
}
else
{
// if clicker is not running (false), start it
Start();
}
}
}
handled = true;
break;
}
break;
}
return IntPtr.Zero;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var window = Window.GetWindow(this);
window.KeyDown += HandleKeyPress;
}
private void HandleKeyPress(object sender, KeyEventArgs e)
{
if (isKeyToggleAllowed)
{
UnregisterHotKey(_windowHandle, HOTKEY_ID);
toggleKey = e.Key;
keyNameTextBox.Text = toggleKey.ToString();
RegisterHotKey(_windowHandle, HOTKEY_ID, MOD_NONE, (uint) KeyInterop.VirtualKeyFromKey(toggleKey));
isKeyToggleAllowed = false;
}
return;
}
private void doAClick()
{
if (IsFixPosition)
{
LeftMouseClick((int) XPosition, (int) YPosition);
}
else
{
Point currentMousePosition = GetMousePosition();
LeftMouseClick((int)currentMousePosition.X, (int)currentMousePosition.Y);
}
}
public static Point GetMousePosition()
{
var w32Mouse = new Win32Point();
GetCursorPos(ref w32Mouse);
return new Point(w32Mouse.X, w32Mouse.Y);
}
//This simulates a left mouse click
public static void LeftMouseClick(int xpos, int ypos)
{
SetCursorPos(xpos, ypos);
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}
private void setKeyToggle(object sender, RoutedEventArgs e)
{
isKeyToggleAllowed = true;
}
private void clearKeyToggle(object sender, RoutedEventArgs e)
{
isKeyToggleAllowed = false;
keyNameTextBox.Text = "";
UnregisterHotKey(_windowHandle, HOTKEY_ID);
}
public void Start()
{
toggleAutoClickerState = true; // allow clicker ejecution
Thread.Sleep(InitialDelay);
// new clicker thread creation
Clicker = new Thread(MyAutoClicker);
Clicker.IsBackground = true;
Clicker.Start();
}
public void Stop()
{
toggleAutoClickerState = false; // stops clicker ejecution
if (Clicker != null)
{
Clicker.Join(); // wait for the thread to finish
}
}
public void MyAutoClicker()
{
try
{
while (toggleAutoClickerState)
{
Thread.Sleep(SleepTimeMillis);
doAClick();
}
}
catch
{
// End silently if error
}
}
private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
}
private void resetGoldenCookiePosition(object sender, RoutedEventArgs e)
{
XPosition = 300 / 1920 * SystemParameters.PrimaryScreenWidth;
YPosition = 400 / 1080 * SystemParameters.PrimaryScreenHeight;
xPositionTextBox.Text = XPosition.ToString();
yPositionTextBox.Text = YPosition.ToString();
IsFixPosition = false;
fixPositionCheckBox.IsChecked = false;
}
private void checkFixPosition(object sender, RoutedEventArgs e)
{
IsFixPosition = true;
}
private void uncheckFixPosition(object sender, RoutedEventArgs e)
{
IsFixPosition = false;
}
}
}