Skip to content

Commit

Permalink
Merge pull request #61 from pre-martin/feature/long-button-press
Browse files Browse the repository at this point in the history
Support for long button press.
  • Loading branch information
pre-martin authored Feb 26, 2023
2 parents 36fadb8 + 8f4bdd0 commit 7ebd115
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
23 changes: 22 additions & 1 deletion StreamDeckSimHub.Plugin/Actions/HotkeyBaseAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public abstract class HotkeyBaseAction<TSettings> : StreamDeckAction<TSettings>,
private Keyboard.ScanCodeShort? _scs;
private int _state;
private PropertyChangedArgs? _lastPropertyChangedEvent;
private bool _simHubTriggerActive;

protected HotkeyBaseAction(SimHubConnection simHubConnection)
{
Expand All @@ -44,6 +45,14 @@ protected override async Task OnWillDisappear(ActionEventArgs<AppearancePayload>
Logger.LogInformation("OnWillDisappear: {settings}", HotkeySettings);
await Unsubscribe();

// Just to be sure that there are no dangling input triggers. Actually we should not reach this code.
if (_simHubTriggerActive)
{
Logger.LogWarning("SimHub trigger still active. Sending \"released\" command");
_simHubTriggerActive = false;
await SimHubConnection.SendTriggerInputReleased(HotkeySettings.SimHubControl);
}

await base.OnWillDisappear(args);
}

Expand Down Expand Up @@ -88,17 +97,29 @@ protected override async Task OnKeyDown(ActionEventArgs<KeyPayload> args)
if (_vks.HasValue && _scs.HasValue) Keyboard.KeyDown(_vks.Value, _scs.Value);
// SimHubControl
if (!string.IsNullOrWhiteSpace(HotkeySettings.SimHubControl))
await SimHubConnection.SendTriggerInput(HotkeySettings.SimHubControl);
{
_simHubTriggerActive = true;
await SimHubConnection.SendTriggerInputPressed(HotkeySettings.SimHubControl);
}

await base.OnKeyDown(args);
}

protected override async Task OnKeyUp(ActionEventArgs<KeyPayload> args)
{
// Hotkey
if (_vks.HasValue && _scs.HasValue) Keyboard.KeyUp(_vks.Value, _scs.Value);
if (HotkeySettings.Ctrl) Keyboard.KeyUp(Keyboard.VirtualKeyShort.LCONTROL, Keyboard.ScanCodeShort.LCONTROL);
if (HotkeySettings.Alt) Keyboard.KeyUp(Keyboard.VirtualKeyShort.LMENU, Keyboard.ScanCodeShort.LMENU);
if (HotkeySettings.Shift) Keyboard.KeyUp(Keyboard.VirtualKeyShort.LSHIFT, Keyboard.ScanCodeShort.LSHIFT);
// SimHubControl
if (!string.IsNullOrWhiteSpace(HotkeySettings.SimHubControl))
{
// Let's hope that nobody changed the settings since the "pressed" command...
_simHubTriggerActive = false;
await SimHubConnection.SendTriggerInputReleased(HotkeySettings.SimHubControl);
}

// Stream Deck always toggles the state for each keypress (at "key up", to be precise). So we have to set the
// state again to the correct one, after Stream Deck has done its toggling stuff.
await SetStateAsync(_state);
Expand Down
30 changes: 29 additions & 1 deletion StreamDeckSimHub.Plugin/Actions/InputAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class InputAction : StreamDeckAction<InputSettings>
{
private readonly SimHubConnection _simHubConnection;
private InputSettings _inputSettings;
private bool _simHubTriggerActive;

public InputAction(SimHubConnection simHubConnection)
{
Expand All @@ -32,6 +33,19 @@ protected override async Task OnWillAppear(ActionEventArgs<AppearancePayload> ar
await base.OnWillAppear(args);
}

protected override async Task OnWillDisappear(ActionEventArgs<AppearancePayload> args)
{
// Just to be sure that there are no dangling input triggers. Actually we should not reach this code.
if (_simHubTriggerActive)
{
Logger.LogWarning("SimHub trigger still active. Sending \"released\" command");
_simHubTriggerActive = false;
await _simHubConnection.SendTriggerInputReleased(_inputSettings.SimHubControl);
}

await base.OnWillDisappear(args);
}

protected override async Task OnDidReceiveSettings(ActionEventArgs<ActionPayload> args, InputSettings settings)
{
Logger.LogInformation(
Expand All @@ -44,11 +58,25 @@ protected override async Task OnDidReceiveSettings(ActionEventArgs<ActionPayload
protected override async Task OnKeyDown(ActionEventArgs<KeyPayload> args)
{
if (!string.IsNullOrWhiteSpace(_inputSettings.SimHubControl))
await _simHubConnection.SendTriggerInput(_inputSettings.SimHubControl);
{
_simHubTriggerActive = true;
await _simHubConnection.SendTriggerInputPressed(_inputSettings.SimHubControl);
}

await base.OnKeyDown(args);
}

protected override async Task OnKeyUp(ActionEventArgs<KeyPayload> args)
{
if (!string.IsNullOrWhiteSpace(_inputSettings.SimHubControl))
{
_simHubTriggerActive = false;
await _simHubConnection.SendTriggerInputReleased(_inputSettings.SimHubControl);
}

await base.OnKeyUp(args);
}

private void SetSettings(InputSettings settings)
{
this._inputSettings = settings;
Expand Down
9 changes: 7 additions & 2 deletions StreamDeckSimHub.Plugin/SimHub/SimHubConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,14 @@ internal async Task Unsubscribe(string propertyName, IPropertyChangedReceiver pr
}
}

internal async Task SendTriggerInput(string inputName)
internal async Task SendTriggerInputPressed(string inputName)
{
await WriteToServer($"trigger-input {inputName}");
await WriteToServer($"trigger-input-pressed {inputName}");
}

internal async Task SendTriggerInputReleased(string inputName)
{
await WriteToServer($"trigger-input-released {inputName}");
}

private async Task SendSubscribe(string propertyName)
Expand Down
2 changes: 2 additions & 0 deletions doc/ShakeIt.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ image::ShakeIt-Bass-Assign.png[Assign controls]
image::ShakeIt-Bass-Assign-Mute.png[Assign mute control]
. Repeat the assignment for "Increment gain" and "Decrement gain"

The plugin supports long button press events. Just hold the Stream Deck button and the gain will increment or decrement gradually. If you set the step size in SimHub down to 1%, you will notice that SimHub stops after 80 steps. This is something that is built into SimHub and happens with other input plugins as well.

=== Bonus: Toggle whole Stream Deck column

In the very first screenshot, you can see that my three "Audi" buttons are greyed out, because the effect group is muted. This can be achieved as follows:
Expand Down

0 comments on commit 7ebd115

Please sign in to comment.