# How to create a button that deletes a character?

I am creating a template that can be used on a touch-screen to input data into an InputField. Following the example from the Basic Math Assistant palette, the general format of these buttons is:

Button[*text*,Inherited,ButtonData->*value*]


This approach works fine for numerical values as well as the decimal point. However, I'd also like to provide a button to delete a character so that the user can fix an error. How is this possible?

This almost works:

Button[
"Backspace",
SelectionMove[EvaluationNotebook[], Previous, Character];
SelectionMove[EvaluationNotebook[], All, Character];
NotebookApply[EvaluationNotebook[], ""]
]


The only problem, as far as I know, with this solution is that if the cursor is at the beginning of the line then it will delete the character after instead of before it:

Button["Backspace",
SelectionMove[EvaluationNotebook[], Previous, Character];
SelectionMove[EvaluationNotebook[], All, Character];
NotebookDelete[EvaluationNotebook[]]]

• It's worth noting that this has the same problem that my solution has, namely that if the cursor is placed at the beginning of the cell then it will delete cells that come after the cursor. +1 for NotebookDelete though. Oct 4, 2018 at 18:09
• @C.E., thank you for the vote. Good point re the issue.
– kglr
Oct 4, 2018 at 18:21

I would use a front end token for this purpose:

Button["Backspace", FrontEndTokenExecute["DeletePrevious"]]


This approach doesn't delete anything when the cursor is at the beginning of the input field.

Short animation: