beetlebug▾
/

focus

Gives or sets the widget that gets the keys of the keyboard.

focus
focus WINDOW
focus -displayof WINDOW
focus -force WINDOW
focus -lastfor WINDOW

Description

The focus widget gets each key event of the application. With no argument, focus gives its name, or an empty string when no widget of the application has the focus. With WINDOW, focus gives the focus to WINDOW and gives an empty string. An empty WINDOW leaves the focus as it is.

A change of the focus sends <FocusOut> to the old widget and <FocusIn> to the new one. The Tab key moves the focus to the next widget with -takefocus. When the focus widget is destroyed, no widget has the focus.

Options

OptionTypeDefaultDescription
-displayofwindow namenoneGives the focus widget of the display of WINDOW.
-forcewindow namenoneGives the focus to WINDOW, also when the application does not have the focus of the platform.
-lastforwindow namenoneGives the widget that gets the focus when the toplevel of WINDOW gets the focus.

-displayof WINDOW

Gives the focus widget of the display of WINDOW.

ztk has one display for each application. The result is the focus widget while the platform window of the application has the focus, and an empty string while it does not.

% entry .d1
> pack .d1
> focus .d1
> focus -displayof .
.d1
% focus -displayof .nope
bad window path name ".nope"
-errorcode NONE

% focus -displayof
wrong # args: should be "focus -displayof window"
-errorcode NONE

-force WINDOW

Gives the focus to WINDOW, also when the application does not have the focus of the platform.

In Tk, -force also asks the platform to give the focus to the application. In ztk, focus -force WINDOW is the same as focus WINDOW.

% entry .fo
> pack .fo
> focus -force .fo
> focus
.fo
% focus -force .nope
bad window path name ".nope"
-errorcode NONE

-lastfor WINDOW

Gives the widget that gets the focus when the toplevel of WINDOW gets the focus.

When the application has no focus widget, the result is the toplevel of WINDOW. When the application has a focus widget, ztk gives that widget, also when it is in another toplevel.

% entry .lf
> pack .lf
> focus .lf
> focus -lastfor .
.lf
% focus -lastfor .nope
bad window path name ".nope"
-errorcode NONE

% focus -bogus .
bad option "-bogus": must be -displayof, -force, or -lastfor
-errorcode NONE

Returns

The name of a widget, or an empty string.

Examples

% entry .e1
> entry .e2
> pack .e1 .e2
> focus .e2
> focus
.e2

% bind .e1 <FocusIn> {lappend ::got %W}
> focus .e1
> update
> set got
.e1

# An empty WINDOW leaves the focus as it is.
% focus {}
> focus
.e1

Errors

% focus .nope
bad window path name ".nope"
-errorcode NONE

% focus a b c
bad option "a": must be -displayof, -force, or -lastfor
-errorcode NONE

Differences from Tcl

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztk 1.0.0-beta.1

See also