Skip to content

Commit fe870ea

Browse files
author
Neurs
committed
🛠️ Remove interger calculations, only comparing sorted sets.
1 parent 1b0eea9 commit fe870ea

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

HotkeysLoad.cs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ internal partial class HotkeysLoad
1111
private static readonly List<IntPtr> yeetedWindows = [];
1212
private static readonly List<IntPtr> enumWindowsResult = [];
1313

14-
private static readonly List<int> modifierKeys = [
15-
(int)VirtualKey.LeftControl,
16-
(int)VirtualKey.RightControl,
17-
(int)VirtualKey.Control,
18-
(int)VirtualKey.LeftShift,
19-
(int)VirtualKey.RightShift,
20-
(int)VirtualKey.Shift,
21-
(int)VirtualKey.LeftMenu,
22-
(int)VirtualKey.RightMenu,
23-
(int)VirtualKey.Menu,
24-
(int)VirtualKey.LeftWindows,
25-
(int)VirtualKey.RightWindows];
26-
27-
private static readonly HashSet<int> currentKeys = [];
28-
private static readonly List<int> assignedHotkeys = [];
14+
private static readonly List<VirtualKey> modifierKeys = [
15+
VirtualKey.LeftControl,
16+
VirtualKey.RightControl,
17+
VirtualKey.Control,
18+
VirtualKey.LeftShift,
19+
VirtualKey.RightShift,
20+
VirtualKey.Shift,
21+
VirtualKey.LeftMenu,
22+
VirtualKey.RightMenu,
23+
VirtualKey.Menu,
24+
VirtualKey.LeftWindows,
25+
VirtualKey.RightWindows];
26+
27+
private static readonly SortedSet<VirtualKey> currentKeys = [];
28+
private static readonly List<SortedSet<VirtualKey>> assignedHotkeys = [];
2929
private static HotkeyEvent pointedEvent = delegate { };
3030

3131
public delegate void HotkeyEvent(int Id);
@@ -39,19 +39,17 @@ public static void RegisterHotkeys(HotkeyEvent hotkeyEvent)
3939
KeyboardHookHandler.SetupHook(DownKeyEvent, UpKeyEvent);
4040
}
4141

42-
private static bool DownKeyEvent(int key)
42+
private static bool DownKeyEvent(VirtualKey key)
4343
{
44-
int index = assignedHotkeys.IndexOf(key);
44+
int index = assignedHotkeys.FindIndex(set => set.SetEquals([key]));
4545
if (index != -1 && !currentKeys.Intersect(modifierKeys).Any())
4646
{
4747
pointedEvent(index);
4848
return true;
4949
}
5050

5151
currentKeys.Add(key);
52-
53-
int combined = currentKeys.Sum();
54-
index = assignedHotkeys.IndexOf(combined);
52+
index = assignedHotkeys.FindIndex(set => set.SetEquals(currentKeys));
5553

5654
if (index != -1)
5755
{
@@ -62,7 +60,7 @@ private static bool DownKeyEvent(int key)
6260
return false;
6361
}
6462

65-
private static bool UpKeyEvent(int key)
63+
private static bool UpKeyEvent(VirtualKey key)
6664
{
6765
currentKeys.Remove(key);
6866
return false;
@@ -140,10 +138,10 @@ private static void ConfigParse()
140138
}
141139
private static void InitHotkey(string name, JObject config)
142140
{
143-
int hotkey = 0;
141+
SortedSet<VirtualKey> hotkey = [];
144142
foreach (string? key in config[name]!.Select(v => (string?)v))
145143
{
146-
hotkey += (int)Enum.Parse<VirtualKey>(key!);
144+
hotkey.Add(Enum.Parse<VirtualKey>(key!));
147145
}
148146

149147
assignedHotkeys.Add(hotkey);

KeyboardHookHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static void RemoveHook()
3333
UnhookWindowsHookEx(_hookID);
3434
}
3535

36-
public delegate bool KeyCallbackEvent(int key);
36+
public delegate bool KeyCallbackEvent(VirtualKey key);
3737

3838
public delegate IntPtr LowLevelKeyboardProc(
3939
int nCode, IntPtr wParam, IntPtr lParam);
@@ -47,11 +47,11 @@ private static IntPtr HookCallback(
4747
{
4848
if (wParam == WM_KEYDOWN)
4949
{
50-
res = keydownEvent(Marshal.ReadInt32(lParam));
50+
res = keydownEvent((VirtualKey)Marshal.ReadInt32(lParam));
5151
}
5252
if (wParam == WM_KEYUP)
5353
{
54-
res = keyupEvent(Marshal.ReadInt32(lParam));
54+
res = keyupEvent((VirtualKey)Marshal.ReadInt32(lParam));
5555
}
5656
}
5757
return res ? 1 : CallNextHookEx(_hookID, nCode, wParam, lParam);

0 commit comments

Comments
 (0)