Skip to content

Commit

Permalink
fixed input manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Valkirie committed Jun 22, 2022
1 parent 0277b4f commit 26d5b1b
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions HandheldCompanion/Managers/InputsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class InputsManager
private bool TriggerLock;
private string TriggerListener = string.Empty;
private List<KeyEventArgsExt> TriggerBuffer = new();
private List<KeyEventArgsExt> Intercepted = new();

private Dictionary<string, bool> Triggered = new Dictionary<string, bool>();

Expand Down Expand Up @@ -119,6 +120,11 @@ private void M_GlobalHook_KeyEvent(object? sender, KeyEventArgs e)

if (Enumerable.SequenceEqual(chord_keys, buffer_keys))
{
Intercepted.Clear();
Intercepted.AddRange(TriggerBuffer);

TriggerBuffer.Clear();

long time_last = TIME_BURST;
long time_duration = time_last;

Expand All @@ -132,44 +138,42 @@ private void M_GlobalHook_KeyEvent(object? sender, KeyEventArgs e)
time_last = args.Timestamp - prevKeyUp[listener];
prevKeyUp[listener] = args.Timestamp;
}

// skip call if too close
if (time_last < TIME_BURST)
break;

// only send trigger on release
if (!args.IsKeyUp)
break;

time_duration = args.Timestamp - prevKeyDown[listener];
if (time_duration > TIME_LONG)
listener += " (HOLD)";

LogManager.LogDebug("Success: {0} at {1}", listener, args.Timestamp);
LogManager.LogDebug("Triggered: {0} at {1}", listener, args.Timestamp);

if (string.IsNullOrEmpty(TriggerListener))
{
string trigger = GetTriggerFromName(listener);

// trigger isn't used
if (string.IsNullOrEmpty(trigger))
{
TriggerBuffer.AddRange(Intercepted);
break;

TriggerBuffer.Clear();
TriggerRaised?.Invoke(trigger, Triggers[trigger]);

return;
}else if (args.IsKeyUp)
TriggerRaised?.Invoke(trigger, Triggers[trigger]);
}
else
{
TriggerInputs inputs = new TriggerInputs(TriggerInputsType.Keyboard, string.Join(",", chord.Keys), listener);
Triggers[TriggerListener] = inputs;

TriggerBuffer.Clear();
TriggerUpdated?.Invoke(TriggerListener, inputs);
TriggerListener = string.Empty;

return;
if (args.IsKeyUp)
{
TriggerUpdated?.Invoke(TriggerListener, inputs);
TriggerListener = string.Empty;
}
}

return;
}
}

Expand All @@ -178,7 +182,7 @@ private void M_GlobalHook_KeyEvent(object? sender, KeyEventArgs e)

private string GetTriggerFromName(string name)
{
foreach(var pair in Triggers)
foreach (var pair in Triggers)
{
string p_trigger = pair.Key;
string p_name = pair.Value.name;
Expand Down Expand Up @@ -241,7 +245,7 @@ private void ReleaseBuffer()
// clear buffer
TriggerBuffer.Clear();
}
catch(Exception)
catch (Exception)
{
}
}
Expand All @@ -250,13 +254,14 @@ private List<KeyCode> GetBufferKeys()
{
List<KeyCode> keys = new List<KeyCode>();

foreach(KeyEventArgsExt e in TriggerBuffer)
foreach (KeyEventArgsExt e in TriggerBuffer)
keys.Add((KeyCode)e.KeyValue);

return keys;
}

public void Start() {
public void Start()
{
foreach (var pair in Triggers)
Triggered[pair.Key] = false;

Expand All @@ -268,7 +273,7 @@ public void Start() {
prevKeyUp[listener] = TIME_BURST;
prevKeyDown[listener] = TIME_BURST;
}

UpdateTimer.Start();

m_GlobalHook.KeyDown += M_GlobalHook_KeyEvent;
Expand Down Expand Up @@ -346,12 +351,14 @@ public void StartListening(string listener)
TriggerListener = listener;
TriggerBuffer = new();

// reset trigger
Triggers[TriggerListener].buttons = 0;
Triggers[TriggerListener].name = "";
}

public void UpdateController(ControllerEx controllerEx)
{
this.controllerEx = controllerEx;
}
}
}
}

0 comments on commit 26d5b1b

Please sign in to comment.