7
$\begingroup$

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?

$\endgroup$

3 Answers 3

5
$\begingroup$

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:

Demo

$\endgroup$
6
$\begingroup$
Button["Backspace", 
 SelectionMove[EvaluationNotebook[], Previous, Character];
 SelectionMove[EvaluationNotebook[], All, Character];
 NotebookDelete[EvaluationNotebook[]]]
$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – C. E.
    Oct 4, 2018 at 18:09
  • $\begingroup$ @C.E., thank you for the vote. Good point re the issue. $\endgroup$
    – kglr
    Oct 4, 2018 at 18:21
2
$\begingroup$

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:

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.