Skip to content

fix: capitalize accelerator modifiers to fix custom keybindings#4134

Open
Bowl42 wants to merge 2 commits into
marktext:developfrom
Bowl42:fix/keybinding-case
Open

fix: capitalize accelerator modifiers to fix custom keybindings#4134
Bowl42 wants to merge 2 commits into
marktext:developfrom
Bowl42:fix/keybinding-case

Conversation

@Bowl42
Copy link
Copy Markdown
Contributor

@Bowl42 Bowl42 commented Mar 4, 2026

Summary

Fixes #4123

  • The keybinding preferences UI captures keyboard shortcuts via getAcceleratorFromKeyboardEvent() which returns lowercase modifier names (e.g. ctrl+alt+1), but Electron requires properly capitalized modifiers (e.g. Ctrl+Alt+1) to register shortcuts correctly.
  • Add capitalizeAccelerator() utility to src/common/keybinding/index.js that normalizes modifier casing against a canonical map (Ctrl, Alt, Shift, Cmd, Meta, Super, AltGr, Option, CommandOrControl, CmdOrCtrl, Command, Control).
  • Apply capitalization in key-input-dialog.vue immediately after capturing the accelerator, before saving.
  • Fix _isDefaultBinding in KeybindingConfigurator.js to use isEqualAccelerator (case-insensitive) instead of strict equality, so re-entering a default binding is correctly recognized.

Test plan

  • Add 21 unit tests for capitalizeAccelerator() covering all modifier names, mixed case, empty/null input, and multi-modifier combinations (test/unit/specs/capitalize-accelerator.spec.js)
  • Manual: Open Preferences > Key Bindings, edit a binding (e.g. press Ctrl+Alt+1), verify it displays as Ctrl+Alt+1 (not ctrl+alt+1)
  • Manual: Save the binding, return to document, verify the shortcut works

🤖 Generated with Claude Code

Bowl42 and others added 2 commits March 4, 2026 21:21
…text#4123)

The keybinding UI captured keyboard shortcuts with lowercase modifier
names (e.g. "ctrl+alt+1") via getAcceleratorFromKeyboardEvent(), but
Electron requires properly capitalized modifiers (e.g. "Ctrl+Alt+1").

- Add capitalizeAccelerator() utility to normalize modifier casing
- Apply capitalization in key-input-dialog before saving
- Fix _isDefaultBinding to use case-insensitive comparison
- Add comprehensive unit tests for capitalizeAccelerator

Fixes marktext#4123

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Apply capitalizeAccelerator when loading user keybindings from disk,
  healing legacy files that contain lowercase modifiers
- Fix JSDoc: clarify that only modifiers are capitalized, not key names
- Fix comment: map contains modifier names only
- Add missing test cases: single modifier without key, Plus key,
  non-modifier special keys, idempotency

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes custom keybinding registration failures caused by lowercase modifier names (e.g. ctrl+alt+1) by normalizing accelerator modifier casing to Electron’s expected format and ensuring default-binding comparisons are case-insensitive.

Changes:

  • Added capitalizeAccelerator() utility in src/common/keybinding/index.js to normalize modifier casing.
  • Applied accelerator capitalization when capturing shortcuts in the preferences UI and when loading legacy keybindings.json entries.
  • Updated default binding detection to use isEqualAccelerator and added unit tests for the new utility.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/unit/specs/capitalize-accelerator.spec.js Adds unit coverage for modifier capitalization behavior and edge cases.
src/common/keybinding/index.js Introduces capitalizeAccelerator() for canonical modifier casing.
src/renderer/prefComponents/keybindings/KeybindingConfigurator.js Makes default-binding detection case-insensitive via isEqualAccelerator.
src/renderer/prefComponents/keybindings/key-input-dialog.vue Normalizes captured accelerators before validation/display/save.
src/main/keyboard/shortcutHandler.js Normalizes modifier casing when loading user keybindings from disk.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +24 to +25
* @param {string} accelerator
* @returns {string}
UCHIHAHA103 pushed a commit to UCHIHAHA103/marktext that referenced this pull request May 14, 2026
…ll position / XSS / contextIsolation

- fix: 编辑菜单截图分割线仅 macOS 可见(Windows 不再出现双分割线)
- fix(marktext#4093): 深色主题打开窗口预设背景色,消除白闪
- feat(marktext#4134): capitalizeAccelerator - 自定义快捷键修饰符大写修复
- fix(marktext#4146): 搜索打开文件后首次 muya 序列化不触发未保存状态
- fix(marktext#4075): 外部文件变更:已保存时静默加载,未保存时才提示
- fix(marktext#4152): 切换 Tab 保留滚动位置;打开文件光标定位到开头
- fix(marktext#4206): 图片属性 HTML 转义防 XSS/RCE
- fix(marktext#4192): contextIsolation=true 防渲染进程调用 Node.js API
@Jocs
Copy link
Copy Markdown
Member

Jocs commented May 15, 2026

This project recently merged a large refactor (electron-vite migration, PR #4001) that upgraded dependencies and fixed several issues.

Please review this PR and let us know if it is still needed, or if the changes have already been addressed upstream.

Note: This PR currently has merge conflicts with the develop branch. If you decide to continue with this PR, please rebase or merge the latest develop branch to resolve the conflicts before it can be reviewed.

@Jocs
Copy link
Copy Markdown
Member

Jocs commented May 20, 2026

Hi @Bowl42, thanks for the well-tested PR. Before reviewing, we'd like to confirm a couple of things.

The current _normalizeAccelerator in src/common/keybinding/index.js lowercases modifiers (e.g. to cmd/ctrl/alt), and we couldn't locate the getAcceleratorFromKeyboardEvent() you mention — it may have been replaced. Could you:

  1. Confirm that Custom Key Binding fails due to lowercase "ctrl" and "alt" #4123 still reproduces on the latest develop branch (set a custom binding via Preferences → Key Bindings, and verify whether the shortcut actually fires), and
  2. Rebase against develop to resolve the current conflicts?

If so, we'll be happy to review. If you don't have the time to push it forward, we'll close after a few weeks. Thanks again for the detailed test coverage.

@Jocs Jocs added the 🐤 blocked/need info Need more details about this issue or pull request label May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐤 blocked/need info Need more details about this issue or pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom Key Binding fails due to lowercase "ctrl" and "alt"

3 participants