Updated 2017-12-12 00:00:03 by MG

Utility procedure for managing the input focus.

Manual edit


tk_focusNext window


tk_focusNext is a utility procedure used for keyboard traversal. It returns the "next" window after window in focus order. The focus order is determined by the stacking order of windows and the structure of the window hierarchy. Among siblings, the focus order is the same as the stacking order, with the lowest window being first. If a window has children, the window is visited first, followed by its children (recursively), followed by its next sibling. Top-level windows other than window are skipped, so that tk_focusNext never returns a window in a different top-level from window.

After computing the next window, tk_focusNext examines the window's -takefocus option to see whether it should be skipped. If so, tk_focusNext continues on to the next window in the focus order, until it eventually finds a window that will accept the focus or returns back to window.

Focus Order and clipping order using the pack geometry manager edit

HaO: The focus order follows Stacking Order while the clipping order is controled by packing order (first packed, first seen). To achieve different clipping orders and focus orders, one may create and pack widgets in different order (or use the raise or lower commands).

Examples: First, two buttons obscure an entry widget, if place is limited (resize window). Tab order is backward as desired.
pack [button .b1 -text Button1] [button .b2 -text Button2] -side right
pack [entry .e -width 30] -side left

To change tab-order, one may raise the .b2 and the .e widget:
pack [button .b1 -text Button1] [button .b2 -text Button2] -side right
pack [entry .e -width 30] -side left
raise .b1 .b2
raise .e .b1

A (IMHO) more natural way to achieve the same configuration, one may create widgets in focus order and pack widgets in clipping order:
entry .e -width 30
button .b1 -text Button1
button .b2 -text Button2
pack .b2 .b1 -side right
pack .e -side left

Key binding edit

To assign the functionality of the Tab key to other keys (ex. Return key), one may use two alternatives:
bind .w <Return> {focus [tk_focusNext %W]}
bind .w <Return> {event generate %W <Tab>}

The first only switches focus. The second performs additional tasks, like selecting the current entry if it gains focus to an entry box.

MG By default tk_focusNext fires as part of the <<NextWindow>> event binding (which is bount to <Tab>), and does:
  tk::TabToWindow [tk_focusNext %W]

See also edit