mirror of
https://github.com/whoswhip/Spectrum.git
synced 2026-12-06 01:28:42 +00:00
refactor: simplify keybind checking
This commit is contained in:
@@ -206,22 +206,43 @@ namespace Spectrum.Input
|
|||||||
|
|
||||||
Action<MakcuMouseButton, bool>? makcuHandler = null;
|
Action<MakcuMouseButton, bool>? makcuHandler = null;
|
||||||
bool subscribedToMakcu = false;
|
bool subscribedToMakcu = false;
|
||||||
|
|
||||||
|
void UnsubscribeMakcu()
|
||||||
|
{
|
||||||
|
if (subscribedToMakcu && makcuHandler != null && MakcuMain.MakcuInstance != null)
|
||||||
|
{
|
||||||
|
try { MakcuMain.MakcuInstance.ButtonStateChanged -= makcuHandler; } catch { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (mainConfig.Data.MovementMethod == MovementMethod.Makcu && EnsureMakcuReady() && MakcuMain.MakcuInstance != null)
|
if (mainConfig.Data.MovementMethod == MovementMethod.Makcu && EnsureMakcuReady() && MakcuMain.MakcuInstance != null)
|
||||||
{
|
{
|
||||||
makcuHandler = (btn, isPressed) =>
|
makcuHandler = (btn, isPressed) =>
|
||||||
{
|
{
|
||||||
if (!isPressed) return;
|
if (!isPressed) return;
|
||||||
var key = MapMakcuButtonToKeys(btn);
|
var key = makcuToKeysMap.GetValueOrDefault(btn);
|
||||||
if (key.HasValue)
|
if (key != Keys.None)
|
||||||
{
|
{
|
||||||
tcs.TrySetResult(key.Value);
|
tcs.TrySetResult(key);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
MakcuMain.MakcuInstance.ButtonStateChanged += makcuHandler;
|
MakcuMain.MakcuInstance.ButtonStateChanged += makcuHandler;
|
||||||
subscribedToMakcu = true;
|
subscribedToMakcu = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
var thread = new Thread(() =>
|
CancellationTokenRegistration ctr = default;
|
||||||
|
if (cancellationToken?.CanBeCanceled == true)
|
||||||
|
{
|
||||||
|
ctr = cancellationToken.Value.Register(() => tcs.TrySetCanceled());
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = tcs.Task.ContinueWith(_ =>
|
||||||
|
{
|
||||||
|
try { ctr.Dispose(); } catch {}
|
||||||
|
UnsubscribeMakcu();
|
||||||
|
}, TaskScheduler.Default);
|
||||||
|
|
||||||
|
Task.Run(() =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -231,14 +252,6 @@ namespace Spectrum.Input
|
|||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (int key = 0x08; key <= 0xFE; key++)
|
|
||||||
{
|
|
||||||
if ((GetAsyncKeyState(key) & 0x8000) != 0)
|
|
||||||
{
|
|
||||||
tcs.TrySetResult((Keys)key);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var mouseKey in MouseKeys)
|
foreach (var mouseKey in MouseKeys)
|
||||||
{
|
{
|
||||||
@@ -249,6 +262,15 @@ namespace Spectrum.Input
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (int key = 0x08; key <= 0xFE; key++)
|
||||||
|
{
|
||||||
|
if ((GetAsyncKeyState(key) & 0x8000) != 0)
|
||||||
|
{
|
||||||
|
tcs.TrySetResult((Keys)key);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (cancellationToken?.IsCancellationRequested == true)
|
if (cancellationToken?.IsCancellationRequested == true)
|
||||||
{
|
{
|
||||||
tcs.TrySetCanceled();
|
tcs.TrySetCanceled();
|
||||||
@@ -259,23 +281,10 @@ namespace Spectrum.Input
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (subscribedToMakcu && makcuHandler != null && MakcuMain.MakcuInstance != null)
|
UnsubscribeMakcu();
|
||||||
{
|
|
||||||
try { MakcuMain.MakcuInstance.ButtonStateChanged -= makcuHandler; } catch { }
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
})
|
|
||||||
{ IsBackground = true };
|
|
||||||
|
|
||||||
tcs.Task.ContinueWith(_ =>
|
|
||||||
{
|
|
||||||
if (subscribedToMakcu && makcuHandler != null && MakcuMain.MakcuInstance != null)
|
|
||||||
{
|
|
||||||
try { MakcuMain.MakcuInstance.ButtonStateChanged -= makcuHandler; } catch { }
|
|
||||||
}
|
|
||||||
}, TaskScheduler.Default);
|
|
||||||
|
|
||||||
thread.Start();
|
|
||||||
return tcs.Task;
|
return tcs.Task;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -300,35 +309,25 @@ namespace Spectrum.Input
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private static Dictionary<MakcuMouseButton, Keys> makcuToKeysMap = new()
|
||||||
private static Keys? MapMakcuButtonToKeys(MakcuMouseButton btn)
|
|
||||||
{
|
{
|
||||||
return btn switch
|
{ MakcuMouseButton.Left, Keys.LButton },
|
||||||
{
|
{ MakcuMouseButton.Right, Keys.RButton },
|
||||||
MakcuMouseButton.Left => Keys.LButton,
|
{ MakcuMouseButton.Middle, Keys.MButton },
|
||||||
MakcuMouseButton.Right => Keys.RButton,
|
{ MakcuMouseButton.Mouse4, Keys.XButton1 },
|
||||||
MakcuMouseButton.Middle => Keys.MButton,
|
{ MakcuMouseButton.Mouse5, Keys.XButton2 }
|
||||||
MakcuMouseButton.Mouse4 => Keys.XButton1,
|
|
||||||
MakcuMouseButton.Mouse5 => Keys.XButton2,
|
|
||||||
_ => null
|
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
public static bool IsKeyOrMouseDown(Keys key)
|
public static bool IsKeyOrMouseDown(Keys key)
|
||||||
{
|
{
|
||||||
if (mainConfig.Data.MovementMethod == MovementMethod.Makcu && MakcuMain.MakcuInstance?.IsInitializedAndConnected == true)
|
if (mainConfig.Data.MovementMethod == MovementMethod.Makcu && MakcuMain.MakcuInstance?.IsInitializedAndConnected == true)
|
||||||
{
|
{
|
||||||
var makcuBtn = MapKeysToMakcuButton(key);
|
var makcuBtn = makcuToKeysMap.FirstOrDefault(x => x.Value == key).Key;
|
||||||
if (makcuBtn.HasValue)
|
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var states = MakcuMain.MakcuInstance.GetCurrentButtonStates();
|
var states = MakcuMain.MakcuInstance.GetCurrentButtonStates();
|
||||||
|
return states.TryGetValue(makcuBtn, out var pressed) && pressed;
|
||||||
return states.TryGetValue(makcuBtn.Value, out var pressed) && pressed;
|
} catch {}
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return (GetAsyncKeyState((int)key) & 0x8000) != 0; ;
|
return (GetAsyncKeyState((int)key) & 0x8000) != 0; ;
|
||||||
}
|
}
|
||||||
@@ -362,18 +361,5 @@ namespace Spectrum.Input
|
|||||||
|
|
||||||
return _keybindToggleStates[toggleKey];
|
return _keybindToggleStates[toggleKey];
|
||||||
}
|
}
|
||||||
|
|
||||||
private static MakcuMouseButton? MapKeysToMakcuButton(Keys key)
|
|
||||||
{
|
|
||||||
return key switch
|
|
||||||
{
|
|
||||||
Keys.LButton => MakcuMouseButton.Left,
|
|
||||||
Keys.RButton => MakcuMouseButton.Right,
|
|
||||||
Keys.MButton => MakcuMouseButton.Middle,
|
|
||||||
Keys.XButton1 => MakcuMouseButton.Mouse4,
|
|
||||||
Keys.XButton2 => MakcuMouseButton.Mouse5,
|
|
||||||
_ => null
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user