You can programmatically map macros or other actions to keys or key combinations. This allows you to modify how a key is mapped without replacing a user's customized keyboard map.
This example maps the Ctrl+B key to a simple macro that gets text from the screen and prints it to the Immediate window.
You can use events to programmatically map a key when a session starts. This sample runs the MapKeyToMacro subroutine (shown in Change a Key Mapping) as the session connects.
You can use events to programmatically map a key for specific screens. This sample runs the MapKeyToMacro subroutine shown in Change a Key Mapping to assign an action to a key when a specific screen opens.
To map a keyboard combination, you'll need to:
To set up the action, we need to define the action and then add it to an action sequence so we can map it to a key.
First, you define the action you want to map by creating an InputMapAction object and defining its properties. The InputMapActionID enum defines a number of actions that you can assign to input maps (keyboard or mouse maps). Some of these actions, like the RunMacroAction, require specific parameters. You can get information about which parameters are required and how to set them in the Help description for each enum item. You'll need to set these parameters in the order in which they are listed in the Help.
In our example, the Help for the RunMacroAction enum indicates we need to add three parameters to the InputMap Action object:
In our example, we set these parameters in the order in which they are listed in the enum description, using the three methods for adding parameters on the InputMapAction object: AddParameter, AddParameterAsInteger, and AddParameterAsBoolean.
After we have defined the action, we need to add it to an action sequence so we can map it to the key we want to use. We do this by creating an InputMapActionSequence object and adding the InputMapAction that we created to this object.
To set up a keyboard shortcut, we need to create a new KeyBoardMapping object and add it to the session keyboard map.
The KeyBoardMapping object has properties that we can use to set the key associated with the keyboard mapping and set the action sequence associated with the keyboard mapping.
First, we set the Key property to items defined in the Reflection Keys enumeration. Then we set the Mapping property to the sequence we created for the action. Finally, we add the new mapping to the session keyboard map.
You can use this same approach to map any action to your keyboard.