{"record":{"id":"9035dbd96d21a472","repo":"babalae/better-genshin-impact","slug":"hotkey-registration-failed","errorCode":null,"errorMessage":"Hotkey registration failed","messagePattern":"Hotkey registration failed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Fischless.HotkeyCapture/HotkeyHook.cs","lineNumber":60,"sourceCode":"    {\n        window.KeyPressed += (sender, args) =>\n        {\n            KeyPressed?.Invoke(this, args);\n        };\n    }\n\n    public void RegisterHotKey(User32.HotKeyModifiers modifier, Keys key)\n    {\n        currentId += 1;\n        if (!User32.RegisterHotKey(window!.Handle, currentId, modifier, (uint)key))\n        {\n            if (Marshal.GetLastWin32Error() == SystemErrorCodes.ERROR_HOTKEY_ALREADY_REGISTERED)\n            {\n                throw new InvalidOperationException(\"Hotkey already registered\");\n            }\n            else\n            {\n                throw new InvalidOperationException(\"Hotkey registration failed\");\n            }\n        }\n    }\n\n    public void UnregisterHotKey()\n    {\n        for (int i = currentId; i > 0; i--)\n        {\n            User32.UnregisterHotKey(window!.Handle, i);\n        }\n    }\n\n    public void Dispose()\n    {\n        UnregisterHotKey();\n        window?.Dispose();\n    }\n}","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/Fischless.HotkeyCapture/HotkeyHook.cs#L42-L78","documentation":"User32.RegisterHotKey returned false for a reason other than already-registered — typically invalid modifiers, an invalid/unsupported key code (e.g. Keys.None), or the message-only window handle not being valid at registration time.","triggerScenarios":"Passing Keys.None or a modifier-only key; invalid/out-of-range key code; registering before the hidden window is created; calling from a thread without a message pump.","commonSituations":"Hotkey config saved with no actual key (modifier-only); key code out of range after a config edit; registering hotkeys during startup before the window handle exists.","solutions":["Ensure a non-modifier Keys value is supplied (Key != Keys.None) before registering.","Verify the hidden message-only window handle is created (window.Handle is valid) before RegisterHotKey.","Log Marshal.GetLastWin32Error() to identify the specific failure code and handle it accordingly."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if (hotkey.Key == Keys.None)\n    return; // modifier-only or empty hotkey is not registrable","typeGuard":"static bool IsRegistrable(Hotkey hk) =>\n    hk.Key != Keys.None;","tryCatchPattern":"try\n{\n    hook.RegisterHotKey(hotkey.ModifierKey, hotkey.Key);\n}\ncatch (InvalidOperationException ex) when (ex.Message == \"Hotkey registration failed\")\n{\n    var code = Marshal.GetLastWin32Error();\n    // log code, inform user the hotkey is invalid\n}","preventionTips":["Reject modifier-only or empty hotkeys before registration.","Ensure the message-only window handle is created before any RegisterHotKey call.","Capture and log the Win32 error code to pinpoint the failure reason."],"tags":["hotkey","win32","registration"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}