Vainolo's Blog

Creating an OPM GEF Editor – Part 21: Adding Keyboard Shortcuts


Previous Tutorial: Creating an OPM GEF Editor – Part  20: Creating a Context Menu and Adding Custom Actions

Keyboard shortcuts are very useful for activating actions. There are many shortcuts that are common in all environments – and for better usability, enabling this shortcuts give the user a better user experience. In my case, I wanted to let the user edit the name of a thing (or state) using the F2 key, which is commonly used for this purpose.

Adding keyboard shortcuts to GEF is fairly easy. You only need to define a <code>KeyHandler</code> and attach it to the graphical viewer. I defined two key shortcuts: F2 for direct editing, and F3 for my own ResizeToContentsAction action.

	private void configureKeyboardShortcuts() {
		getGraphicalViewer().getKeyHandler();<br> GraphicalViewerKeyHandler keyHandler = new GraphicalViewerKeyHandler(getGraphicalViewer())
		keyHandler.put(KeyStroke.getPressed(SWT.F2, 0), getActionRegistry().getAction(GEFActionConstants.DIRECT_EDIT));
		keyHandler.put(KeyStroke.getPressed(SWT.F3, 0),

This function is called from the configureGraphicalViewer. And that is all! You can find the source for this change in my github repository:

Next tutorial: Creating an OPM GEF Editor – Part 22: Enabling Select-All Action in a GEF Editor

Enhanced by Zemanta

Written by vainolo

April 30th, 2012 at 1:37 pm

Posted in Programming

Tagged with , ,

4 Responses to 'Creating an OPM GEF Editor – Part 21: Adding Keyboard Shortcuts'

Subscribe to comments with RSS or TrackBack to 'Creating an OPM GEF Editor – Part 21: Adding Keyboard Shortcuts'.

  1. Keyboard Shortcuts doesnot seems to work. When I pressed F2, basically there is no effect. Any clues??


    28 Jun 12 at 15:17

  2. Without more information I cannot help you.


    29 Jun 12 at 00:56

  3. i’ve the same problem.
    Selected a node – F2 – no reaction.


    4 Jun 13 at 16:27

  4. See my answer to the comment above. Is your core exactly like mine?


    4 Jun 13 at 16:39

Leave a Reply

%d bloggers like this: