Skip to content

Commit

Permalink
fix unregister hotkey not properly recognising single and combo of sa…
Browse files Browse the repository at this point in the history
…me key
  • Loading branch information
jjw24 committed Dec 27, 2024
1 parent 0ea7523 commit d47343c
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions ChefKeys/ChefKeysManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static void RegisterHotkey(string hotkeys, string previousHotkey, Action<
hotkeys = ConvertIncorrectKeyString(hotkeys);
previousHotkey = ConvertIncorrectKeyString(previousHotkey);

UnregisterHotkey(hotkeys, previousHotkey, action);
UnregisterHotkey(previousHotkey);

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

public static void UnregisterHotkey(string hotkey, string previousHotkey, Action<string> action)
public static void UnregisterHotkey(string hotkey)

Check warning on line 101 in ChefKeys/ChefKeysManager.cs

View workflow job for this annotation

GitHub Actions / workflow

Missing XML comment for publicly visible type or member 'ChefKeysManager.UnregisterHotkey(string)'
{
var hotkeyToCheck = hotkey;

if (!registeredHotkeys.TryGetValue(ToKeyCode(SplitHotkeyReversed(hotkeyToCheck).First()), out var existingKeyRecord))
{
hotkeyToCheck= previousHotkey;
if (!registeredHotkeys.TryGetValue(ToKeyCode(SplitHotkeyReversed(hotkeyToCheck).First()), out var existingPrevKeyRecord))
return;
if (!registeredHotkeys.TryGetValue(ToKeyCode(SplitHotkeyReversed(hotkey).First()), out var existingKeyRecord))
return;

existingKeyRecord = existingPrevKeyRecord;
}
if (!existingKeyRecord.KeyComboRecords.Exists(x => x.comboRaw == hotkey))
return;

if (existingKeyRecord.isSingleKeyRegistered && !existingKeyRecord.AreKeyCombosRegistered() && !hotkeyToCheck.Contains('+'))
if (!hotkey.Contains('+'))
{
existingKeyRecord.action -= existingKeyRecord.action;
registeredHotkeys.Remove(existingKeyRecord.vk_code);
return;
}
existingKeyRecord.isSingleKeyRegistered = false;

var comboRecord = existingKeyRecord.KeyComboRecords.FirstOrDefault(x => x.comboRaw == hotkeyToCheck);
if (!existingKeyRecord.AreKeyCombosRegistered())
registeredHotkeys.Remove(existingKeyRecord.vk_code);

// There is a single key press still registered, no need to remove anything from registeredHotkeys.
if (comboRecord is null)
return;
}

var comboRecord = existingKeyRecord.KeyComboRecords.FirstOrDefault(x => x.comboRaw == hotkey);
comboRecord.action -= comboRecord.action;
existingKeyRecord.KeyComboRecords.RemoveAll(x => x.comboRaw == hotkeyToCheck);
existingKeyRecord.KeyComboRecords.RemoveAll(x => x.comboRaw == hotkey);

if (!existingKeyRecord.isSingleKeyRegistered && !existingKeyRecord.AreKeyCombosRegistered())
registeredHotkeys.Remove(existingKeyRecord.vk_code);
}
Expand Down Expand Up @@ -201,7 +196,7 @@ private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
if (blockKeyPress)
return (IntPtr)1;
}

return CallNextHookEx(_hookID, nCode, wParam, lParam);
}

Expand Down Expand Up @@ -232,7 +227,6 @@ private static bool HandleRegisteredKeyPress(IntPtr wParam, int vkCode, KeyRecor
break;
}
}


if (triggerCombo)
{
Expand All @@ -257,7 +251,7 @@ private static bool HandleRegisteredKeyPress(IntPtr wParam, int vkCode, KeyRecor
isLWinKeyDown = false;
SendAltKeyUp();

keyRecord.action?.Invoke("LWin key remapped");
keyRecord.action?.Invoke("");

return true;
}
Expand Down

0 comments on commit d47343c

Please sign in to comment.