KeyBehaviors
KeyBehaviors is a sample that illustrates how to attach behaviors
to an MEventTarget so that the behaviors get first chance at the
events. In this sample, a subclass of MEventTarget and
MKeyEventHandler is defined, TKeyFilterBehavior, that "swallows"
certain key events. When this behavior is adopted by another
MEventTarget, it prevents those key events from reaching it. In this
sample the TKeyInputView class from the previous examples is asked to
adopt the behavior.
Running the sample
Execute KeyBehaviorsSApp. With the window active, type some text.
Only events that pass the filter (those that match characters in the
string 'Taligent') are passed on to the view. Close the window to
quit.
Files and classes
TKeyInputView, defined in KeyInputView.h and KeyInputView.C, is
much the same as TKeyInputView from KeyEventsInViews. The main
difference is that it displays the key text, rather than the key code.
TKeyFilterBehavior, defined in KeyFilterBehavior.h and
KeyFilterBehavior.C, derrives from MEventTarget and MKeyEventHandler
and overrides KeyDown to filter out events that don't generate
characters in the target text 'Taligent'. A true result from KeyDown
indicates that the event is handled, effectively "swallowing" it.
KeyBehaviorMain.C simply instantiates the TKeyInputView and
TKeyFilterBehavior, and has the view adopt it with
MEventHandler::AdoptBehavior.
Notes
By using behaviors, the TKeyInputView class can be unaware of the
behavior class, and conversely the behavior class can be unaware of
the view.
The list of attached behaviors can be updated dynamically.
Behaviors can even themselves have behaviors, so that you can in
theory construct complicated behaviors from simpler parts.
Because the event is not const, behaviors can also modify the
event passed to them.
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.