Parent document is top of "Motif FAQ (Part 6 of 9)"
Previous document is "202) How can I determine which widget has keyboard focus?"
Next document is "204) How can I have a modal dialog which has to be answered before"

203) How can I direct the keyboard input to a particular widget?

Answer:  In Motif 1.1 call XmProcessTraversal(target, XmTRAVERSE_CURRENT).
The widget (and all of its ancestors) does need to be realized BEFORE you call
this. Otherwise it has no effect.  XmProcessTraversal is reported to have many
bugs, so it may not work right.  A common occurrence is that it doesn't move
to the widget, but if you call XmProcessTraversal *twice* in a row, it will.
If you can't get it to work, try this from Kee Hinckley:

    // This insane sequence is as follows:
    //      On manage set up a focus callback
    //      On focus callback set up a timer (and get rid of focus callback!)
    //      On timer set the focus (which only works if the parent
    //      has the focus,
    //      which is why we went through all of this garbage)
    // There may be a better way, but I haven't time to try it now.
    //
    static void focusTO(void *data, XtIntervalId *) {
        XmProcessTraversal((Widget) data, XmTRAVERSE_CURRENT);
    }

    static void focusCB(Widget w, XtPointer data, XtPointer) {
        XtRemoveCallback(w, XmNfocusCallback, focusCB, data);
        XtAppAddTimeOut(XtWidgetToApplicationContext(w), 0, focusTO, data);
    }

    void OmXSetFocus(Widget parent, Widget w) {
        XtAddCallback(parent, XmNfocusCallback, focusCB, w);
    }


In Motif 1.0 call the undocumented _XmGrabTheFocus(target).

Do not use the X or Xt calls such as XtSetKeyboardFocus since this bypasses
the Motif traversal layer and can cause it to get confused.  This can lead to
odd keyboard behaviour elsewhere in your application.

Parent document is top of "Motif FAQ (Part 6 of 9)"
Previous document is "202) How can I determine which widget has keyboard focus?"
Next document is "204) How can I have a modal dialog which has to be answered before"