{"record":{"id":"a96369c4da2bdfdc","repo":"NickeManarin/ScreenToGif","slug":"hotkey-already-in-use","errorCode":null,"errorMessage":"Hotkey already in use","messagePattern":"Hotkey already in use","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"warning","filePath":"ScreenToGif.Native/Helpers/HotKey.cs","lineNumber":41,"sourceCode":"\n    public Key Key { get; }\n\n    #endregion\n\n\n    public HotKey(ModifierKeys modifier, Key key, IntPtr windowsHandle, Action callback)\n    {\n        Modifier = modifier;\n        Key = key;\n\n        var keys = ConvertWinformsToWpfKey(key);\n\n        _windowHandle = windowsHandle;\n        _id = GetHashCode();\n        _callback = callback;\n\n        if (!User32.RegisterHotKey(_windowHandle, _id, Modifier, keys))\n            throw new InvalidOperationException(\"Hotkey already in use\");\n\n        ComponentDispatcher.ThreadPreprocessMessage += ThreadPreprocessMessageMethod;\n    }\n\n    public HotKey(ModifierKeys modifier, Key key, Action callback, bool unregisterFirst = false)\n    {\n        Modifier = modifier;\n        Key = key;\n\n        var keys = ConvertWinformsToWpfKey(key);\n\n        _windowHandle = IntPtr.Zero;\n        _id = GetHashCode();\n        _callback = callback;\n\n        if (unregisterFirst)\n            User32.UnregisterHotKey(_windowHandle, _id);\n","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/NickeManarin/ScreenToGif/blob/a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd/ScreenToGif.Native/Helpers/HotKey.cs#L23-L59","documentation":"Thrown by the first HotKey constructor when User32.RegisterHotKey returns false. RegisterHotKey fails if the exact modifier+key combination is already registered system-wide by any application. The hotkey ID is derived from GetHashCode() which is based on (Modifier * 397) ^ Key, so identical combos collide.","triggerScenarios":"RegisterHotKey is called with a modifier+key pair that another process has already registered. Common conflicting apps: other screen recorders, global hotkey utilities (e.g., AutoHotkey, PowerToys), or a previous ScreenToGif instance that did not properly unregister.","commonSituations":"User configured the same hotkey in ScreenToGif and another application. ScreenToGif crashed previously without calling UnregisterHotKey in Dispose. Multiple ScreenToGif windows or viewmodels are trying to register the same shortcut simultaneously.","solutions":["Check if the hotkey is already registered by another app and choose a different key combination in settings.","Ensure Dispose() is called on the previous HotKey instance before creating a new one with the same combo.","Use the second constructor overload with unregisterFirst = true to force-unregister before re-registering.","Catch InvalidOperationException at the call site and prompt the user to pick a different hotkey."],"exampleFix":"// before\nif (!User32.RegisterHotKey(_windowHandle, _id, Modifier, keys))\n    throw new InvalidOperationException(\"Hotkey already in use\");\n\n// after: try unregister first, then re-register, surface a recoverable error\nif (unregisterFirst)\n    User32.UnregisterHotKey(_windowHandle, _id);\nif (!User32.RegisterHotKey(_windowHandle, _id, Modifier, keys))\n    throw new InvalidOperationException($\"Hotkey {Modifier}+{Key} is already registered by another application.\");","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    _hotKey = new HotKey(modifier, key, windowHandle, callback);\n}\ncatch (InvalidOperationException)\n{\n    // Prompt user to choose a different combination\n    ShowHotkeyConflictMessage(modifier, key);\n}","preventionTips":["Always call Dispose() on the previous HotKey instance before registering a new one with the same combo.","Check for known conflicting hotkeys from common apps before registering.","Let the user pick hotkeys from a constrained list of available combinations."],"tags":["windows-api","hotkey","user32","registerhotkey","interop"],"backgroundTag":null,"analyzedSha":"a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd","analyzedAt":"2026-08-13T11:12:06.147Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}