1
$\begingroup$

If you press Enter in the example below it should become clear that test is being adding to the textbox instead of the current Notebook.

EventHandler[
 InputField["", String]
 , {"ReturnKeyDown" :> (
    SetSelectedNotebook[EvaluationNotebook[]];
    SelectionMove[EvaluationNotebook[], Notebook, All];
    NotebookWrite[EvaluationNotebook[], "test"]
    )}]

How might I force the text to be added to the Notebook instead of the other InputField?

EDIT: Ideally I am looking for a solution that doesn't involve removing focus from the InputField.

$\endgroup$
2
  • 2
    $\begingroup$ Does SelectionMove[EvaluationNotebook[], After, Cell]; inested of ..Notebook, All] works like you want? $\endgroup$
    – Kuba
    Commented Sep 8, 2013 at 0:02
  • $\begingroup$ @Kuba Ideally I was looking for a solution that didn't involve removing focus from the InputField. I will add that to the question. But yes that does partially work. $\endgroup$
    – William
    Commented Sep 8, 2013 at 0:52

1 Answer 1

4
$\begingroup$

For version 9 or above, one can use CellPrint (or simply Print, as it is a specific case of CellPrint) which does not move the selection and thus preserves the focus on the InputField.

EventHandler[InputField["", String],
    {"ReturnKeyDown" :> CellPrint[Cell["test", "Output"]]}
]

Since the above code only works in v9, here is an equivalent with tedious selection-manipulation. First I set up the EventHandler as a cell, so that I can add a cell tag ("input") to it. This is necessary to be able to navigate the selection back (NotebookFind) after using Enter in the InputField, as one doesn't know how many extra cells will be generated. Using NotebookSelection however can only get us before the InputField, but not inside. To move the caret inside, a specific FrontEndToken is used ("MovePreviousPlaceHolder"). Perhaps someone knows a shorter method.

nb = EvaluationNotebook[];
CellPrint[ExpressionCell[
  EventHandler[InputField[Dynamic@test, String], {"ReturnKeyDown" :> (
      SelectionMove[nb, After, Notebook, AutoScroll -> False];
      NotebookWrite[nb, Cell[test, "Output"], AutoScroll -> False];
      NotebookFind[nb, "input", Previous, CellTags, AutoScroll -> False];
      SelectionMove[nb, Before, CellContents, AutoScroll -> False];
      FrontEndExecute@FrontEnd`FrontEndToken[nb,
            "MovePreviousPlaceHolder", AutoScroll -> False];
      SelectionMove[nb, After, Characters, AutoScroll -> False];
      )}],
   "Output", CellTags -> {"input"}]]
$\endgroup$
5
  • $\begingroup$ The cell is printed to the messages window for me. I'm on v8 what version of Mathematica are you on? $\endgroup$
    – William
    Commented Sep 8, 2013 at 13:31
  • $\begingroup$ @Liam I was a bit afraid of this, as usually Print statements inside a dynamic gui controller get printed to the messages window, but for me, CellPrint here consistently prints to the actual notebook under v9.0.1. let me investigate this. $\endgroup$ Commented Sep 8, 2013 at 13:34
  • $\begingroup$ @Liam. the above code works fine for me using V.9.0.1 on OS X 10.6.8 $\endgroup$
    – m_goldberg
    Commented Sep 8, 2013 at 14:31
  • $\begingroup$ @IstvánZachar +1 and accepted. Thank you for the solution for v8. Both are appreciated ;) $\endgroup$
    – William
    Commented Oct 22, 2013 at 2:55
  • $\begingroup$ Thanks @Liam, I hope it really helps :) $\endgroup$ Commented Oct 22, 2013 at 7:52

Your Answer

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

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