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

  1. Ensure a non-modifier Keys value is supplied (Key != Keys.None) before registering.
  2. Verify the hidden message-only window handle is created (window.Handle is valid) before RegisterHotKey.
  3. 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

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


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/9035dbd96d21a472. Report an issue: GitHub.