Skip to content

Fix macOS cursor shortcuts in BibTeX source editor (#5937)#15358

Merged
Siedlerchr merged 6 commits into
JabRef:mainfrom
AnvitaPrasad:fix-5937-macos-text-cursor-shortcuts
Mar 18, 2026
Merged

Fix macOS cursor shortcuts in BibTeX source editor (#5937)#15358
Siedlerchr merged 6 commits into
JabRef:mainfrom
AnvitaPrasad:fix-5937-macos-text-cursor-shortcuts

Conversation

@AnvitaPrasad

@AnvitaPrasad AnvitaPrasad commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

Related issues and pull requests

Closes #5937

PR Description

This PR fixes macOS cursor navigation behavior in the BibTeX source editor so it matches standard macOS text editing. Command+Left/Right now moves to line start/end in that editor as expected. This makes editing BibTeX source on macOS consistent with other text fields and with native app behavior.

Steps to test

  1. Use macOS.
  2. Start JabRef from this branch.
  3. Open any .bib library.
  4. Select one entry and open the entry editor.
  5. Go to the Source tab (BibTeX source editor).
  6. Place the cursor in the middle of a long line.
  7. Press Command+Left and Command+Right:
    • Expected: cursor jumps to beginning/end of line.
  8. Compare with behavior on main branch to confirm the fix.

Before

Screen.Recording.2026-03-18.at.12.06.52.AM.mov

After

Screen.Recording.2026-03-18.at.12.12.33.AM.mov

Checklist

  • I own the copyright of the code submitted and I license it under the MIT license
  • I manually tested my changes in running JabRef (always required)
  • [/] I added JUnit tests for changes (if applicable)
  • I added screenshots in the PR description (if change is visible to the user)
  • I added a screenshot in the PR description showing a library with a single entry with me as author and as title the issue number
  • I described the change in CHANGELOG.md in a way that can be understood by the average user (if change is visible to the user)
  • I checked the user documentation for up to dateness and submitted a pull request to our user documentation repository

Copilot AI review requested due to automatic review settings March 17, 2026 18:45
@github-actions

Copy link
Copy Markdown
Contributor

Hey @AnvitaPrasad! 👋

Thank you for contributing to JabRef!

We have automated checks in place, based on which you will soon get feedback if any of them are failing. We also use Qodo for review assistance. It will update your pull request description with a review help and offer suggestions to improve the pull request.

After all automated checks pass, a maintainer will also review your contribution. Once that happens, you can go through their comments in the "Files changed" tab and act on them, or reply to the conversation if you have further inputs. You can read about the whole pull request process in our contribution guide.

Please ensure that your pull request is in line with our AI Usage Policy and make necessary disclosures.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown
Contributor

Review Summary by Qodo

Fix macOS cursor shortcuts in BibTeX source editor

🐞 Bug fix

Grey Divider

Walkthroughs

Description
• Fixes macOS Command+Left/Right cursor navigation in BibTeX source editor
• Implements proper line start/end movement matching native macOS behavior
• Adds Option+Left/Right word navigation support for consistency
• Detects macOS platform and handles cursor shortcuts separately
Diagram
flowchart LR
  A["KeyEvent in CodeArea"] --> B["Check if macOS"]
  B -->|Yes| C["Handle Cursor Shortcuts"]
  B -->|No| D["Use Default Bindings"]
  C --> E["Command+Left/Right<br/>Line Start/End"]
  C --> F["Option+Left/Right<br/>Word Navigation"]
  E --> G["Consume Event"]
  F --> G
  D --> H["Process via<br/>KeyBindingRepository"]
Loading

Grey Divider

File Changes

1. jabgui/src/main/java/org/jabref/gui/keyboard/CodeAreaKeyBindings.java 🐞 Bug fix +46/-0

Add macOS-specific cursor navigation handling

• Added imports for KeyCode and OS utility class
• Introduced handleMacCursorMovementShortcuts() method to detect and process macOS-specific cursor
 navigation
• Integrated macOS shortcut handler at the beginning of call() method to intercept events before
 default processing
• Implemented Command+Left/Right for line start/end navigation and Option+Left/Right for word-based
 navigation
• Properly handles Shift modifier for text selection during cursor movement

jabgui/src/main/java/org/jabref/gui/keyboard/CodeAreaKeyBindings.java


Grey Divider

Qodo Logo

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0) 📐 Spec deviations (0)

Grey Divider


Remediation recommended

1. Back shortcut blocked 🐞 Bug ✓ Correctness
Description
In the BibTeX source editor on macOS, Option(Alt)+Left/Right is now consumed and used for word
navigation, preventing JabRef’s global BACK/FORWARD shortcuts (default alt+LEFT/RIGHT) from firing
while the CodeArea has focus.
Code

jabgui/src/main/java/org/jabref/gui/keyboard/CodeAreaKeyBindings.java[R131-142]

+        boolean optionOnly = event.isAltDown() && !event.isMetaDown() && !event.isControlDown();
+        boolean commandOnly = event.isMetaDown() && !event.isAltDown() && !event.isControlDown();
+
+        if (optionOnly) {
+            if (code == KeyCode.LEFT) {
+                codeArea.wordBreaksBackwards(2, policy);
+            } else {
+                codeArea.wordBreaksForwards(2, policy);
+            }
+            event.consume();
+            return true;
+        }
Evidence
SourceTab installs CodeAreaKeyBindings as a KEY_PRESSED event filter on the CodeArea, so consuming
the event there prevents it from reaching global handlers. The new macOS handler consumes
Option(Alt)+LEFT/RIGHT events, while JabRef defines BACK/FORWARD as alt+LEFT/RIGHT and wires them to
navigation actions, meaning those shortcuts will not work when focus is inside the source editor on
macOS.

jabgui/src/main/java/org/jabref/gui/entryeditor/SourceTab.java[194-207]
jabgui/src/main/java/org/jabref/gui/keyboard/CodeAreaKeyBindings.java[117-142]
jabgui/src/main/java/org/jabref/gui/keyboard/KeyBinding.java[72-74]
jabgui/src/main/java/org/jabref/gui/actions/StandardActions.java[104-106]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
On macOS in the SourceTab CodeArea, Option(Alt)+Left/Right is now consumed for word-navigation, which blocks JabRef’s default global BACK/FORWARD shortcuts (alt+LEFT/RIGHT) while the editor has focus.
### Issue Context
This behavior change is beyond the PR description (which focuses on Cmd+Left/Right) and can be a regression for users relying on alt+LEFT/RIGHT navigation.
### Fix Focus Areas
- jabgui/src/main/java/org/jabref/gui/keyboard/CodeAreaKeyBindings.java[117-155]
- jabgui/src/main/java/org/jabref/gui/entryeditor/SourceTab.java[194-207]
- jabgui/src/main/java/org/jabref/gui/keyboard/KeyBinding.java[72-74]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Hardcoded Cmd keys🐞 Bug ✓ Correctness
Description
On macOS, CodeAreaKeyBindings handles and consumes Cmd+Left/Right before KeyBindingRepository
mapping runs, making Cmd+Left/Right impossible to override via JabRef’s normal configurable
keybinding system in the source editor.
Code

jabgui/src/main/java/org/jabref/gui/keyboard/CodeAreaKeyBindings.java[R15-21]

   public static void call(CodeArea codeArea, KeyEvent event, KeyBindingRepository keyBindingRepository) {
+        if (handleMacCursorMovementShortcuts(codeArea, event)) {
+            return;
+        }
+
       keyBindingRepository.mapToKeyBinding(event).ifPresent(binding -> {
           switch (binding) {
Evidence
CodeAreaKeyBindings.call returns immediately if the new macOS handler processes the event, so
KeyBindingRepository.mapToKeyBinding is never consulted for those key presses. Since
KeyBindingRepository is the central mechanism for configurable shortcuts, this prevents
user-configured bindings for Cmd+Left/Right from taking effect in this editor.

jabgui/src/main/java/org/jabref/gui/keyboard/CodeAreaKeyBindings.java[15-21]
jabgui/src/main/java/org/jabref/gui/keyboard/CodeAreaKeyBindings.java[144-152]
jabgui/src/main/java/org/jabref/gui/keyboard/KeyBindingRepository.java[110-120]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The macOS Cmd+Left/Right behavior is hard-coded and consumes the event before the normal keybinding mapping executes, so Cmd+Left/Right cannot be overridden via preferences in the BibTeX source editor.
### Issue Context
`KeyBindingRepository` is the central mechanism for configurable shortcuts, but `CodeAreaKeyBindings.call` returns early when the macOS handler triggers.
### Fix Focus Areas
- jabgui/src/main/java/org/jabref/gui/keyboard/CodeAreaKeyBindings.java[15-21]
- jabgui/src/main/java/org/jabref/gui/keyboard/CodeAreaKeyBindings.java[117-155]
- jabgui/src/main/java/org/jabref/gui/keyboard/KeyBindingRepository.java[110-120]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes macOS-specific cursor navigation in the BibTeX source editor (CodeArea) so standard macOS shortcuts behave as expected (Cmd+Left/Right to line start/end, Opt+Left/Right to word boundaries).

Changes:

  • Added a macOS-only key handling path for Left/Right arrow keys with Cmd/Opt (and Shift selection extension).
  • Consumes handled key events before they reach the keybinding repository / default handlers.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +3 to +4
import javafx.scene.input.KeyEvent;
import javafx.scene.input.KeyCode;
Comment on lines 15 to +18
public static void call(CodeArea codeArea, KeyEvent event, KeyBindingRepository keyBindingRepository) {
if (handleMacCursorMovementShortcuts(codeArea, event)) {
return;
}
@github-actions github-actions Bot added the status: changes-required Pull requests that are not yet complete label Mar 17, 2026
@testlens-app

This comment has been minimized.

@Siedlerchr

Copy link
Copy Markdown
Member

Command left/right works for me, but option left/right which should move the caret to the word, doesn't work for me here on macOS Sequoia 15.7.3 (Macbook pro) but that option doesn't work in text fields either (seems to also not work on main)
Can you take a look at that or reproduce that as well?
Otherwise just fix the failing tests and add a changelog entry

@k3KAW8Pnf7mkmdSMPHz27 k3KAW8Pnf7mkmdSMPHz27 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Tested and working on macOS 26.3. A few suggestions below.

public class CodeAreaKeyBindings {

public static void call(CodeArea codeArea, KeyEvent event, KeyBindingRepository keyBindingRepository) {
if (handleMacCursorMovementShortcuts(codeArea, event)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The method both calls event.consume() and returns true, which are two signals for the same thing. Since event.consume() is required regardless to stop JavaFX event propagation, the boolean return is redundant. If they ever diverge (e.g. calling consume() but forgetting return true), you have a bug. Consider changing to void and checking event.isConsumed() instead as a single source of truth.


KeyCode code = event.getCode();
if ((code != KeyCode.LEFT) && (code != KeyCode.RIGHT)) {
return false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggestion, possibly outside the scope of this PR: Per Apple's keyboard shortcuts, standard macOS text editing also includes:

  • Command+Up/Down for document start/end
  • Option+Up/Down for paragraph start/end
  • Shift variants for selection

All needed CodeArea methods exist (start(policy), end(policy), paragraphStart(policy), paragraphEnd(policy)).


boolean optionOnly = event.isAltDown() && !event.isMetaDown() && !event.isControlDown();
boolean commandOnly = event.isMetaDown() && !event.isAltDown() && !event.isControlDown();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice touch. The named booleans optionOnly and commandOnly document the macOS intent clearly and make the modifier checks readable without needing comments.

@testlens-app

This comment has been minimized.

@AnvitaPrasad AnvitaPrasad force-pushed the fix-5937-macos-text-cursor-shortcuts branch from b9055c7 to 2b5302f Compare March 18, 2026 14:36
@testlens-app

This comment has been minimized.

@testlens-app

This comment has been minimized.

- Fix ternary operator indentation in CodeAreaKeyBindings to match JabRef
  code style (aligns ? and : with the start of the condition value)
- Add CodeAreaKeyBindingsTest with 8 unit tests covering all macOS cursor
  shortcut cases (Cmd+Left/Right, Option+Left/Right, Shift variants, non-Mac,
  non-arrow key)
- Add CHANGELOG entry for the macOS cursor shortcuts fix

Closes JabRef#5937

Made-with: Cursor
- Remove duplicate JabRef#5937 CHANGELOG entry (keep single descriptive entry)
- Add Ctrl+Left/Right as word navigation shortcut (in addition to Option+Left/Right)
  for users who have macOS Mission Control shortcuts disabled
- Add tests for Ctrl+Left/Right word navigation behavior

Made-with: Cursor
@testlens-app

This comment has been minimized.

@AnvitaPrasad AnvitaPrasad force-pushed the fix-5937-macos-text-cursor-shortcuts branch from e3178a2 to a974ecd Compare March 18, 2026 18:17
@testlens-app

This comment has been minimized.

@AnvitaPrasad

Copy link
Copy Markdown
Contributor Author

@k3KAW8Pnf7mkmdSMPHz27 @Siedlerchr
Looks like issue was that JabRefFrame.initKeyBindings() had an event filter at the frame level that was intercepting alt+LEFT/RIGHT before the CodeArea even saw them. basically it was consuming those keys for BACK/FORWARD navigation before the handler could do anything with it.
What I did was add a check before handling BACK/FORWARD. if a TextInputControl or CodeArea is focused, the frame level handler just skips it. That way the event goes through to the editor and option+left/right works as expected. It also ends up fixing the behavior for regular text fields too.

@Siedlerchr

Copy link
Copy Markdown
Member

Ah great that makes my PR then obsolte!

@testlens-app

testlens-app Bot commented Mar 18, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 90b00af
▶️ Tests: 10195 executed
⚪️ Checks: 52/52 completed


Learn more about TestLens at testlens.app.

@Siedlerchr

Copy link
Copy Markdown
Member

The only thing that doesn't yet work in the CodeArea is paragraph up/down using Option + up/down as @k3KAW8Pnf7mkmdSMPHz27 mentioned, the rest works fine and also fixes the normal text input fields!

@github-actions github-actions Bot added status: no-bot-comments and removed status: changes-required Pull requests that are not yet complete labels Mar 18, 2026
@AnvitaPrasad

Copy link
Copy Markdown
Contributor Author

@Siedlerchr Sorry for the overlap with your PR 😅.

About the Option + Up/Down. I wasn’t sure if that should be in scope for this PR since #5937 was mainly about left/right navigation.Should I add it here or open a small follow up PR for that?

@Siedlerchr

Copy link
Copy Markdown
Member

No worries, your solution is more elegant :)
Just create a follow up PR

@Siedlerchr Siedlerchr added this pull request to the merge queue Mar 18, 2026
@github-actions github-actions Bot added the status: to-be-merged PRs which are accepted and should go into the merge-queue. label Mar 18, 2026
Merged via the queue into JabRef:main with commit 18f1d8c Mar 18, 2026
52 checks passed
FynnianB pushed a commit to FynnianB/jabref that referenced this pull request Mar 19, 2026
…Ref#15358)

* Refine macOS source-editor cursor shortcuts

* fix formatting

* Address review feedback: fix formatting, add tests, update changelog

- Fix ternary operator indentation in CodeAreaKeyBindings to match JabRef
  code style (aligns ? and : with the start of the condition value)
- Add CodeAreaKeyBindingsTest with 8 unit tests covering all macOS cursor
  shortcut cases (Cmd+Left/Right, Option+Left/Right, Shift variants, non-Mac,
  non-arrow key)
- Add CHANGELOG entry for the macOS cursor shortcuts fix

Closes JabRef#5937

Made-with: Cursor

* Remove duplicate changelog entry, add Ctrl+Left/Right word navigation

- Remove duplicate JabRef#5937 CHANGELOG entry (keep single descriptive entry)
- Add Ctrl+Left/Right as word navigation shortcut (in addition to Option+Left/Right)
  for users who have macOS Mission Control shortcuts disabled
- Add tests for Ctrl+Left/Right word navigation behavior

Made-with: Cursor

* Fix Option+Left/Right in source editor by allowing it past BACK/FORWARD

Made-with: Cursor

* fix import order for checkstyle

Made-with: Cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: ui first contrib status: no-bot-comments status: to-be-merged PRs which are accepted and should go into the merge-queue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Text cursor movement shortcuts do not work correctly on macOS

4 participants