# How to preserve focus on InputField after Print?

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.

-
Does SelectionMove[EvaluationNotebook[], After, Cell]; inested of ..Notebook, All] works like you want? –  Kuba Sep 8 '13 at 0:02
@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. –  Liam William Sep 8 '13 at 0:52

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@FrontEndFrontEndToken[nb,
"MovePreviousPlaceHolder", AutoScroll -> False];
SelectionMove[nb, After, Characters, AutoScroll -> False];
)}],
"Output", CellTags -> {"input"}]]

-
The cell is printed to the messages window for me. I'm on v8 what version of Mathematica are you on? –  Liam William Sep 8 '13 at 13:31
@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. –  István Zachar Sep 8 '13 at 13:34
@Liam. the above code works fine for me using V.9.0.1 on OS X 10.6.8 –  m_goldberg Sep 8 '13 at 14:31
@IstvánZachar +1 and accepted. Thank you for the solution for v8. Both are appreciated ;) –  Liam William Oct 22 '13 at 2:55
Thanks @Liam, I hope it really helps :) –  István Zachar Oct 22 '13 at 7:52