KeyEventsInViews

KeyEventsInViews is a sample that illustrates how to write a simple view, TKeyInputView, that handles key events. It mixes in MSimpleEventTarget, so that events are sent to it, and MKeyEventHandler, so that MSimpleEventTarget redispatches key events to it. When a key down occurs, HandleKeyDown is called. It handles the event and returns true to stop the event from being dispatched to other targets that might be in the target chain.

Running the sample

Execute KeyEventsInViewsSApp. A window appears and as you type, it displays the key code. Close the window to quit.

Files and classes

TKeyInputView is defined in KeyInputView.h and KeyInputView.C. It derives from TContentView, which derives from MEventTarget, and so does not have to mix this in directly. It also mixes in MKeyEventHandler, so that it can receive key events. TKeyInputView overrides KeyDown to record the key code associated with the most recent key event, and displays this key code when it draws.

Notes

The MEventTarget must be registered with a TStandardEventReceiver. Fortunately, the view system provides one for you to use. This sample view connects to the event receiver in HandleAfterAddedToViewRoot, and disconnects from it in HandleBeforeRemovedFromViewRoot.


Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.