Skip to content

Commit

Permalink
Make AFK key interval configurable
Browse files Browse the repository at this point in the history
Change default interval to 5000ms
Change key press delay to 5ms
  • Loading branch information
Begus001 committed Apr 24, 2022
1 parent ad7a010 commit a79e72b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
14 changes: 8 additions & 6 deletions GTAOBusinesses/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public partial class MainWindow : Window
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(IntPtr handle);

private readonly Version version = new Version("1.6.4");
private readonly Version version = new Version("1.6.5");

private readonly string stateDir = @"C:\Users\" + Environment.UserName + @"\AppData\Roaming\GTAOBusinesses\";
private const string stateFilename = "state.txt";
Expand Down Expand Up @@ -75,7 +75,7 @@ public partial class MainWindow : Window

private readonly Timer saveTimer = new Timer(1000);

private readonly Timer afkTimer = new Timer(1000);
private readonly Timer afkTimer = new Timer();

private readonly HotkeyManager hotkeyManager;

Expand Down Expand Up @@ -108,15 +108,16 @@ public MainWindow()
saveTimer.AutoReset = true;
saveTimer.Start();

afkTimer.Elapsed += antiAFKAction;
afkTimer.AutoReset = true;

load();

settings = new SettingsManager(settingsPath);
settings.RestoreWindowDimensions(this);
settings.RestoreWindowLocation(this);

afkTimer.Interval = settings.AFKKeyInterval;
afkTimer.Elapsed += antiAFKAction;
afkTimer.AutoReset = true;

hotkeyManager = new HotkeyManager(this, keymapPath);

hotkeyManager.HotkeyPressed += globalKeyHandler;
Expand Down Expand Up @@ -533,6 +534,7 @@ private void btSettings_Click(object sender, RoutedEventArgs e)
window.ShowDialog();
isGTAOpen = false;
settingsOpen = false;
afkTimer.Interval = settings.AFKKeyInterval;
}

private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
Expand Down Expand Up @@ -593,7 +595,7 @@ private void btKeymap_Click(object sender, RoutedEventArgs e)
private void antiAFKAction(object sender, ElapsedEventArgs e)
{
keybd_event((byte)VirtualKey.Noname, (byte)settings.AFKKey, 0, UIntPtr.Zero);
System.Threading.Thread.Sleep(50);
System.Threading.Thread.Sleep(5);
keybd_event((byte)VirtualKey.Noname, (byte)settings.AFKKey, 2, UIntPtr.Zero);
}

Expand Down
2 changes: 2 additions & 0 deletions GTAOBusinesses/Settings.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
<CheckBox x:Name="cbPauseOnGameClose" Margin="10,0,0,0" Content="Pause when GTA is closed" VerticalAlignment="Center" IsChecked="True" Checked="cbPauseOnGameClose_Checked" Unchecked="cbPauseOnGameClose_Unchecked"/>
<Label Content="AFK Key (Hex Hardware Scancode):" Grid.Row="1" VerticalAlignment="Center"/>
<TextBox x:Name="tbAFKKey" Grid.Row="1" Margin="200,5,10,5" KeyDown="tbAFKKey_KeyDown"/>
<Label Content="AFK Key Interval (ms):" Grid.Row="2" VerticalAlignment="Center"/>
<TextBox x:Name="tbAFKKeyInterval" Grid.Row="2" Margin="200,5,10,5" KeyDown="tbAFKKey_KeyDown"/>
</Grid>
</TabItem>
<TabItem x:Name="tiKeymap" Header="Keymap">
Expand Down
12 changes: 12 additions & 0 deletions GTAOBusinesses/Settings.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public partial class Settings : Window

private bool settingPauseOnClose;
private int settingAFKKey;
private int settingAFKKeyInterval;

private Button beingEdited;

Expand All @@ -50,6 +51,7 @@ public Settings(HotkeyManager hotkeyManager, SettingsManager settingsManager, bo

settingPauseOnClose = settingsManager.PauseOnClose;
settingAFKKey = settingsManager.AFKKey;
settingAFKKeyInterval = settingsManager.AFKKeyInterval;

hotkeyManager.UnregisterAll();

Expand Down Expand Up @@ -99,6 +101,7 @@ private void updateUI()

cbPauseOnGameClose.IsChecked = settingPauseOnClose;
tbAFKKey.Text = settingAFKKey.ToString("X");
tbAFKKeyInterval.Text = settingAFKKeyInterval.ToString();
}

private void btCancel_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -224,12 +227,21 @@ private void btOK_Click(object sender, RoutedEventArgs e)
hotkeyManager.Save();
hotkeyManager.ReregisterAll();
settingsManager.PauseOnClose = settingPauseOnClose;

try
{
settingAFKKey = Convert.ToInt32(tbAFKKey.Text, 16);
settingsManager.AFKKey = settingAFKKey;
}
catch { MessageBox.Show("AFK key value invalid, value was not saved!", "Invalid AFK Key", MessageBoxButton.OK, MessageBoxImage.Error); }

try
{
settingAFKKeyInterval = Convert.ToInt32(tbAFKKeyInterval.Text);
settingsManager.AFKKeyInterval = settingAFKKeyInterval;
}
catch { MessageBox.Show("AFK key interval value invalid, value was not saved!", "Invalid AFK Key Interval", MessageBoxButton.OK, MessageBoxImage.Error); }

settingsManager.Save();
Close();
}
Expand Down
16 changes: 15 additions & 1 deletion GTAOBusinesses/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ private enum Settings : int
Y,
Paused,
PauseOnClose,
AFKKey
AFKKey,
AFKKeyInterval
}
public double WindowWidth
{
Expand Down Expand Up @@ -98,6 +99,17 @@ public int AFKKey
settings[(int)Settings.AFKKey] = value;
}
}
public int AFKKeyInterval
{
get
{
return (int)settings[(int)Settings.AFKKeyInterval];
}
set
{
settings[(int)Settings.AFKKeyInterval] = value;
}
}

public string SaveLocation { get; set; }

Expand All @@ -115,6 +127,7 @@ public SettingsManager(string saveLocation)
types.Add(typeof(bool));
types.Add(typeof(bool));
types.Add(typeof(int));
types.Add(typeof(int));

defaults.Add(0.0d);
defaults.Add(0.0d);
Expand All @@ -123,6 +136,7 @@ public SettingsManager(string saveLocation)
defaults.Add(false);
defaults.Add(true);
defaults.Add(0x4e); // NumPad+ (Add)
defaults.Add(5000);

Reset();

Expand Down

0 comments on commit a79e72b

Please sign in to comment.