focus(3tk)

๐Ÿ“ฆ
version 9.0.4 ยท 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

๐Ÿ’– NAME ๐Ÿ’–

ok so bestie, this is focus from Tk's built-in commands โ€” it's the one that manages the input focus. that's the whole vibe. ๐ŸŽฏ


โœจ SYNOPSIS โœจ

focus
focus window
focus option ?arg ...?

๐ŸŽ€ DESCRIPTION ๐ŸŽ€

The focus command is the one you call when you need to wrangle the Tk input focus. fr fr, at any given moment, exactly one window on each display is the designated focus window โ€” and every key press or key release event for that display gets yeeted straight to it. ๐Ÿ‘‘

Now, normally it's the window manager's job to redirect the focus around the top-level windows of a display. and like, every window manager has its own personality here: some of them will auto-set the input focus to a top-level window the second your mouse wanders into it, while others are lowkey stubborn and only redirect the focus when you actually click on a window. usually the window manager only bothers setting the focus to top-level windows, and then it's on the application to redirect the focus among the children of that top-level. ๐Ÿช†

Here's the part that's giving main character energy: Tk remembers one focus window for each top-level โ€” specifically, the most recent descendant of that top-level to receive the focus. so when the window manager hands the focus to a top-level, Tk automatically redirects it to that remembered window. ๐Ÿ’ซ within a top-level, Tk uses an explicit focus model by default. meaning: moving the mouse around inside a top-level does not normally change the focus. nope. the focus changes only when a widget explicitly decides to claim it (like, say, because of a button click), or when the user types a key such as Tab that moves the focus. ๐Ÿฆ‹

The Tcl procedure tk_focusFollowsMouse can be invoked if you want an implicit focus model instead: it reconfigures Tk so that the focus is set to a window whenever the mouse enters it. ๐Ÿญโœจ and the Tcl procedures tk_focusNext and tk_focusPrev implement a focus order among the windows of a top-level; they're what's used in the default bindings for Tab and Shift-Tab, among other things. iykyk.

The focus command can take any of the following forms:

  • focus โ€” Returns the path name of the focus window on the display containing the application's main window, or an empty string if no window in this application has the focus on that display.
๐Ÿ’€ note: it is better to specify the display explicitly using -displayof (see below) so that the code will work in applications using multiple displays. no cap, do the explicit thing.
  • focus window โ€” If the application currently has the input focus on window's display, this command resets the input focus for window's display to window and returns an empty string. If the application does not currently have the input focus on window's display, window will be remembered as the focus for its top-level; the next time the focus arrives at the top-level, Tk will redirect it to window. If window is an empty string then the command does nothing. (nothing. zero. nada. ๐Ÿ™ˆ)
  • focus -displayof window โ€” Returns the name of the focus window on the display containing window. If the focus window for window's display is not in this application, the return value is an empty string.
  • focus -force window โ€” Sets the focus of window's display to window, even if the application does not currently have the input focus for the display.
โš ๏ธ bestie, this command should be used sparingly, if at all. in normal usage, an application should not claim the focus for itself; instead it should wait for the window manager to give it the focus. like, be patient, the wm will get there. ๐Ÿ™ If window is an empty string then the command does nothing.
  • focus -lastfor window โ€” Returns the name of the most recent window to have the input focus among all the windows in the same top-level as window. If no window in that top-level has ever had the input focus, or if the most recent focus window has been deleted, then the name of the top-level is returned. this return value is the window that will receive the input focus the next time the window manager gives the focus to the top-level. ๐Ÿ”ฎ

๐Ÿซถ QUIRKS ๐Ÿซถ

ok so this is the funky part. When an internal window receives the input focus, Tk does not actually set the X focus to that window. as far as X is concerned? the focus stays sitting on the top-level window that contains the window with the focus. ๐Ÿ‘€

BUT โ€” and this is the slay โ€” Tk still generates FocusIn and FocusOut events just as if the X focus were on the internal window. โœจ this approach gets around a whole pile of problems that would happen if the X focus were actually moved. the fact that the X focus is on the top-level is invisible unless you go use C code to query the X server directly. sneaky. love that for us. ๐Ÿ’…


๐ŸŒˆ EXAMPLE ๐ŸŒˆ

say you want a window that only participates in the focus traversal ring when a variable is set โ€” add the following bindings to the widgets before and after it in that focus ring:

button .before -text "Before"
button .middle -text "Middle"
button .after  -text "After"
checkbutton .flag -variable traverseToMiddle -takefocus 0
pack .flag -side left
pack .before .middle .after
bind .before <Tab> {
   if {!$traverseToMiddle} {
      focus .after
      break
   }
}
bind .after <Shift-Tab> {
   if {!$traverseToMiddle} {
      focus .before
      break
   }
}
focus .before

๐Ÿงš KEYWORDS ๐Ÿงš

events, focus, keyboard, top-level, window manager


and that's the whole page, bestie ๐Ÿ’œ this was Tk 4.0, focus(3tk) โ€” written from the Tk Built-In Commands corner of the manual. go forth and focus responsibly. ๐ŸŒธ๐ŸŽ€โœจ

๐Ÿ“ฆ where this page comes from