Back to top

vogella training Training Books

Define key bindings for Eclipse Commands - Tutorial

Lars Vogel

Version 2.7

17.01.2012

Revision History
Revision 0.1 30.03.2009 Lars
Vogel
Created
Revision 0.2 - 2.7 11.04.2009 - 17.01.2012 Lars
Vogel
bugfixes and improvements

Define keybindings for Eclipse Commands

This article describes how to define shortcuts for Eclipse commands.

This article is based on Eclipse Indigo (3.7).


Table of Contents

1. Eclipse key bindings
2. Prerequisites for this tutorial
3. Eclipse Commands
4. Tutorial: Defining key bindings for Commands
4.1. Keybinding using the default scheme
4.2. Defining your own scheme
5. Thank you
6. Questions and Discussion
7. Links and Literature
7.1. Source Code
7.2. Eclipse Commands Resources
7.3. vogella Resources

1. Eclipse key bindings

Via key bindings you can define shortcuts for your Eclipse Commands.

Eclipse uses a default key configuration scheme. If your application uses existing plug-ins where keybindings are already defined these keybindings will also be active in your Eclipse RCP application. If that is not desired then you can configure your product to use another scheme.

The following describes first how to create keybindings using this default configuration scheme. Afterwards I how to overwrite the default key binding scheme of Eclipse.

To define a shortcut for a command you use org.eclipse.ui.bindings extension point together with the org.eclipse.ui.defaultAcceleratorConfiguration schemeId and the org.eclipse.ui.contexts.window contextId.

To define and use your own scheme you need:

  • Define a new scheme via extension point "org.eclipse.ui.bindings"

  • Assign this scheme to the keybindings you defined

  • Have a product created

  • Define the file "plugin_customization.ini" and set the scheme for the product via a property.

2. Prerequisites for this tutorial

This tutorial assumes what you have basic understanding of development for the Eclipse platform. Please see Eclipse 4 RCP Tutorial or Eclipse Plug-in Tutorial if you need any basic information.

3. Eclipse Commands

The following also assumes that you are familiar with Eclipse Commands. To learn about them you can use the For an introduction please see the Eclipse Commands Tutorial and the Eclipse Advanced Commands Tutorial which contain a introduction into the material.

4. Tutorial: Defining key bindings for Commands

4.1. Keybinding using the default scheme

Create a new project "de.vogella.rcp.intro.commands.keybinding" using the "Hello RCP" template, declare command "de.vogella.rcp.intro.commands.keybinding.hello" with a default handler "de.vogella.rcp.intro.commands.keybinding.HelloHandler" which prints out "Hello" to the console

Add the extension point "org.eclipse.ui.bindings" to your project. Right-click in this extension point, select New-> Key.

Select as schemeID "org.eclipse.ui.defaultAcceleratorConfiguration" this is the workbench default and will make sure you keybinding is valid in the whole application. The commandId is the ID of the command you just created. The sequence is the shortcut key for calling the command. M1 represents the Ctrl key.

If you now run it the keybinding should work and if you press Ctrll+1 the message on the console should be visible.

4.2. Defining your own scheme

Add now another key binding "Ctrl+N" to the command "de.vogella.rcp.intro.commands.keybinding.hello". Try it. This will not work as it is conflicting with the Eclipse default schema.

Right-click in this extension point "org.eclipse.ui.bindings", select New-> Scheme. Create a schema with the id "MyScheme".

Assign the scheme id to your commands.

Define a product. See Defining a product for Eclipse RCP for details. Create the following file "plugin_customization.ini" and put it in your main directory

org.eclipse.ui/KEY_CONFIGURATION_ID=myscheme 

If you now run your product the "Ctrl+N" shortcut should work.

5. Thank you

Please help me to support this article:

Flattr this

6. Questions and Discussion

Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.

7. Links and Literature

7.1. Source Code

Source Code of Examples

7.2. Eclipse Commands Resources

Introduction to Eclipse Commands

7.3. vogella Resources

vogella Training Android and Eclipse Training from the vogella team

Android Tutorial Introduction to Android Programming

GWT Tutorial Program in Java and compile to JavaScript and HTML

Eclipse RCP Tutorial Create native applications in Java

JUnit Tutorial Test your application

Git Tutorial Put everything you have under distributed version control system