Skip to content

Conversation

@7se7en72025
Copy link
Contributor

Fix backspace behavior after cut operation by resetting selection to cursor position.

Purpose / Description

After cutting text with CTRL+X in the note editor, pressing backspace would remove multiple characters (equal to the number of characters that were cut) instead of just one character. This bug occurred when using a physical keyboard with AnkiDroid.

Steps to reproduce the bug:

  1. Connect a physical keyboard
  2. Insert text to front/back field in note editor
  3. Select some text
  4. Press CTRL + X to cut
  5. Press Backspace
  6. Bug: Backspace removes multiple characters (equal to the number of characters cut)
  7. Expected: Backspace should remove only one character

Fixes

Approach

The issue was caused by the selection range not being properly reset after the cut operation. When text was cut using CTRL+X, Android's default cut behavior would remove the selected text, but the selection state (start and end positions) might still retain the old range. When backspace was pressed afterward, it would use this stale selection range instead of just removing a single character at the cursor position.

Solution:

  • Handle the cut operation explicitly in FieldEditText.onTextContextMenuItem()
  • Capture the cut position before calling the parent's cut implementation
  • After the cut operation completes, explicitly reset the selection to a single cursor position at the cut location
  • This ensures that subsequent backspace operations behave correctly by removing only one character

How Has This Been Tested?

Manual Testing:

  1. Connected a physical keyboard to an Android device/emulator
  2. Opened AnkiDroid note editor
  3. Inserted text into a field (e.g., "Hello World")
  4. Selected text (e.g., "World")
  5. Pressed CTRL+X to cut the selected text
  6. Pressed Backspace
  7. Result: Backspace now correctly removes only one character instead of multiple characters

Test Configuration:

  • Tested on Android emulator with physical keyboard input
  • Tested with various text selections (single word, multiple words, partial selections)
  • Verified that normal backspace behavior (without cut) still works correctly
  • Verified that cut operation itself still works correctly (text is removed and copied to clipboard)

Learning (optional, can help others)

Research:

  • Investigated Android's EditText behavior with keyboard shortcuts
  • Found that onTextContextMenuItem() is called for both context menu actions and keyboard shortcuts (CTRL+X, CTRL+V, etc.)
  • Discovered that after cut operations, the selection state can retain the old range, causing unexpected behavior with subsequent key presses
  • The fix follows the pattern already used in the codebase for handling paste operations

Key Insight:
Android's default cut implementation in EditText handles the clipboard and text removal, but doesn't always properly reset the selection state. By explicitly managing the selection after cut operations, we ensure consistent behavior.

Checklist

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
    • N/A: This is a bug fix for keyboard input behavior, no UI changes
  • UI Changes: You have tested your change using the Google Accessibility Scanner
    • N/A: No UI changes, only keyboard input behavior fix

Fix backspace behavior after cut operation by resetting selection to cursor position.
@david-allison
Copy link
Member

What causes this bug to occur in the first place? This doesn't occur on regular EditTexts

I suspect we can fix this in a more simple manner once the code which causes this issue is identified.

@david-allison david-allison added the Needs Author Reply Waiting for a reply from the original author label Nov 16, 2025
@david-allison
Copy link
Member

david-allison commented Nov 16, 2025

I stand corrected, I can replicate this on the Create Deck dialog (only if using the keyboard to select text)... interesting

@7se7en72025
Copy link
Contributor Author

The issue likely persists because pastePlainText() still uses setText(), which disrupts Android’s Editor state and leads to stale selection during cut operations. Switching to Editable.replace() / insert() methods for pasting should better preserve the Editor’s internal state and help resolve the problem.

@7se7en72025
Copy link
Contributor Author

Fixed root cause in FieldEditText by changing pastePlainText() to use Editable.replace() instead of setText(), preserving editor state and fixing cut/backspace issues. Create Deck dialog uses standard TextInputEditText, so issue there may differ. Should I check Create Deck dialog too or is this fix enough?

Replaced setText() with Editable.replace() to maintain Editor state during paste operations.
Copy link
Member

@david-allison david-allison left a comment

Choose a reason for hiding this comment

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

Can you confirm that you tested this? The fix doesn't work for me

AnkiDroid Version = 2.23.0alpha8-debug (ffcd536cc23c820a0d5354403c7335f35bff0418)  
Screen_recording_20251116_230548.webm

@7se7en72025
Copy link
Contributor Author

Screen_recording_20251117_050427.webm
yes it does in my emulator

@david-allison
Copy link
Member

The issue only occurs if:

  • The keyboard is used to select text (via shift + arrow keys)
  • Ctrl+X is used, not the on-screen 'cut' menu item

@7se7en72025
Copy link
Contributor Author

7se7en72025 commented Nov 17, 2025

Despite having my mathematics-3, i spent fixing this and was much fun. :)
also i did use shift + arrow keys only.
https://github.com/user-attachments/assets/0b0cf7a2-53df-410d-80a8-9968336a2df1

@david-allison
Copy link
Member

david-allison commented Nov 17, 2025

Much appreciated, but school takes priority, always! (we'll be here when you have free time, and my schooling significantly changed the direction of my life)


Anki-Android git:(patch-2) gh pr checkout --force 19500
From https://github.com/ankidroid/Anki-Android
 * branch                  refs/pull/19500/head -> FETCH_HEAD
HEAD is now at ffcd536cc2 Use Editable.replace() for pasting plain text

Running on an API 36 Tablet emulator

AnkiDroid Version = 2.23.0alpha8-debug (ffcd536cc23c820a0d5354403c7335f35bff0418)

Screen_recording_20251117_020230.webm

@7se7en72025
Copy link
Contributor Author

Thank you for your kind words! I haven’t made a commit with the changes yet , I’m getting this error right now. I’ll fix it after school :)
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Author Reply Waiting for a reply from the original author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backspace after cut removes more than one character in physical keyboard

2 participants