1focus(n) Tk Built-In Commands focus(n)
2
3
4
5______________________________________________________________________________
6
8 focus - Manage the input focus
9
11 focus
12 focus window
13 focus option ?arg arg ...?
14_________________________________________________________________
15
16
18 The focus command is used to manage the Tk input focus. At any given
19 time, one window on each display is designated as the focus window;
20 any key press or key release events for the display are sent to that
21 window. It is normally up to the window manager to redirect the focus
22 among the top-level windows of a display. For example, some window
23 managers automatically set the input focus to a top-level window when‐
24 ever the mouse enters it; others redirect the input focus only when
25 the user clicks on a window. Usually the window manager will set the
26 focus only to top-level windows, leaving it up to the application to
27 redirect the focus among the children of the top-level.
28
29 Tk remembers one focus window for each top-level (the most recent
30 descendant of that top-level to receive the focus); when the window
31 manager gives the focus to a top-level, Tk automatically redirects it
32 to the remembered window. Within a top-level Tk uses an explicit focus
33 model by default. Moving the mouse within a top-level does not nor‐
34 mally change the focus; the focus changes only when a widget decides
35 explicitly to claim the focus (e.g., because of a button click), or
36 when the user types a key such as Tab that moves the focus.
37
38 The Tcl procedure tk_focusFollowsMouse may be invoked to create an
39 implicit focus model: it reconfigures Tk so that the focus is set to a
40 window whenever the mouse enters it. The Tcl procedures tk_focusNext
41 and tk_focusPrev implement a focus order among the windows of a top-
42 level; they are used in the default bindings for Tab and Shift-Tab,
43 among other things.
44
45 The focus command can take any of the following forms:
46
47 focus Returns the path name of the focus window on the display con‐
48 taining the application's main window, or an empty string if no
49 window in this application has the focus on that display.
50 Note: it is better to specify the display explicitly using
51 -displayof (see below) so that the code will work in applica‐
52 tions using multiple displays.
53
54 focus window
55 If the application currently has the input focus on window's
56 display, this command resets the input focus for window's dis‐
57 play to window and returns an empty string. If the application
58 does not currently have the input focus on window's display,
59 window will be remembered as the focus for its top-level; the
60 next time the focus arrives at the top-level, Tk will redirect
61 it to window. If window is an empty string then the command
62 does nothing.
63
64 focus -displayof window
65 Returns the name of the focus window on the display containing
66 window. If the focus window for window's display is not in this
67 application, the return value is an empty string.
68
69 focus -force window
70 Sets the focus of window's display to window, even if the appli‐
71 cation does not currently have the input focus for the display.
72 This command should be used sparingly, if at all. In normal
73 usage, an application should not claim the focus for itself;
74 instead, it should wait for the window manager to give it the
75 focus. If window is an empty string then the command does noth‐
76 ing.
77
78 focus -lastfor window
79 Returns the name of the most recent window to have the input
80 focus among all the windows in the same top-level as window. If
81 no window in that top-level has ever had the input focus, or if
82 the most recent focus window has been deleted, then the name of
83 the top-level is returned. The return value is the window that
84 will receive the input focus the next time the window manager
85 gives the focus to the top-level.
86
88 When an internal window receives the input focus, Tk does not actually
89 set the X focus to that window; as far as X is concerned, the focus
90 will stay on the top-level window containing the window with the focus.
91 However, Tk generates FocusIn and FocusOut events just as if the X
92 focus were on the internal window. This approach gets around a number
93 of problems that would occur if the X focus were actually moved; the
94 fact that the X focus is on the top-level is invisible unless you use C
95 code to query the X server directly.
96
98 To make a window that only participates in the focus traversal ring
99 when a variable is set, add the following bindings to the widgets
100 before and after it in that focus ring:
101 button .before -text "Before"
102 button .middle -text "Middle"
103 button .after -text "After"
104 checkbutton .flag -variable traverseToMiddle -takefocus 0
105 pack .flag -side left
106 pack .before .middle .after
107 bind .before <Tab> {
108 if {!$traverseToMiddle} {
109 focus .after
110 break
111 }
112 }
113 bind .after <Shift-Tab> {
114 if {!$traverseToMiddle} {
115 focus .before
116 break
117 }
118 }
119 focus .before
120
121
123 events, focus, keyboard, top-level, window manager
124
125
126
127Tk 4.0 focus(n)