Focus Options

In an application, you can tab between widgets to make them available for input from the keyboard. The application indicates that a widget is available for keyboard input by drawing an outline around it in black (this is called the highlight rectangle; see Figure 4-37). If a widget has this outline around it, it is said to have the focus of the application. (You can force the focus of an application to start with a specific widget by using $widget->focus;.) Once a Button has the focus, you can use the spacebar on your keyboard to activate it instead of using the mouse.

The first Button has the input focus

Figure 4-37. The first Button has the input focus

You can force the application not to allow your Button to receive the keyboard focus at all by using the -takefocus option:

-takefocus => 0 | 1 | undef

The -takefocus option is normally set to an empty string (undef), which allows the application to dynamically decide if the widget will accept focus. If a widget has its state set to 'disabled', it will be skipped over when users tab through all the widgets. To have the application always ignore the widget when tabbing through, use -takefocus => 0. To have the application always allow focus to the widget, use -takefocus => 1.

Get Mastering Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.