Skip to content

Missing macOS Cmd+Backspace (meta BACK_SPACE) key binding for text components #1095

Description

@FoxyBOA

Description

FlatLaf provides excellent macOS-native key bindings for text components, including:

  • alt BACK_SPACEdelete-previous-word
  • meta LEFT / meta RIGHTbeginLineAction / endLineAction
  • shift meta LEFT / shift meta RIGHT → select to line boundaries

However, meta BACK_SPACE (Cmd+Backspace) → "delete to beginning of line" is missing. This is a standard Cocoa/NSTextField behavior that macOS users expect in text fields.

Note: Apple's own AquaLookAndFeel also does not provide this binding, so this would be an improvement over the system L&F.

Expected behavior

Pressing Cmd+Backspace in a text field on macOS should delete all text from the caret position to the beginning of the current line (same as native macOS text editing in Safari, Notes, TextEdit, etc.).

Current behavior

Nothing happens. The key combination is not bound to any action.

Suggested implementation

  1. Add mac("meta BACK_SPACE"), "delete-to-line-start" to text component input maps in FlatInputMaps
  2. Register a TextAction that uses Utilities.getRowStart() to find the line start and Document.remove() to delete the text

This would be consistent with the existing macOS key binding set that FlatLaf already provides.

Workaround

Custom TextAction installed per-component via KeyboardFocusManager focus listener:

final int menuMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
final KeyStroke cmdBackspace = KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, menuMask);

KeyboardFocusManager.getCurrentKeyboardFocusManager()
    .addPropertyChangeListener("focusOwner", evt -> {
        if (!(evt.getNewValue() instanceof JTextComponent)) return;
        JTextComponent tc = (JTextComponent) evt.getNewValue();
        if (tc.getClientProperty("delete-to-line-start") != null) return;

        tc.getInputMap().put(cmdBackspace, "delete-to-line-start");
        tc.getActionMap().put("delete-to-line-start", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JTextComponent comp = (JTextComponent) e.getSource();
                if (!comp.isEditable()) return;
                try {
                    int caretPos = comp.getCaretPosition();
                    int lineStart = Utilities.getRowStart(comp, caretPos);
                    if (lineStart < caretPos) {
                        comp.getDocument().remove(lineStart, caretPos - lineStart);
                    }
                } catch (BadLocationException ex) { }
            }
        });
        tc.putClientProperty("delete-to-line-start", Boolean.TRUE);
    });

Environment

  • FlatLaf version: 3.7
  • OS: macOS
  • Java: 8+

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions