| Previous | Next
Focus MethodsWhen your application is running, you can force a widget to have the keyboard focus by calling $widget->focus; You might want to do this if you have an Entry widget into which the user should start typing first. Calling There are several methods that allow you to manipulate the focus. To make the focus follow the mouse around, use $widget->focusFollowsMouse; To find out which widget has the focus, call $who = $widget->focusCurrent; To force a widget to have the focus even if the application isn't currently active, call $widget->focusForce; This is not a nice thing to do, so try to not use it. To find out which widget had the focus last, call $which = $widget->focusLast; If none of the widgets in the window has the focus, the Toplevel is returned. You can use the $nextwidget = $widget->focusNext; $prevwidget = $widget->focusPrev; So, what is focus order? First, focus order is constrained to Once a candidate widget to receive the focus is determined, the candidate widget's
Keyboard TraversalWhat should you do when you don't want to use your mouse in your Perl/Tk application? There are ways you can move around in your application without having to touch the mouse. Tabbing between widgetsRun any of the Perl/Tk applications you have and hit the Tab key. Assuming you haven't bound Tab to anything else, you'll see different widgets in your application get the focus, each in turn. You know a widget has the focus by a variety of ways. A Button will have a dotted or solid line drawn around it that wasn't there before it had the focus. An Entry will automatically select all the text in it when it has the focus. Only one widget in your application can have the focus at a time. When that widget has the focus, you are able to interact with it using the keyboard. With a widget such as Entry, this makes complete sense. You need to be able to type text into it using the keyboard. We'll talk about this in more detail in the next section. Not all widgets will take the focus. A Label doesn't accept any keyboard or mouse input, so it won't ever get the focus. A Text widget is special because once it has the focus, a Tab is rebound to enter a Tab as part of the Text. Check with documentation on each widget to determine if you can Tab out of the widget or not. The order in which the focus moves around matches the order that you packed the widgets into your application. If you are going to rely on using the Tab key to move between widgets in a logical fashion, you may need to redesign the packing order. Try using Shift-Tab; you'll now be moving between widgets in backwards order. Sometimes when Tab has been rebound to do something else, you can use Shift-Tab to get out of the widget and on to the next one (e.g., a Text widget). Default widget bindingsSo what happens when you start hitting other keys and a widget has the focus? A lot depends on what widget has the focus, because there are different built-in bindings for each widget. A Button will let you hit the spacebar to invoke it (this is true for a Button, Checkbutton, or Radiobutton). An Entry or Text widget will let you start typing text into the widget. A Listbox will let you use the arrow keys to move between different items in it. Each widget has its own set of default bindings that let you use the keyboard to interact with it. Check the documentation for each widget to determine what the default bindings are. You can also take a look at the Menu TraversalOne of the more common things you'll want to do with your application is allow shortcuts to commands that are in menus. Here is a typical file menu being created: my $filem = $menubar->cascade(-label => "~File", -tearoff => 0); $filem->command(-label => "~Open...", -command => \&open_file, -accelerator => "Ctrl+O"); $filem->command(-label => "~Close", -command => \&close_file, -accelerator => "Ctrl+W"); $filem->command(-label => "~Save", -command => \&save_file, -accelerator => "Ctrl+S"); $filem->command(-label => "Save ~As...", -command => \&saveas_file); You'll see we used the
The second binding on the Text widget is necessary only in some cases where the Text widget will actually parse the entered command to try and insert text into the widget. Without that additional binding, you'll get a funny little rectangle if the Text widget has the focus and you'll type Control-s. This type of conflict reminds us to check out the default bindings for each widget we are using in our application. We might be replacing default widget functionality. Take a look at "Anatomy of the MainLoop" for more information on bindings and the different ways to create them. |