-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.xaml.cs
187 lines (153 loc) · 5.76 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
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Threading;
namespace Autoclicker
{
public partial class MainWindow : Window
{
[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);
[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);
private const int HOTKEY_ID = 9000;
private uint selectedHotkey = 0x77;
private const uint MOD_NONE = 0x0000;
private uint mouseButtonDown = 0x02;
private uint mouseButtonUp = 0x04;
private bool autoclickerEnabled = false;
private DispatcherTimer autoclickerTimer;
public MainWindow()
{
InitializeComponent();
InitializeAutoclickerTimer();
}
private void InitializeAutoclickerTimer()
{
autoclickerTimer = new DispatcherTimer();
autoclickerTimer.Interval = TimeSpan.FromMilliseconds(100);
autoclickerTimer.Tick += AutoclickerTimer_Tick;
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
RegisterHotkey();
}
private void RegisterHotkey()
{
IntPtr hWnd = new WindowInteropHelper(this).Handle;
if (hWnd == IntPtr.Zero)
{
//MessageBox.Show("Window handle is not valid.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
HwndSource source = HwndSource.FromHwnd(hWnd);
source.AddHook(HwndHook);
if (!RegisterHotKey(hWnd, HOTKEY_ID, MOD_NONE, selectedHotkey))
{
//MessageBox.Show("Failed to register hotkey.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
}
private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
const int WM_HOTKEY = 0x0312;
if (msg == WM_HOTKEY && wParam.ToInt32() == HOTKEY_ID)
{
ToggleAutoclicker();
handled = true;
}
return IntPtr.Zero;
}
private void ToggleAutoclicker()
{
autoclickerEnabled = !autoclickerEnabled;
if (autoclickerEnabled)
{
StartStopButton.Content = "Stop Autoclicker";
this.Title += " (Clicking)";
autoclickerTimer.Start();
}
else
{
StartStopButton.Content = "Start Autoclicker";
this.Title = "Simple Auto Clicker";
autoclickerTimer.Stop();
}
}
private void StartStopButton_Click(object sender, RoutedEventArgs e)
{
ToggleAutoclicker();
}
private void AutoclickerTimer_Tick(object sender, EventArgs e)
{
DoMouseClick();
}
private void DoMouseClick()
{
mouse_event(mouseButtonDown | mouseButtonUp, 0, 0, 0, 0);
}
protected override void OnClosed(EventArgs e)
{
IntPtr hWnd = new WindowInteropHelper(this).Handle;
if (hWnd != IntPtr.Zero)
{
UnregisterHotKey(hWnd, HOTKEY_ID);
}
base.OnClosed(e);
}
private void ClickSpeedSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (autoclickerTimer != null)
{
autoclickerTimer.Interval = TimeSpan.FromMilliseconds(e.NewValue);
ClickSpeedLabel.Text = $"{e.NewValue} ms";
}
}
private void HotkeyComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
IntPtr hWnd = new WindowInteropHelper(this).Handle;
UnregisterHotKey(hWnd, HOTKEY_ID);
switch ((HotkeyComboBox.SelectedItem as System.Windows.Controls.ComboBoxItem).Content.ToString())
{
case "F8":
selectedHotkey = 0x77;
break;
case "F9":
selectedHotkey = 0x78;
break;
case "F10":
selectedHotkey = 0x79;
break;
}
RegisterHotkey();
}
private void MouseButtonComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
switch ((MouseButtonComboBox.SelectedItem as System.Windows.Controls.ComboBoxItem).Content.ToString())
{
case "Left Click":
mouseButtonDown = 0x02;
mouseButtonUp = 0x04;
break;
case "Right Click":
mouseButtonDown = 0x08;
mouseButtonUp = 0x10;
break;
}
}
private void TextBlock_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
string url = "https://github.com/thedogecraft/autoclicker";
Process.Start(new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
});
}
}
}