libnyanpasu/clash-nyanpasu · error

hotkey_error.invalid_hotkey

Error message

hotkey_error.invalid_hotkey

What it means

check_key validates a hotkey string by parsing it as a global-hotkey Shortcut before registering, because Tauri's shortcut APIs panic on invalid input. If parsing fails it bails with the localized message hotkey_error.invalid_hotkey (which includes the offending hotkey). This is a pre-flight guard converting would-be panics into errors.

Source

Thrown at backend/tauri/src/core/hotkey.rs:234

                    _ => {
                        let key = key.unwrap_or("None");
                        let func = func.unwrap_or("None");
                        log::error!(target: "app", "invalid hotkey `{key}`:`{func}`");
                    }
                }
            }
            *self.current.lock() = hotkeys;
        }

        Ok(())
    }

    /// 检查一个键是否合法
    fn check_key(hotkey: &str) -> anyhow::Result<()> {
        // fix #287
        // tauri的这几个方法全部有Result expect,会panic,先检测一遍避免挂了
        if hotkey.parse::<Shortcut>().is_err() {
            bail!("{}", t!("hotkey_error.invalid_hotkey", hotkey = hotkey));
        }
        // Validate super key requirement
        if !Self::validate_super_key(hotkey) {
            bail!("{}", t!("hotkey_error.missing_super_key"));
        }
        Ok(())
    }

    /// Check if the hotkey contains a super key modifier (case-insensitive)
    pub fn validate_super_key(hotkey: &str) -> bool {
        let hotkey_lower = hotkey.to_lowercase();
        SUPER_KEYS
            .iter()
            .any(|key| hotkey_lower.contains(&key.to_lowercase()))
    }

    fn register(&self, hotkey: &str, func: &str) -> Result<()> {
        let app_handle = self.app_handle.lock();

View on GitHub (pinned to f7dbce2997)

Solutions

  1. Correct the hotkey string to a valid accelerator (modifier+key, e.g. CmdOrCtrl+Shift+P) and re-save config
  2. Validate hotkeys in the frontend picker before persisting them
  3. Surface the localized hotkey_error.invalid_hotkey message (it already includes the bad value) to guide the user

Example fix

// config before
"Ctrl Alt P": "toggle_system_proxy"
// after
"Ctrl+Alt+P": "toggle_system_proxy"
Defensive patterns

Strategy: validation

Validate before calling

fn is_valid_hotkey(hotkey: &str) -> bool {
    hotkey.parse::<global_hotkey::Shortcut>().is_ok()
}

Try / catch

if let Err(e) = Hotkey::register(hotkey, func) {
    ui.show_error(t!("hotkey_error.invalid_hotkey", hotkey = hotkey));
}

Prevention

When it happens

Trigger: registering/updating a hotkey whose string cannot parse as a Shortcut — wrong separator, missing modifier, unknown key name (e.g. 'Ctrl+Alt+PrtSc', 'Meta+A' unsupported forms, empty string).

Common situations: Users typing hotkeys manually in config; frontend sending malformed accelerator strings; platform-specific key names not recognized on all OSes.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of libnyanpasu/clash-nyanpasu@f7dbce2997 (2026-09-08). Data as JSON: /api/errors/57b6ebcab77a5064. Report an issue: GitHub.