# How to set focus of a dialog window?

I have a dialog, which, when pops up, requests input from the user. How can I set the window up that the focus is on the InputField of the new window (i.e. the caret stands in the InputField), so that when the user starts typing, it is immediately registered by the field? At the moment, I have to click inside the field first to make it the active control on screen.

DialogInput[{InputField["", String], Button["Ok", DialogReturn[]]}]

-
I look forward to seeing the answer to this one. – Mr.Wizard Feb 7 '12 at 22:42

Since Input is a DialogInput, it seemed reasonable to peek into Input's structure to understand how the focus is set. After removing the ReadProtected attribute I've realized that there is no neat way to do it, as WRI itself has done the reposition of the focus via successive SelectionMove calls.

This example below is not the original but a modified version, as the original Input definition only includes one InputField but no other expressions (like a Button). Note that it is not documented that you can use Initialization in dialogs, moreover it is colored red by the syntax highlighter to suggest an invalid option.

DialogInput[{InputField["", String], Button["Ok", DialogReturn[]]},
Initialization :> (FrontEndExecute[{
FrontEndSelectionMove[#1, Before, Notebook,
AutoScroll -> False],
FrontEndSelectionMove[#1, Next, Cell, 2, AutoScroll -> False],
FrontEndSelectionMove[#1, Previous, CellContents,
AutoScroll -> False],
FrontEndFrontEndToken[#1, "MovePreviousPlaceHolder"]
}] &), ShowCellBracket -> True, Selectable -> True]

-
+1 for low level digging – Mr.Wizard Feb 8 '12 at 0:01
+1. I used similar code for my syntax highlighter generator, but wasn't succesful in trying to adopt it here. Good that you managed - another good example of how this is done. – Leonid Shifrin Feb 8 '12 at 9:22
@Mr.Wizard Leonid and Istvan, you all may be interested in fact that WRI changed approach and use now FrontEndReferenceBoxFind which is quite handy and more general. – Kuba Apr 2 at 7:38
@Kuba This seems interesting, but do you possess any further information about where, how, and who uses ReferenceBox* and BoxReference* functions? It is hard to experiment with them without any pointer on what they do. – István Zachar Apr 10 at 16:23
Only from my own tests, and not much. E.g you can put BoxID almost everythere and it can be simple string. – Kuba Apr 10 at 18:36

After István Zachar's points, I was investigating Input definitions to learn more. It seams that 2 years later WRI changed approach from SelectionMove based to more automatic BoxReferenceFind.

### usage

So what we only have to do is to set BoxID option for fields of interest and find those references when we want, with:

MathLinkCallFrontEnd[
FrontEndBoxReferenceFind[
FEBoxReference[_NotebookObject, {{_ID}}, FEBoxOffset -> {FEBoxChild[1]},
FESearchStart -> "StartFromBeginning"]]]


This is way more flexible approach, e.g. you can easily put InputField somewhere else and you don't have to change SelectionMove steps to get there.

### example

The following example sets initial focus on second InputField and you can use button to switch it to the first one, just for fun.

ClearAll @ setFocus;
setFocus[nb_, ID_] := MathLinkCallFrontEnd[ FrontEndBoxReferenceFind[
FEBoxReference[nb, {{ID}}, FEBoxOffset -> {FEBoxChild[1]},
FESearchStart -> "StartFromBeginning"]
]];

Composition[
CellPrint
,
Cell[#, "Output", CellDynamicExpression :> Refresh[
setFocus[EvaluationNotebook[], "surname"], None]
] /. DownValues[setFocus] &
,
BoxData
,
ToBoxes
][
DynamicModule[{name = "", surname = "", setFocus}
,
Column[{
InputField[Dynamic @ name,    String, BoxID -> "name"],
InputField[Dynamic @ surname, String, BoxID -> "surname"],
Button["setFocusToFirst", setFocus[EvaluationNotebook[], "name"]]}
]
]/. DownValues[setFocus]
]


### notes

Notice that I'm using CellDynamicExression, it is because Initialization often fires before Dynamic cells or cell at all are even created so setting focus then would have no effect. CellDynamicExpression fires later, where the content is in place and everything works smoothly. (in this example it does not matter but may for more complicated ones).

-
It is great that this can be done. It is a travesty that users have to poke around in the internals of Mathematica to accomplish a simple task like this. – John McGee May 1 at 10:21
Now it's only missing the tab key jump order control ! – P. Fonseca May 4 at 11:48
@P.Fonseca On Windows it switches InputFields with Tab quite well. – Kuba May 4 at 11:55
Right. But through which order? Can you change the default order? In most visual languages, you can easily change the order of these jumps... – P. Fonseca May 4 at 12:33
@P.Fonseca what do you mean by order in case of nonregular layout full of InputFields? You want to be able to provide list of tags (here BoxID) to go through when pressing Tab? – Kuba May 4 at 12:39

What's wrong with Input[""]?

-
I must assume it is not general enough, that he is building an entire dialog and that example given is quite reduced. – Mr.Wizard Feb 7 '12 at 22:51
Nothing is wrong with Input but as Spartacus has pointed out, it is not general enough. Nevertheless, this pointed me to the right direction, so thanks David! – István Zachar Feb 7 '12 at 23:29
Spartacus is right. My suggestion was really aimed at quick fix. By the way, István, how did you manage to peek into Input's structure? – David Carraher Feb 8 '12 at 0:04
@David: The usual way: by removing all attributes (Attributes[Input] = {}) and then querying the definition as ??Input. This trick could give useful insight only if the symbol is ReadProtected` (meaning that most of its definition is written in Mathematica instead of C or whatnot), or not even then. – István Zachar Feb 8 '12 at 0:13
Thanks, István. – David Carraher Feb 8 '12 at 0:25