Skip to content

Commit

Permalink
Minor cleanup and bug fixes (#23)
Browse files Browse the repository at this point in the history
Fixed the countdown not working and the hold of the button not working when you tab back into the game
  • Loading branch information
smith-j-travis authored May 16, 2023
1 parent 3b3fa63 commit 4b7102e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 43 deletions.
Binary file removed AutoClicker.exe
Binary file not shown.
14 changes: 7 additions & 7 deletions AutoClicker/ButtonInputs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ namespace AutoClicker
{
public partial class ButtonInputs : UserControl
{
private readonly uint _buttonDownCode;
private readonly uint _buttonUpCode;
private readonly uint buttonDownCode;
private readonly uint buttonUpCode;

public bool Needed => cbButtonEnable.Checked;

public ButtonInputs(string buttonName, uint buttonDownCode, uint buttonUpCode)
{
InitializeComponent();

this._buttonDownCode = buttonDownCode;
this._buttonUpCode = buttonUpCode;
this.buttonDownCode = buttonDownCode;
this.buttonUpCode = buttonUpCode;
cbButtonEnable.Text = buttonName;
numDelay.Maximum = int.MaxValue;
numDelay.Value = 200;
}

internal Clicker StartClicking(IntPtr minecraftHandle)
{
var delay = cbHold.Checked ? 0 : (int)numDelay.Value;
var clicker = new Clicker(this._buttonDownCode, this._buttonUpCode, minecraftHandle);
var delay = (int)numDelay.Value;
var clicker = new Clicker(buttonDownCode, buttonUpCode, minecraftHandle);

clicker.Start(delay);
clicker.Start(delay, cbHold.Checked);

return clicker;
}
Expand Down
28 changes: 22 additions & 6 deletions AutoClicker/Clicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,28 @@ public Clicker(uint buttonDownCode, uint buttonUpCode, IntPtr minecraftHandle)

private void Timer_Tick(object sender, EventArgs e)
{
Click();
// keep sending hold every tick as well in case they do something to interrupt the input
if(hold)
{
Hold();
} else
{
Click();
}
}

public void Start(int delay)
public void Start(int delay, bool hold)
{
this.hold = hold;
Stop();
hold = (delay == 0);

if (hold)
//Select the minecraft handle with Alt+Tab to not stop holding (when using the program)
Win32Api.PostMessage(minecraftHandle, buttonDownCode, (IntPtr)0x0001, IntPtr.Zero);
if (hold)
{
Hold();
timer.Interval = delay;
timer.Start();

}
else
{
Click();
Expand All @@ -52,6 +63,11 @@ public void Stop()
Click();
}

private void Hold()
{
Win32Api.PostMessage(minecraftHandle, buttonDownCode, IntPtr.Zero, IntPtr.Zero);
}

private void Click()
{
Win32Api.PostMessage(minecraftHandle, buttonDownCode, IntPtr.Zero, IntPtr.Zero);
Expand Down
39 changes: 21 additions & 18 deletions AutoClicker/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoClicker
Expand All @@ -22,7 +23,7 @@ public Main()
InitializeComponent();
}

private void Btn_action_Click(object sender, EventArgs e)
private async void Btn_action_Click(object sender, EventArgs e)
{
try
{
Expand Down Expand Up @@ -68,29 +69,15 @@ private void Btn_action_Click(object sender, EventArgs e)
lblStartTime.Text = DateTime.Now.ToString("MMMM dd HH:mm tt");
lblStarted.Visible = true;
lblStartTime.Visible = true;

foreach (var mcProcess in mcProcesses)
{
SetControlPropertyThreadSafe(btn_start, "Enabled", false);
SetControlPropertyThreadSafe(btn_stop, "Enabled", true);

var minecraftHandle = mcProcess.MainWindowHandle;
FocusToggle(minecraftHandle);

SetControlPropertyThreadSafe(btn_start, "Text", @"Starting in: ");
Thread.Sleep(500);

for (var i = 5; i > 0; i--)
{
SetControlPropertyThreadSafe(btn_start, "Text", i.ToString());
Thread.Sleep(500);
}
await Task.Run(() => CountDown(mainHandle));

FocusToggle(mainHandle);
SetControlPropertyThreadSafe(btn_start, "Text", @"Running...");
Thread.Sleep(500);

//Right click needs to be ahead of left click for concrete mining
// Right click needs to be ahead of left click for concrete mining
if (biRightMouse.Needed)
{
var clicker = biRightMouse.StartClicking(minecraftHandle);
Expand All @@ -117,6 +104,22 @@ private void Btn_action_Click(object sender, EventArgs e)
}
}

private void CountDown(IntPtr mainHandle)
{
SetControlPropertyThreadSafe(btn_start, "Text", @"Starting in: ");
Thread.Sleep(750);

for (var i = 5; i > 0; i--)
{
SetControlPropertyThreadSafe(btn_start, "Text", i.ToString());
Thread.Sleep(750);
}

FocusToggle(mainHandle);
SetControlPropertyThreadSafe(btn_start, "Text", @"Running...");
Thread.Sleep(750);
}

private void AddToInstanceClickers(Process mcProcess, Clicker clicker)
{
if (instanceClickers.ContainsKey(mcProcess))
Expand Down
24 changes: 12 additions & 12 deletions AutoClicker/MultipleInstances.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public MultipleInstances(IEnumerable<Process> foundProcesses)
{
InitializeComponent();

this.SelectedInstances = new List<int>();
SelectedInstances = new List<int>();
const int x = 25;
var y = 20;
var buttonX = this.grpInstances.Location.X + this.grpInstances.Width - 100;
var buttonX = grpInstances.Location.X + grpInstances.Width - 100;
var processCount = 0;

foreach (var process in foundProcesses)
Expand All @@ -40,8 +40,8 @@ public MultipleInstances(IEnumerable<Process> foundProcesses)

button.Click += (sender, e) => { Win32Api.SetForegroundWindow(process.MainWindowHandle); };

this.grpInstances.Controls.Add(checkbox);
this.grpInstances.Controls.Add(button);
grpInstances.Controls.Add(checkbox);
grpInstances.Controls.Add(button);

y += 25;
processCount++;
Expand All @@ -51,7 +51,7 @@ public MultipleInstances(IEnumerable<Process> foundProcesses)

private void Btn_ok_Click(object sender, EventArgs e)
{
var selectedInstances = this.grpInstances.AllControls<ValueCheckBox>().Where(b => b.Checked).ToList();
var selectedInstances = grpInstances.AllControls<ValueCheckBox>().Where(b => b.Checked).ToList();

if (!selectedInstances.Any())
{
Expand All @@ -60,20 +60,20 @@ private void Btn_ok_Click(object sender, EventArgs e)
}

foreach (var instance in selectedInstances)
this.SelectedInstances.Add(int.Parse(instance.Value));
SelectedInstances.Add(int.Parse(instance.Value));

this.DialogResult = DialogResult.OK;
this.Close();
DialogResult = DialogResult.OK;
Close();
}

private void AdjustForm(int processCount)
{
if (processCount > 4)
{
this.grpInstances.Height += 25;
this.Height += 25;
this.btn_cancel.Location = new Point(this.btn_cancel.Location.X, this.btn_cancel.Location.Y + 25);
this.btn_ok.Location = new Point(this.btn_ok.Location.X, this.btn_ok.Location.Y + 25);
grpInstances.Height += 25;
Height += 25;
btn_cancel.Location = new Point(btn_cancel.Location.X, btn_cancel.Location.Y + 25);
btn_ok.Location = new Point(btn_ok.Location.X, btn_ok.Location.Y + 25);
}
}
}
Expand Down

0 comments on commit 4b7102e

Please sign in to comment.