home / skills / lobehub / lobe-chat / hotkey

hotkey skill

/.agents/skills/hotkey

This skill guides adding keyboard shortcuts and hotkeys, helping implement, register, and document keyboard interactions across features.

npx playbooks add skill lobehub/lobe-chat --skill hotkey

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
2.3 KB
---
name: hotkey
description: Guide for adding keyboard shortcuts. Use when implementing new hotkeys, registering shortcuts, or working with keyboard interactions. Triggers on hotkey implementation or keyboard shortcut tasks.
---

# Adding Keyboard Shortcuts Guide

## Steps to Add a New Hotkey

### 1. Update Hotkey Constant

In `src/types/hotkey.ts`:

```typescript
export const HotkeyEnum = {
  // existing...
  ClearChat: 'clearChat', // Add new
} as const;
```

### 2. Register Default Hotkey

In `src/const/hotkeys.ts`:

```typescript
import { KeyMapEnum as Key, combineKeys } from '@lobehub/ui';

export const HOTKEYS_REGISTRATION: HotkeyRegistration = [
  {
    group: HotkeyGroupEnum.Conversation,
    id: HotkeyEnum.ClearChat,
    keys: combineKeys([Key.Mod, Key.Shift, Key.Backspace]),
    scopes: [HotkeyScopeEnum.Chat],
  },
];
```

### 3. Add i18n Translation

In `src/locales/default/hotkey.ts`:

```typescript
const hotkey: HotkeyI18nTranslations = {
  clearChat: {
    desc: '清空当前会话的所有消息记录',
    title: '清空聊天记录',
  },
};
```

### 4. Create and Register Hook

In `src/hooks/useHotkeys/chatScope.ts`:

```typescript
export const useClearChatHotkey = () => {
  const clearMessages = useChatStore((s) => s.clearMessages);
  return useHotkeyById(HotkeyEnum.ClearChat, clearMessages);
};

export const useRegisterChatHotkeys = () => {
  useClearChatHotkey();
  // ...other hotkeys
};
```

### 5. Add Tooltip (Optional)

```tsx
const clearChatHotkey = useUserStore(settingsSelectors.getHotkeyById(HotkeyEnum.ClearChat));

<Tooltip hotkey={clearChatHotkey} title={t('clearChat.title', { ns: 'hotkey' })}>
  <Button icon={<DeleteOutlined />} onClick={clearMessages} />
</Tooltip>
```

## Best Practices

1. **Scope**: Choose global or chat scope based on functionality
2. **Grouping**: Place in appropriate group (System/Layout/Conversation)
3. **Conflict check**: Ensure no conflict with system/browser shortcuts
4. **Platform**: Use `Key.Mod` instead of hardcoded `Ctrl` or `Cmd`
5. **Clear description**: Provide title and description for users

## Troubleshooting

- **Not working**: Check scope and RegisterHotkeys hook
- **Not in settings**: Verify HOTKEYS_REGISTRATION config
- **Conflict**: HotkeyInput component shows warnings
- **Page-specific**: Ensure correct scope activation

Overview

This skill guides developers through adding and managing keyboard shortcuts (hotkeys) in a TypeScript app. It explains where to declare new hotkey constants, how to register defaults, add translations, create hooks, and optionally show tooltips. The goal is reliable, conflict-free hotkeys that respect scope and platform conventions.

How this skill works

The skill inspects the hotkey lifecycle: enum declaration, registration in the default config, i18n entry, and hook registration for activation. It also shows how to bind a handler using a helper hook and surface the shortcut in the UI with an optional tooltip. It enforces scope-aware behavior and recommends using modifier abstractions for cross-platform consistency.

When to use it

  • Implementing a new keyboard shortcut or modifying an existing one
  • Registering default hotkeys so they appear in settings and work by default
  • Binding handlers that respond to shortcut activation in specific scopes
  • Ensuring shortcuts are localized and visible in the UI
  • Troubleshooting shortcuts that fail to trigger or conflict with others

Best practices

  • Declare the hotkey identifier in the hotkey enum to keep keys centralized
  • Register defaults in the hotkey registration list so settings and UI reflect the shortcut
  • Use scope-aware hooks to activate shortcuts only where appropriate (global vs chat)
  • Prefer Key.Mod (modifier abstraction) for cross-platform Ctrl/Cmd behavior
  • Provide clear title and description translations for user-facing settings
  • Check for conflicts with browser/system shortcuts before choosing a combination

Example use cases

  • Add ClearChat: declare enum, register Hotkey with Mod+Shift+Backspace, add i18n, implement a useClearChatHotkey hook
  • Create a SaveDraft shortcut scoped to the editor group and register it under the editor scope
  • Expose a frequently used action in the toolbar with a Tooltip that shows the registered hotkey
  • Migrate hardcoded modifiers to Key.Mod across the codebase to avoid platform-specific bugs
  • Debug a page-specific hotkey by verifying the scope and that the register hook runs on that page

FAQ

Why register hotkeys in a single config file?

Central registration ensures the settings UI, default behavior, and persistence are all consistent and discoverable.

Hotkey doesn’t trigger — what should I check first?

Verify the hook that registers the hotkey runs, confirm the correct scope is active, and ensure the chosen key combination isn’t blocked by the browser or OS.