Important: The information in this document is obsolete and should not be used for new development.
Recipes--The Keyboard
The recipes and sample code in this section demonstrate how to work with various kinds of keystrokes in a MacApp application.Working With the Keyboard--A General Outline
MacApp gives event-handler objects and behavior objects in the target chain a chance to handle keystrokes. Your application normally works with keystrokes in three ways:
- If the Command key is not held down when a key is pressed, MacApp calls the
HandleKeyCommandmethod of the current target object, which in turn calls theDoKeyEventmethod. To handle a keystroke in your application, do the following:
- Override the
DoKeyEventmethod of an event-handling or behavior class.- Use the
TToolboxEventparameter toDoKeyEventto determine if the key is one you wish to handle. Identify the key by examining the parameter'sfCharacterfield.- If the character is one you wish to handle, perform the desired action.
- If the character is not one you wish to handle, call
Inherited::DoKeyEventto give other event-handler and behavior objects in the target chain a chance to handle the keystroke.For a detailed example, see the next section, "Recipe--Handling a Keystroke Other Than a Command-Key Combination."
- If the keystroke is a Command-key combination and it has a menu command equivalent, MacApp calls the
DoMenuCommandmethod of the current target object, just as if the user had selected the menu command with the mouse.For a detailed example, see "Recipe--Responding to a Command-Key Combination," beginning on page 481.
- If the keystroke is a Command-key combination and it does not have a menu command equivalent, MacApp calls the
HandleCommandKeymethod of the current target object, which in turn calls theDoCommandKeyEventmethod. This call travels along the target chain until it is handled by one of the event-handler or behavior objects in the chain. To handle Command-key combinations of this type, do the following:
- Override the
DoCommandKeyEventmethod of an event-handling or behavior class.- Use the
TToolboxEventparameter toDoCommandKeyEventto determine whether the key is one you wish to handle. Identify the key that was pressed along with the Command key by examining the parameter'sfCharacterfield.- If the character is one you wish to handle, perform the desired action.
- If the character is not one you wish to handle, call
Inherited::DoCommandKeyEventto give other event-handler objects and behaviors in the target chain a chance to handle the keystroke.For a detailed example, see "Recipe--Responding to a Command-Key Combination," beginning on page 481.
Recipe--Handling a Keystroke Other Than a Command-Key Combination
To handle a keystroke other than a Command-key combination, you perform only one step.
The sample code shown in this recipe is from the DemoDialogs application.
- Override the
DoKeyEventmethod of an event-handling or behavior class.
- If the key is one you wish to handle, perform the desired action.
- If the key is not one you wish to handle, call
Inheritedto pass on the keystroke.
Override the DoKeyEvent Method of an Event-Handling or Behavior Class
The DemoDialogs sample application uses theTMonthlyDialogview class to display a window containing 12 number-entry views labeled with the months of the year. TMonthlyDialog overrides theDoKeyEventmethod to set the target to the view's window whenever the user presses the Option-Tab combination.The DoKeyEvent method uses the
fCharacterfield of theTToolboxEventparameter to determine whether the Tab key was pressed and uses the IsOptionKeyPressed method to check for the Option key. If the keystroke is Option-Tab, DoKeyEvent sets the current target object to the view's window. If not, it callsInheritedto give other objects in the target chain a chance to handle the keystroke. Your application can use similar code when it needs to process a simple keystroke or a particular keystroke combination.
- Note
- The DemoDialogs application processes the Option-Tab key combination to demonstrate setting the target object. However, your application may want to avoid using Option-Tab because MacApp uses that combination to tab between the mailer view and the document view for documents with PowerTalk mailers.
![]()
void TMonthlyDialog::DoKeyEvent(TToolboxEvent* event) // Override. { TWindow * theWindow = NULL; Boolean dummy; // If Option-Tab is pressed, make the view's window the current // target. This is for test purposes only. if ((event->fCharacter == chTab) && (event->IsOptionKeyPressed())) { // Get the view's window and make it the current target. // The return value indicates whether the window actually // became the target. Since we're just testing, ignore the // return value. theWindow = this->GetWindow(); if (theWindow != NULL) dummy = theWindow->BecomeTarget(); } else Inherited::DoKeyEvent(event); }Recipe--Responding to a Command-Key Combination
When your application receives a Command-key combination that has a menu command equivalent, MacApp calls theHandleMenuCommandmethod of the current target object, which in turn calls theDoMenuCommandmethod, passing the command constant for the menu command. Your application responds by overriding theDoMenuCommandmethod of an event-handling or behavior class, as described in "Recipe--Adding, Enabling, and Responding to a Menu Command," beginning on page 314.When your application receives a Command-key combination that does not have a menu command equivalent, MacApp calls the
DoCommandKeyEventmethod of the current target object. To respond to the Command-key combination, you perform only one step:
The sample code shown in this recipe is based on the
- Override the
DoCommandKeyEventmethod of an event-handling or behavior class.
- Determine whether the key is one you wish to handle.
- If so, perform the desired action.
- If not, call
Inheritedto pass on the keystroke.
TDialogBehaviorclass from the MacApp class library.Override DoCommandKeyEvent in an Event-Handling or Behavior Class
MacApp uses theTDialogViewview class to provide standard dialog-box behavior. TheTDialogViewclass has few methods and fields--most of its behavior is supplied by an attached behavior object of type TDialogBehavior. For example, TDialogBehavior overrides the DoCommandKeyEvent method to check for the Command-period combination. When it finds this combination, it cancels the dialog; otherwise, it calls Inherited::DoCommandKeyEvent to give other behaviors and event-handler objects in the target chain a chance to handle the keystroke.The TDialogBehavior::DoCommandKeyEvent method is shown below. It uses the
fCharacterfield of theTToolboxEventparameter to determine whether the period key was pressed. If the behavior has a view and the view is enabled and the keystroke is a period and the view can be dismissed, DoCommandKeyEvent attempts to cancel the dialog box. Otherwise, it callsInheritedto give other objects in the target chain a chance to handle the keystroke.
void TDialogBehavior::DoCommandKeyEvent(TToolboxEvent* event) // Override. { // Maps Command-period to the cancel item (view). TView* owner = ( TView* )fOwner; if (owner && owner->IsEnabled() && event->fCharacter == '.' && ((long)fCancelItem != (long)kNoIdentifier)) { TView* cancelView = owner->FindSubView(fCancelItem); if (cancelView) { if (cancelView->IsEnabled()) { cancelView->HandleEvent(cancelView->GetEventNumber(), owner, NULL); } } else owner->HandleEvent(mCancelKey, owner, NULL); } else Inherited::DoCommandKeyEvent(event); }