Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for detection of long press #77

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions StreamDeckSimHub.Plugin/Actions/ShortAndLongPressHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ShortAndLongPressHandler
private TimeSpan LongPressTimeSpan { get; }
private Func<ActionEventArgs<KeyPayload>, Task> OnShortPress { get; }
private Func<ActionEventArgs<KeyPayload>, Task> OnLongPress { get; }
private readonly CancellationTokenSource _cancellationTokenSource = new();
private CancellationTokenSource? _cancellationTokenSource;

public ShortAndLongPressHandler(
Func<ActionEventArgs<KeyPayload>, Task> onShortPress,
Expand Down Expand Up @@ -46,6 +46,7 @@ public Task KeyDown(ActionEventArgs<KeyPayload> args)
try
{
var me = this;
_cancellationTokenSource = new();
await Task.Delay(LongPressTimeSpan, _cancellationTokenSource.Token);
await me.TryHandlePress(OnLongPress);
}
Expand All @@ -68,7 +69,7 @@ private async Task TryHandlePress(Func<ActionEventArgs<KeyPayload>, Task> handle
{
if (KeyPressStack.TryPop(out var result))
{
_cancellationTokenSource.Cancel();
_cancellationTokenSource?.Cancel();
await handler(result);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ public async Task TestShortShort()
cb.AssertAndReset(true, false);
}

[Test]
public async Task TestLongLong()
{
// Long+Long to test that the delay timer works several times

var cb = new CallbackHolder();
var handler = new ShortAndLongPressHandler(_timeSpan, cb.OnShortPress, cb.OnLongPress);

await handler.KeyDown(new ActionEventArgs<KeyPayload>());
await Task.Delay(_timeSpanLonger);
await handler.KeyUp();

cb.AssertAndReset(false, true);

await handler.KeyDown(new ActionEventArgs<KeyPayload>());
await Task.Delay(_timeSpanLonger);
await handler.KeyUp();

cb.AssertAndReset(false, true);
}

private class CallbackHolder
{
private bool _shortWasCalled;
Expand Down