On_Key

See Also: Accelerator Key Assignments, KeyAction method, Class, Object

Purpose

To assign a message to be sent upon the pressing of a key assigned to an accelerator key function, or of a particular key on the keyboard.

To add custom key handlers in Web Applications, see AddKeyHandler in the Class Reference.

Syntax

On_Key {KeyName} Send {Message}

What It Does

With On_Key, you can assign a message (message) to be sent upon the pressing of a particular key. Message may be a predefined message or user-defined. A key to which a message has been assigned with On_Key is called an accelerator key.

KeyName may be the name of a physical key on a keyboard or one of the function names mapped to physical keys. The physical references always point to one actual key on the keyboard, while function references can be remapped to different physical keys.

Example

On_Key KCancel send AskIfUserWantsToCancel

Example

On_Key Key_Alt+Key_T send DoTest

The logical key functions of DataFlex bear "generic" names that describe processes that are common throughout applications generally. As such, they can be used in different objects to perform processes that, although not identical, still bear a functional resemblance to each other. KHelp, kUpArrow, kCancel, and kSave_Record are examples of logical key names. Logical key functions are used extensively in On_Key commands in the object classes supplied with DataFlex, and provide a good example of this process of "functional analogy". Otherwise, it is up to the developer to maintain consistency of usage across objects, object classes, programs, and applications.

Message may not have arguments. Only messages that function without arguments may be called from the On_Key message parameter. Message is sent to the focus object.

Accelerator keys are typically defined either within the object creation block or the Construct_Object method of a class. When the On_Key command is used outside of all object definition blocks and classes, the key is assigned to the Desktop Object. As with all messages, the keys of the desktop are inherited by all objects. These keys become global throughout the application.

Key definitions of On_Key commands are inherited by the descendants of an object. If a key is defined in an object, all of the object's descendents will also have the same key definition. If the key is redefined in one of the descendents, then the definition in the ancestor is overridden, and the descendent (and all of its descendants) will use the new definition of the key. If the private keyword is used, then the inheritance is not performed. The key will operate only if the object where the key is defined is the focus object.

Objects act on messages received through an On_Key command irrespectively of whether the sending key is defined in the receiving object or one of its ancestors. If the On_Key message is process_key, however, the message defined for the key locally or in an ancestor is received, just as if the key had been typed from the keyboard.

There is no limit to the number of keys that an object may define.

Default_Key

Default_Key may be sent via the On_Key command to restore the standard accelerator for an object. See Default_Key for details.