Skip to content
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
2 changes: 2 additions & 0 deletions src/SoundType.Input/KeyboardHookService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ public void Stop()
{
if (_hookId == IntPtr.Zero)
{
_pressedKeys.Clear();
return;
}

_ = _platform.Unhook(_hookId);
_hookId = IntPtr.Zero;
_pressedKeys.Clear();
}

private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
Expand Down
20 changes: 20 additions & 0 deletions src/SoundType.Tests/KeyboardHookServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ public void KeyPressed_MarksSecondKeydownAsRepeatUntilKeyup()
});
}

[Fact]
public void Stop_ClearsPressedKeysSoRestartDoesNotMarkFreshKeydownAsRepeat()
{
FakeKeyboardHookPlatform platform = new();
using KeyboardHookService service = new(platform);
List<KeyPressedEvent> events = [];
service.KeyPressed += (_, e) => events.Add(e);

service.Start();
platform.Send(WmKeydown, AKey);
service.Stop();
service.Start();
platform.Send(WmKeydown, AKey);

Assert.Collection(
events,
first => Assert.False(first.IsRepeat),
afterRestart => Assert.False(afterRestart.IsRepeat));
}

private sealed class FakeKeyboardHookPlatform : IKeyboardHookPlatform
{
private KeyboardHookService.LowLevelKeyboardProc? _callback;
Expand Down
Loading