babalae/better-genshin-impact · error · InvalidOperationException
Hotkey registration failed
Error message
Hotkey registration failed
What it means
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.
Source
Thrown at Fischless.HotkeyCapture/HotkeyHook.cs:60
{
window.KeyPressed += (sender, args) =>
{
KeyPressed?.Invoke(this, args);
};
}
public void RegisterHotKey(User32.HotKeyModifiers modifier, Keys key)
{
currentId += 1;
if (!User32.RegisterHotKey(window!.Handle, currentId, modifier, (uint)key))
{
if (Marshal.GetLastWin32Error() == SystemErrorCodes.ERROR_HOTKEY_ALREADY_REGISTERED)
{
throw new InvalidOperationException("Hotkey already registered");
}
else
{
throw new InvalidOperationException("Hotkey registration failed");
}
}
}
public void UnregisterHotKey()
{
for (int i = currentId; i > 0; i--)
{
User32.UnregisterHotKey(window!.Handle, i);
}
}
public void Dispose()
{
UnregisterHotKey();
window?.Dispose();
}
}View on GitHub (pinned to a7cb36712d)
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.
Defensive patterns
Strategy: validation
Validate before calling
if (hotkey.Key == Keys.None)
return; // modifier-only or empty hotkey is not registrable Type guard
static bool IsRegistrable(Hotkey hk) =>
hk.Key != Keys.None; Try / catch
try
{
hook.RegisterHotKey(hotkey.ModifierKey, hotkey.Key);
}
catch (InvalidOperationException ex) when (ex.Message == "Hotkey registration failed")
{
var code = Marshal.GetLastWin32Error();
// log code, inform user the hotkey is invalid
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Hotkey already registered
- 动作 {snapshot.Description} 执行 {attempts} 次后,等待 {snapshot.Targ
- 鼠标限制区域必须具有有效宽度和高度。
- 临时解除本地鼠标限制失败
- 注册 Raw Input 鼠标设备失败
AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13).
Data as JSON: /api/errors/9035dbd96d21a472.
Report an issue: GitHub.