Skip to content

Commit d47343c

Browse files
committed
fix unregister hotkey not properly recognising single and combo of same key
1 parent 0ea7523 commit d47343c

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

ChefKeys/ChefKeysManager.cs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static void RegisterHotkey(string hotkeys, string previousHotkey, Action<
5959
hotkeys = ConvertIncorrectKeyString(hotkeys);
6060
previousHotkey = ConvertIncorrectKeyString(previousHotkey);
6161

62-
UnregisterHotkey(hotkeys, previousHotkey, action);
62+
UnregisterHotkey(previousHotkey);
6363

6464
// The released key need to be the unique key in the dictionary.
6565
// The last key in the combo is the release key that triggers action
@@ -98,34 +98,29 @@ public static void RegisterHotkey(string hotkeys, string previousHotkey, Action<
9898
registeredHotkeys.Add(ToKeyCode(keys.First()), keyRecord);
9999
}
100100

101-
public static void UnregisterHotkey(string hotkey, string previousHotkey, Action<string> action)
101+
public static void UnregisterHotkey(string hotkey)
102102
{
103-
var hotkeyToCheck = hotkey;
104-
105-
if (!registeredHotkeys.TryGetValue(ToKeyCode(SplitHotkeyReversed(hotkeyToCheck).First()), out var existingKeyRecord))
106-
{
107-
hotkeyToCheck= previousHotkey;
108-
if (!registeredHotkeys.TryGetValue(ToKeyCode(SplitHotkeyReversed(hotkeyToCheck).First()), out var existingPrevKeyRecord))
109-
return;
103+
if (!registeredHotkeys.TryGetValue(ToKeyCode(SplitHotkeyReversed(hotkey).First()), out var existingKeyRecord))
104+
return;
110105

111-
existingKeyRecord = existingPrevKeyRecord;
112-
}
106+
if (!existingKeyRecord.KeyComboRecords.Exists(x => x.comboRaw == hotkey))
107+
return;
113108

114-
if (existingKeyRecord.isSingleKeyRegistered && !existingKeyRecord.AreKeyCombosRegistered() && !hotkeyToCheck.Contains('+'))
109+
if (!hotkey.Contains('+'))
115110
{
116111
existingKeyRecord.action -= existingKeyRecord.action;
117-
registeredHotkeys.Remove(existingKeyRecord.vk_code);
118-
return;
119-
}
112+
existingKeyRecord.isSingleKeyRegistered = false;
120113

121-
var comboRecord = existingKeyRecord.KeyComboRecords.FirstOrDefault(x => x.comboRaw == hotkeyToCheck);
114+
if (!existingKeyRecord.AreKeyCombosRegistered())
115+
registeredHotkeys.Remove(existingKeyRecord.vk_code);
122116

123-
// There is a single key press still registered, no need to remove anything from registeredHotkeys.
124-
if (comboRecord is null)
125117
return;
118+
}
126119

120+
var comboRecord = existingKeyRecord.KeyComboRecords.FirstOrDefault(x => x.comboRaw == hotkey);
127121
comboRecord.action -= comboRecord.action;
128-
existingKeyRecord.KeyComboRecords.RemoveAll(x => x.comboRaw == hotkeyToCheck);
122+
existingKeyRecord.KeyComboRecords.RemoveAll(x => x.comboRaw == hotkey);
123+
129124
if (!existingKeyRecord.isSingleKeyRegistered && !existingKeyRecord.AreKeyCombosRegistered())
130125
registeredHotkeys.Remove(existingKeyRecord.vk_code);
131126
}
@@ -201,7 +196,7 @@ private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
201196
if (blockKeyPress)
202197
return (IntPtr)1;
203198
}
204-
199+
205200
return CallNextHookEx(_hookID, nCode, wParam, lParam);
206201
}
207202

@@ -232,7 +227,6 @@ private static bool HandleRegisteredKeyPress(IntPtr wParam, int vkCode, KeyRecor
232227
break;
233228
}
234229
}
235-
236230

237231
if (triggerCombo)
238232
{
@@ -257,7 +251,7 @@ private static bool HandleRegisteredKeyPress(IntPtr wParam, int vkCode, KeyRecor
257251
isLWinKeyDown = false;
258252
SendAltKeyUp();
259253

260-
keyRecord.action?.Invoke("LWin key remapped");
254+
keyRecord.action?.Invoke("");
261255

262256
return true;
263257
}

0 commit comments

Comments
 (0)