GeekyEggo / SharpDeck

A .NET wrapper for the official Elgato Stream Deck SDK.
MIT License
25 stars 9 forks source link

Fast short keypresses trigger OnKeyLongPress #18

Open pre-martin opened 11 months ago

pre-martin commented 11 months ago

When a button is pressed repeatedly and faster than LongKeyPressInterval, the implementation of StreamDeckAction triggers falsely OnKeyLongPress.

The flow is as follows:

  1. Button is pressed
  2. OnKeyDown() puts the EventArgs on the stack and starts Task.Delay()
  3. Button is released
  4. OnKeyUp() successfully pops the EventArgs from the stack and thus calls OnKeyPress() (the callback for short keypress).
  5. Problem: The Task.Delay() is still active and waiting.
  6. Button is pressed again. This has to happen with 500ms after the first time that the button was pressed (or <= LongKeyPressInterval)
  7. OnKeyDown() puts the EventArgs on the stack
  8. Task.Delay() from the first button press finishes and successfully pops an event from the stack. Unfortunately, this is the event from the second button press and not from the first button press. In consequence, the callback OnKeyLongPresss() is getting called.

I think that TryHandleKeyPress() must also cancel Task.Delay() when it successfully popped an item from the stack.

pre-martin commented 11 months ago

Fix is easy by using a CancellationTokenSource. See https://github.com/pre-martin/StreamDeckSimHubPlugin/pull/76/files#diff-1e758b02ba044ebded4f7f775722fadaa5cceae3fda25bf1e4f09a43565244b8R46-R55 for a solution. The Delay is cancelled in TryHandlePress()

pre-martin commented 11 months ago

The CancelationTokenSource has to be recreated. This has to be added to the fix from above: https://github.com/pre-martin/StreamDeckSimHubPlugin/pull/77/files