Using Keyboard Shortcuts with Enterprise Architect Extension Menus
by Phil Chudley, Principal Consultant at Dunstan Thomas Consulting
Extensions written for Enterprise Architect often define menus to invoke functionality, and although a short cut of the form Alt+character can be defined by prefixing the character in the menu option with &, this does not work as a keyboard shortcut.
After digging around a bit, I have discovered a work-around so that defined keyboard shortcuts can invoke menu options.
In this article I will explain through a simple example how to use this work-around in your EA Extension. This example has been tested on EA version 13.5 and EA version 14.
Plan of Attack
The logic behind this workaround is summarised as follows:
- Encapsulate all functionality that is invoked from the menu options in a Manager class, with public methods. This allows the functionality to be invoked from the menu and from the keyboard shortcut.
- Define your keyboard shortcut, ideally selecting combinations that are not already in use by EA. These are of the form of:
- Modifier(s) + key – for example Alt+1 or Ctrl+Alt+D
- All these keyboard shortcuts are registered with a keyboard Hook class associated with a Windows Form class.
- A key pressed event handler is added to the keyboard Hook class.
- The Windows Form class is never displayed, but catches the key pressed event. The event can be tested against the keyboard shortcut keys that have been registered and the corresponding menu function invoked using the Menu Manager class.
Overview of the Classes Defined in the Example
The workaround uses several classes to provide the keyboard shortcut functionality:
- Main – the central class that is always required for all EA Extensions
- MenuManager – a C# class that provides the functionality for all menu options
- AboutBox – a standard Windows AboutBox Form class
- HotKeyForm – a Windows Form class defining an empty window, which is never displayed, but whose sole purpose is to catch the keyboard event raised when pressing the keyboard shortkey combination, and to invoke the Extension’s menu functionality.
- KeyBoardHook – a C# class which registers the keyboard shortcuts, and raises the keyboard event when the keyboard shortcut keys are pressed.
- KeyPressedEventArgs – a C# class that encapsulates the event after the keyboard shortcut keys have been pressed. (Defined within KeyBoardHook)
The following class diagram illustrates the design of the example:
Detail & Code of the Classes
This class provides the entry point to all EA Extensions and is where the developer will define:
- Menu text for the extensions menus
- Global references to other classes, including EA.Repository
- Handle the appropriate EA broadcast events for;
- the initial connection to the Extension made by EA
- the menu contents
- menu enable / disable check / uncheck
- menu click
- EA disconnecting from the Extension
- Helper methods to provide functionality such as if a model repository is loaded.
- Code to handle any events broadcast by EA.
In this example, there are four menu options which provide very basic functionality:
- Option 1 – displays a Message Box
- Option 2 – displays a Message Box
- Option 3 – displays a Message Box
- About – displays the About Box
This functionality is available whether or not a model repository has been loaded and can be invoked via the menu, or by pressing the defined keyboard shortcut key combination.
In the method EA_Connect, the instantiation of HotKeyForm, registers the keyboard shortcuts and creates the form, its event and listener. An instantiation of the MenuManager class is also created.
This class defines a public method for each menu option so that these can be invoked from the class Main (as shown above), and also from with the HotKeyForm (shown later).
For the purposes of this example, the class is the standard class that is provided by the C# .NET framework.
This class is a Windows Form class and instantiates an instance of the KeyBoardHook class, adds an EventHandler for the KeyPressedEvent and registers each keyboard shortcut key combination with the KeyBoardHook instance.
This class provides all the “wiring” between the registered keyboard shortcuts, and the keypressed event.
Does it Work?
Absolutely! The only way to see that it works is to create the Extension and try it out. This could be achieved by adding the code above to you own extension, or using the attached zip file which contains:
- Visual Studio solution for classes used in the example
- MSI installer for the example
Please note that this example was built using Visual Studio 2017 and the .NET framework version 4.6.1
This simple example assumes all Extension menu options are enabled all of the time, I am working on a variation of this example to provide keyboard shortcuts which are active only if a menu option is enabled or checked, and not active when a menu option is disabled or unchecked.
I hope this example is useful to all those of you who develop extensions for EA and I welcome your feedback.
Follow me on Twitter @SparxEAGuru