Focus Highlighting

The focus highlighting features allow keyboard interaction with Tk interfaces. They work best when used in combination with the tab group widget. When focus highlighting is activated (by sourcing the relevant bindings into the Tcl script), the widget which currently has the keyboard focus is highlighted by drawing a coloured 2D border around the outside of the widget.

Synopsis

focuspolicy ?mode?
When no mode is given, this command returns the current focus policy mode. If the focus policy mode is given, it sets the focus policy to the given mode. Mode can either be explicit or pointer. When the focus policy is explicit, the keyboard focus is given to a widget by clicking on it with the mouse or via keyboard traversal (via tab groups). When the focus policy is pointer, the keyboard focus is given to the widget containing the mouse pointer.

The focus policy is set automatically when the focus highlighting module is loaded by getting the value of the X resource .focusPolicy (class FocusPolicy).

Private Procedures and Data

The following procedures and data are defined by the focus highlighting module. They are used by the event-bindings created by the module when it is sourced and should not generally be used by scripts.

__showfocus widget
If the widget argument is the empty string, the __showfocus procedure removes any focus highlight. If it is a widget path name, it displays the focus highlight around the given widget. This procedure is bound to the FocusIn and FocusOut events on all widgets except frames and top level widgets.
__resizefocus widget
Resize the focus highlight so that it is surrounds widget properly. This is bound to the Configure event on all widgets.
__showfocus(element)
This array holds information used by the event bindings and private procedures.

Note: since all widget classes have bindings for the FocusIn, FocusOut and Configure events, new bindings for these events must be added using the widget :: add_binding proc.

Keyboard bindings

The focus highlighting module binds event handlers to widgets to allow keyboard interaction. These handlers are:

Button, Radiobutton and Checkbutton wudgets
The space key invokes the button if it is not disabled (ie: it acts as a mouse-click).
Entry and Text widgets
The standard Tk bindings apply to these widgets.
Listboxes
Standard Tk listbox widgets cannot easily be used via the keyboard since they provide no way of indicating which list item currently has the keyboard focus. However, if a listbox is in single-select mode, the selected item and the item with the keyboard focus are one and the same. Because of this, the focus-highlighting module redefines the tk_listboxSingleSelect procedure to add bindings to listboxes allowing them to be used via the keyboard.

Resources

The focus highlighting module gets its parameters from the X resource database via the following resources:

Name: .focusPolicy
Class: FocusPolicy
Type: either explicit or pointer
Default value: explicit
The policy used in assigning the keyboard focus. Pointer means that the widget with the mouse pointer receives keyboard input; explicit means that the keyboard focus must be moved via the cursor and tab keys (the tabgroup widgets must be used to allow keyboard navigation.

Name: .focusHighlightWidth
Class: FocusHighlightWidth
Type: screen units
Default value: 2
The width of the highlight used to indicate which widget currently has the keyboard focus.

Name: .focusHighlightColor
Class: FocusHighlightColor
Type: X color
Default value: red
The color of the highlight used to indicate which widget currently has the keyboard focus.

Autoloading and Initialisation

The focus highlighting module can be loaded using the source command. However, if the Itcl widget library is installed so as to take advantage of Tcl's auto-loading system one can load the module by invoking the command focuspolicy at the top of the script.


Nat Pryce (np2@doc.ic.ac.uk)