I'm making my own text editor, and I want paragraphs to always have extra space between them, so I've overridden EditText's OnKeyListener so that when Enter is pressed two newlines are inserted into the text and when Backspace is pressed when the cursor is at the start of a paragraph two characters are deleted instead of one.
Seems simple enough, but my problem is that if the Input Method (ie keyboard) has just finished editing a word when Enter is pressed and then Backspace is pressed, it will try to handle the Backspace itself by replacing that word with an underlined auto-correctible version of itself (overwriting the word with itself). However, the cursor has been moved forward two characters because of the extra newline and the Input Method doesn't know that, so when it does the overwrite it's off by one character.
Sorry if that was confusing; here's an example of the bug (vertical bar marks where the cursor is):
Write a word on an empty line
test|
Hit enter so a new line appears
test |
Press delete
ttest|
Things I've tried so far to fix the bug:
Using the OnKeyListener to handle backspace properly. Unfortunately, OnKeyListener only gets the key event after the Input Method has done its thing.
Using a TextWatcher to undo what the Input Method has done. Unfortunately, TextWatcher has no way of knowing if Backspace has been pressed or if the text it's seeing is the result of something else.
Discarding the whole idea of automatically adding extra newlines whenever Enter is pressed. Unfortunately, there doesn't seem to be any other way to add extra whitespace after a paragraph. As far as I know TextView's spans are only able to add horizontal margins to a paragraph, but not vertical ones.
So I'm left thinking that my best bet is trying to catch the Backspace before the Input Method gets it (OnKeyPreIme didn't work either, by the way). I'd rather not have to write my own IME entirely, and I'd like to give users the option of typing with whichever IME they prefer, but if I have to bundle a custom IME with the text editor then so be it. Even so, there's so little documentation on how autocorrect works and where it's implemented that I don't even know where to start.
Any help would be much appreciated; thanks in advance!