Skip to content

Commit

Permalink
Add support for alphabetic keys, and add an option to automatically c…
Browse files Browse the repository at this point in the history
…onvert arrow keys into WASD keys.

Fixes #40
  • Loading branch information
kpreisser committed Jan 24, 2022
1 parent 1941155 commit 951b67d
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ build and run the Mouse Click Simulator on your computer.

- Added support for **Corporate Clash** ([#32](https://github.com/kpreisser/MouseClickSimulator/issues/32)).
- Background mode is now disabled by default (like in earlier versions that didn't support this mode) as it doesn't seem to always work
correctly ([#40](https://github.com/kpreisser/MouseClickSimulator/issues/40)).
correctly.
- Fixed an issue for auto fishing in Walrus Way that occured often when using a low FPS rate (like 30), where the toon would miss the
fishing board after selling the fish.

Expand Down
13 changes: 13 additions & 0 deletions TTMouseclickSimulator/Core/Environment/InteractionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ public void PressKey(WindowsEnvironment.VirtualKey key)
// Check if the window is still active and in foreground.
this.GetWindowPositionCore(failIfMinimized: !this.backgroundMode);

// Allow the subclass to transform the key (e.g. change arrow keys to WASD
// keys).
this.TransformVirtualKey(ref key);

if (!this.keysCurrentlyPressed.Contains(key))
{
if (!this.backgroundMode)
Expand All @@ -333,6 +337,10 @@ public void ReleaseKey(WindowsEnvironment.VirtualKey key)
this.ThrowIfCapabilityNotSet(SimulatorCapabilities.KeyboardInput);
this.CancellationToken.ThrowIfCancellationRequested();

// Allow the subclass to transform the key (e.g. change arrow keys to WASD
// keys).
this.TransformVirtualKey(ref key);

int kcpIdx = this.keysCurrentlyPressed.IndexOf(key);
if (kcpIdx >= 0)
{
Expand Down Expand Up @@ -537,6 +545,11 @@ public void CancelActiveInteractions()

protected abstract List<Process> FindProcesses();

protected virtual void TransformVirtualKey(ref WindowsEnvironment.VirtualKey key)
{
// Do nothing.
}

private void ThrowIfCapabilityNotSet(SimulatorCapabilities capabilities)
{
if (!this.simulator.RequiredCapabilities.IsSet(capabilities))
Expand Down
46 changes: 45 additions & 1 deletion TTMouseclickSimulator/Core/Environment/WindowsEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -695,13 +695,57 @@ public enum VirtualKey : ushort
{
Enter = 0x0D,
Control = 0x11,
Shift = 0x10,
Escape = 0x1B,

Left = 0x25,
Up = 0x26,
Right = 0x27,
Down = 0x28,

Home = 0x24,
End = 0x23
End = 0x23,

Space = 0x20,

A = 'A',
B = 'B',
C = 'C',
D = 'D',
E = 'E',
F = 'F',
G = 'G',
H = 'H',
I = 'I',
J = 'J',
K = 'K',
L = 'L',
M = 'M',
N = 'N',
O = 'O',
P = 'P',
Q = 'Q',
R = 'R',
S = 'S',
T = 'T',
U = 'U',
V = 'V',
W = 'W',
X = 'X',
Y = 'Y',
Z = 'Z',

F1 = 0x70,
F2 = 0x71,
F3 = 0x72,
F4 = 0x73,
F5 = 0x74,
F6 = 0x75,
F7 = 0x76,
F8 = 0x77,
F9 = 0x78,
F10 = 0x79,
F11 = 0x7A,
F12 = 0x7B,
}
}
6 changes: 5 additions & 1 deletion TTMouseclickSimulator/Core/Simulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public Simulator(
ToontownFlavor toontownFlavor,
IAction mainAction,
WindowsEnvironment environmentInterface,
bool useWasdMovement,
bool backgroundMode)
{
if (mainAction is null)
Expand All @@ -36,7 +37,10 @@ public Simulator(
this,
toontownFlavor,
environmentInterface,
backgroundMode);
backgroundMode)
{
UseWasdMovement = useWasdMovement
};
}

public SimulatorCapabilities RequiredCapabilities
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public ToontownFlavor ToontownFlavor
get;
}

public bool UseWasdMovement
{
get;
init;
}

protected override sealed List<Process> FindProcesses()
{
if (this.ToontownFlavor is ToontownFlavor.ToontownRewritten)
Expand Down Expand Up @@ -67,5 +73,21 @@ protected override sealed List<Process> FindProcesses()
throw new NotSupportedException("Unsupported Toontown flavor: " + this.ToontownFlavor);
}
}

protected override void TransformVirtualKey(ref WindowsEnvironment.VirtualKey key)
{
if (this.UseWasdMovement)
{
// Replace arrow keys with WASD keys.
key = key switch
{
WindowsEnvironment.VirtualKey.Up => WindowsEnvironment.VirtualKey.W,
WindowsEnvironment.VirtualKey.Down => WindowsEnvironment.VirtualKey.S,
WindowsEnvironment.VirtualKey.Left => WindowsEnvironment.VirtualKey.A,
WindowsEnvironment.VirtualKey.Right => WindowsEnvironment.VirtualKey.D,
var other => other
};
}
}
}
}
17 changes: 14 additions & 3 deletions TTMouseclickSimulator/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TTMouseClickSimulator"
mc:Ignorable="d"
Title="AppName" Height="520" Width="542" TextOptions.TextFormattingMode="Display" UseLayoutRounding="True" Closing="HandleWindowClosing">
Title="AppName" Height="540" Width="542" TextOptions.TextFormattingMode="Display" UseLayoutRounding="True" Closing="HandleWindowClosing">
<Grid>
<Button x:Name="btnStart" Content="Start" HorizontalAlignment="Left" Margin="10,0,0,10" Width="75" Height="21" Click="HandleBtnStartClick" VerticalAlignment="Bottom"/>
<Button x:Name="btnStop" IsEnabled="False" Content="Stop" HorizontalAlignment="Left" Margin="110,0,0,10" Width="75" Height="21" Click="HandleBtnStopClick" VerticalAlignment="Bottom"/>
<Label x:Name="lblAppName" Content="AppName" Margin="10,10,10,0" VerticalAlignment="Top" HorizontalAlignment="Left" FontSize="18"/>
<Button x:Name="btnLoad" Content="Load XML Project file…" HorizontalAlignment="Left" Margin="10,50,0,0" VerticalAlignment="Top" Width="154" Height="21" Click="HandleBtnLoadClick"/>
<Grid Margin="10,76,10,60">
<Grid Margin="10,76,10,80">
<Grid x:Name="gridProjectControls">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
Expand Down Expand Up @@ -39,7 +39,18 @@
</Grid>

<TextBlock x:Name="textBlockStopSimulatorNote" Margin="212,0,10,0" TextWrapping="Wrap" Text="Note: To stop the Simulator, press the Windows key." Height="31" VerticalAlignment="Bottom" />
<CheckBox x:Name="chkEnableBackgroundMode" Margin="10,0,0,36" Content="Enable Background Mode" Height="15" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="156" Checked="HandleChkEnableBackgroundModeChecked" Unchecked="HandleChkEnableBackgroundModeUnchecked" ToolTipService.ShowDuration="60000">
<CheckBox x:Name="chkUseWasdMovement" Margin="10,0,0,56" Content="Use WASD instead of array keys for movement" Height="15" VerticalAlignment="Bottom" HorizontalAlignment="Left" ToolTipService.ShowDuration="60000">
<CheckBox.ToolTip>
<ToolTip>
<TextBlock>
Enable this option when you have set W, A, S, D keys instead of array keys for movement in the controls options.
<LineBreak/><LineBreak/>
This will automatically convert arrow keys as specified in project files to WASD keys.
</TextBlock>
</ToolTip>
</CheckBox.ToolTip>
</CheckBox>
<CheckBox x:Name="chkEnableBackgroundMode" Margin="10,0,0,36" Content="Enable Background Mode" Height="15" VerticalAlignment="Bottom" HorizontalAlignment="Left" Checked="HandleChkEnableBackgroundModeChecked" Unchecked="HandleChkEnableBackgroundModeUnchecked" ToolTipService.ShowDuration="60000">
<CheckBox.ToolTip>
<ToolTip>
<TextBlock>
Expand Down
4 changes: 4 additions & 0 deletions TTMouseclickSimulator/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ private async ValueTask RunSimulatorAsync()
this.btnStart.IsEnabled = false;
this.btnStop.IsEnabled = true;
this.btnLoad.IsEnabled = false;
this.chkUseWasdMovement.IsEnabled = false;
this.chkEnableBackgroundMode.IsEnabled = false;

if (this.quickActionButtons is not null)
Expand All @@ -132,6 +133,7 @@ private async ValueTask RunSimulatorAsync()
}

bool backgroundMode = this.chkEnableBackgroundMode.IsChecked is true;
bool useWasdMovement = this.chkUseWasdMovement.IsChecked is true;

this.simulatorStartAction?.Invoke();

Expand All @@ -149,6 +151,7 @@ this.currentQuickAction is not null ?
this.currentQuickAction.Action! :
this.project.Configuration.MainAction!,
environment,
useWasdMovement,
backgroundMode);

// Start a new thread to run the simulator.
Expand Down Expand Up @@ -331,6 +334,7 @@ private void HandleSimulatorStopped()
this.btnStart.IsEnabled = true;
this.btnStop.IsEnabled = false;
this.btnLoad.IsEnabled = true;
this.chkUseWasdMovement.IsEnabled = true;
this.chkEnableBackgroundMode.IsEnabled = true;

if (this.quickActionButtons is not null)
Expand Down

0 comments on commit 951b67d

Please sign in to comment.