Skip to content

Commit

Permalink
UI: Set max width of application
Browse files Browse the repository at this point in the history
UI: SimulatedKeystrokesDialog: Add crosshair icon in prep for selecting applications by crosshair
KBM: StickyRepeatService, EventSimulatorService: Create and pass a cancellation token to cancel tap keys quicker
KBM: StickyRepeatService, EventSimulatorService: Move repeat delay into TapKeys method to properly time each individual keystroke
  • Loading branch information
FaithBeam committed May 12, 2024
1 parent 6ef632e commit 79c4bc7
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SharpHook.Native;
using System.Threading;
using SharpHook.Native;
using YMouseButtonControl.Core.KeyboardAndMouse.Models;

namespace YMouseButtonControl.Core.KeyboardAndMouse.Interfaces;
Expand Down Expand Up @@ -31,4 +32,11 @@ public interface IEventSimulatorService
void TapKeys(string? keys);
void SimulateMousePress(MouseButton mb);
void SimulateMouseRelease(MouseButton mb);

/// <summary>
/// TODO
/// </summary>
/// <param name="keys"></param>
/// <param name="delay">Optional delay between key presses</param>
void TapKeys(string? keys, int delay, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,84 @@ public void TapKeys(string? keys)
}
}

/// <summary>
/// TODO
/// </summary>
/// <param name="keys"></param>
/// <param name="delay">Optional delay between key presses</param>
/// <param name="cancellationToken"></param>
public void TapKeys(string? keys, int delay, CancellationToken cancellationToken)
{
var parsed = ParseKeys(keys);
var stack = new Stack<ParsedKey>();

foreach (var pk in parsed)
{
if (cancellationToken.IsCancellationRequested)
{
_logger.Information("========STOPPING TAP KEYS===========");
return;
}
if (delay > -1)
{
Thread.Sleep(delay);
}

// Pop the entire stack if the last key pressed is a normal key
if (stack.TryPeek(out var peekPk) && !peekPk.IsModifier)
{
while (stack.TryPop(out var poppedPk))
{
switch (poppedPk.Value)
{
case <= 5:
SimulateMouseRelease((MouseButton)poppedPk.Value);
break;
case >= (int)KeyCode.VcEscape:
SimulateKeyRelease(poppedPk.Key);
break;
default:
throw new Exception($"Unknown key value {poppedPk.Value}");
}
}
}

stack.Push(pk);
switch (pk.Value)
{
case <= (ushort)MouseButton.Button5:
SimulateMousePress((MouseButton)pk.Value);
break;
case >= (int)KeyCode.VcEscape:
SimulateKeyPress(pk.Key);
break;
default:
throw new Exception($"Unknown key value {pk.Value}");
}
}

// Release any remaining keys in the stack
while (stack.TryPop(out var poppedPk))
{
if (cancellationToken.IsCancellationRequested)
{
_logger.Information("========STOPPING TAP KEYS===========");
return;
}
switch (poppedPk.Value)
{
case <= 5:
SimulateMouseRelease((MouseButton)poppedPk.Value);
break;
case >= (int)KeyCode.VcEscape:
SimulateKeyRelease(poppedPk.Key);
break;
default:
throw new Exception($"Unknown key value {poppedPk.Value}");
}
}
}

/// <summary>
/// Splits a string of characters into a list of strings. Words surrounded by {} will be added as the whole word.
/// For example, {shift} will be "shift" in the list.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading;
using Serilog;
using YMouseButtonControl.Core.DataAccess.Models.Interfaces;
using YMouseButtonControl.Core.KeyboardAndMouse.Enums;
using YMouseButtonControl.Core.KeyboardAndMouse.Interfaces;
Expand All @@ -12,6 +13,8 @@ public class StickyRepeatService(IEventSimulatorService eventSimulatorService)
private bool _shouldStop;
private readonly object _lock = new();
private const int RepeatRateMs = 33;
private CancellationTokenSource? _cts;
private readonly ILogger _log = Log.Logger.ForContext<StickyRepeatService>();

public void StickyRepeat(IButtonMapping mapping, MouseButtonState state)
{
Expand All @@ -28,6 +31,8 @@ public void StickyRepeat(IButtonMapping mapping, MouseButtonState state)
{
lock (_lock)
{
_log.Information("=====CANCELATION REQUESTED=======");
_cts?.Cancel();
_shouldStop = true;
}
_thread.Join();
Expand All @@ -44,6 +49,7 @@ public void StickyRepeat(IButtonMapping mapping, MouseButtonState state)

private void StartThread(IButtonMapping mapping)
{
_cts = new CancellationTokenSource();
_thread = new Thread(() =>
{
while (true)
Expand All @@ -52,12 +58,13 @@ private void StartThread(IButtonMapping mapping)
{
if (_shouldStop)
{
_log.Information("=====CANCELATION REQUESTED=======");
_cts.Cancel();
break;
}
}

Thread.Sleep(RepeatRateMs);
eventSimulatorService.TapKeys(mapping.Keys);
eventSimulatorService.TapKeys(mapping.Keys, RepeatRateMs, _cts.Token);
}
});

Expand Down
Binary file added YMouseButtonControl/Resources/crosshair.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,14 @@
<TextBox Grid.Column="1" Text="{Binding Description}"></TextBox>
</Grid>

<Grid Grid.Row="6" ColumnDefinitions="Auto,Auto,Auto,Auto" HorizontalAlignment="Right">
<Grid Grid.Row="6" ColumnDefinitions="Auto,Auto,Auto,Auto,Auto" HorizontalAlignment="Right">
<Label Content="Cursor Position: X,Y" />
<TextBox Grid.Column="1" Text="{Binding ComputedXy}"></TextBox>
<Button Grid.Column="2" Content="OK" Command="{Binding OkCommand}" />
<Button Grid.Column="3" Content="Cancel" Click="Button_OnClick"></Button>
<TextBox Grid.Column="1" Text="{Binding ComputedXy}" />
<Button Grid.Column="2">
<Image Source="../../Resources/crosshair.png" Width="16" Height="16" />
</Button>
<Button Grid.Column="3" Content="OK" Command="{Binding OkCommand}" />
<Button Grid.Column="4" Content="Cancel" Click="Button_OnClick" />
</Grid>
</Grid>
</Window>
2 changes: 1 addition & 1 deletion YMouseButtonControl/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Width="800"
Height="700"
CanResize="False"
SizeToContent="WidthAndHeight"
SizeToContent="Height"
x:Class="YMouseButtonControl.Views.MainWindow"
Title="YMouseButtonControl"
Icon="/Resources/mouse.ico">
Expand Down
2 changes: 2 additions & 0 deletions YMouseButtonControl/YMouseButtonControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
<AvaloniaResource Include="Resources\gear_icon1.png" />
<None Remove="Resources\mouse.ico" />
<AvaloniaResource Include="Resources\mouse.ico" />
<None Remove="Resources\crosshair.png" />
<AvaloniaResource Include="Resources\crosshair.png" />
</ItemGroup>
<ItemGroup>
<!--This helps with theme dll-s trimming.
Expand Down

0 comments on commit 79c4bc7

Please sign in to comment.