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:
MathLink`CallFrontEnd[
FrontEnd`BoxReferenceFind[
FE`BoxReference[
_NotebookObject, {{ID_String}},
FE`BoxOffset -> {FE`BoxChild[1]},
FE`SearchStart -> "StartFromBeginning"
]
]
]
This is a 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
DynamicModule[{name = "", surname = "", setFocus}
, Column[{
InputField[Dynamic@name, String, BoxID -> "name"]
, InputField[Dynamic@surname, String, BoxID -> "surname"]
, Button["setFocusToFirst", setFocus[EvaluationNotebook[], "name"]]
}]
, SynchronousInitialization -> False
, Initialization :> (
setFocus[nb_, ID_] := MathLink`CallFrontEnd[
FrontEnd`BoxReferenceFind[ FE`BoxReference[
nb
, {{ID}}
, FE`BoxOffset -> {FE`BoxChild[1]}
, FE`SearchStart -> "StartFromBeginning"
]]
]
; setFocus[EvaluationNotebook[], "surname"]
)
]