NickeManarin/ScreenToGif · warning · InvalidOperationException

Hotkey already in use

Error message

Hotkey already in use

What it means

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.

Source

Thrown at ScreenToGif.Native/Helpers/HotKey.cs:41

    public Key Key { get; }

    #endregion


    public HotKey(ModifierKeys modifier, Key key, IntPtr windowsHandle, Action callback)
    {
        Modifier = modifier;
        Key = key;

        var keys = ConvertWinformsToWpfKey(key);

        _windowHandle = windowsHandle;
        _id = GetHashCode();
        _callback = callback;

        if (!User32.RegisterHotKey(_windowHandle, _id, Modifier, keys))
            throw new InvalidOperationException("Hotkey already in use");

        ComponentDispatcher.ThreadPreprocessMessage += ThreadPreprocessMessageMethod;
    }

    public HotKey(ModifierKeys modifier, Key key, Action callback, bool unregisterFirst = false)
    {
        Modifier = modifier;
        Key = key;

        var keys = ConvertWinformsToWpfKey(key);

        _windowHandle = IntPtr.Zero;
        _id = GetHashCode();
        _callback = callback;

        if (unregisterFirst)
            User32.UnregisterHotKey(_windowHandle, _id);

View on GitHub (pinned to a4d0a67c21)

Solutions

  1. Check if the hotkey is already registered by another app and choose a different key combination in settings.
  2. Ensure Dispose() is called on the previous HotKey instance before creating a new one with the same combo.
  3. Use the second constructor overload with unregisterFirst = true to force-unregister before re-registering.
  4. Catch InvalidOperationException at the call site and prompt the user to pick a different hotkey.

Example fix

// before
if (!User32.RegisterHotKey(_windowHandle, _id, Modifier, keys))
    throw new InvalidOperationException("Hotkey already in use");

// after: try unregister first, then re-register, surface a recoverable error
if (unregisterFirst)
    User32.UnregisterHotKey(_windowHandle, _id);
if (!User32.RegisterHotKey(_windowHandle, _id, Modifier, keys))
    throw new InvalidOperationException($"Hotkey {Modifier}+{Key} is already registered by another application.");
Defensive patterns

Strategy: try-catch

Try / catch

try
{
    _hotKey = new HotKey(modifier, key, windowHandle, callback);
}
catch (InvalidOperationException)
{
    // Prompt user to choose a different combination
    ShowHotkeyConflictMessage(modifier, key);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of NickeManarin/ScreenToGif@a4d0a67c21 (2026-08-13). Data as JSON: /api/errors/a96369c4da2bdfdc. Report an issue: GitHub.