Keys

The function of the keyboard can be extensively customized in Eclipse. Within Eclipse, key strokes and key sequences are assigned to invoke particular commands.

 

Key strokes, Key sequences, and Key bindings

A 'key stroke' is the pressing of a key on the keyboard, while optionally holding down one or more of these modifier keys: Ctrl, Alt (Option on the Macintosh), Shift, or Command (only on the Macintosh.) For example, holding down Ctrl then pressing A produces the key stroke Ctrl+A. The pressing of the modifier keys themselves do not constitute key strokes.

A 'key sequence' is one or more key strokes. Traditionally, emacs assigned two or three key stroke key sequences to particular commands. For example, the normal key sequence assigned to Close All in emacs is Ctrl+X Ctrl+C. To enter this key sequence, one presses the key stroke Ctrl+X followed by the key stroke Ctrl+C. While Eclipse supports key sequences of arbitrary lengths, it is recommended that keyboard shortcuts be four key strokes in length (or less).

A 'key binding' is the assignment of a key sequence to a command.

 

Configurations

A 'configuration' is a set of key bindings. Eclipse includes two configurations:

The Default configuration contains a general set of key bindings, in many cases recognizable to users as traditional key sequences. For instance, Ctrl+A is assigned to Select All, Ctrl+S is assigned to Save, etc.

The Emacs configuration contains a set of key bindings familiar to users of Emacs. For instance, Ctrl+X H is assigned to Select All, Ctrl+X S is assigned to Save, etc.

It is important to understand why the Emacs configuration says that it 'extends Default'. The Emacs configuration is not a complete set of key bindings like the Default configuration. Rather, it borrows from the Default configuration where possible, only defining explicit Emacs style key bindings where they vary from the Default configuration. Generally, only well known commands like Select All, Save, etc. have specific Emacs key sequences associated with them.

The user decides which configuration they are most comfortable using by changing the 'Active Configuration' setting on the keys preference page. If the user chooses the Default configuration, all Emacs key bindings are ignored. If the user chooses the Emacs configuration, explicit Emacs style key sequence assignments take precedence over any conflicting assignments in the Default configuration.

 

Contexts

Key bindings can vary based on the current context of Eclipse.

Sometimes the active part might be a Java file editor, for instance, where a different set of key sequence assignments may be more appropriate than if the active part was an html file editor. As a specific example, typically Ctrl+B is assigned to Build in a context such as Java file editing, while Ctrl+B is assigned to Make Text Bold in a context such as HTML file editing. This context is usually determined by the active part, but it can be influenced by the active window or dialog as well. If the active part does not choose a particular context, the workbench will set the active context to In Windows.

Eclipse includes seven different contexts. They are:

Much like configurations, contexts can extend other contexts. For example, the Editing Java Source context borrows key bindings from the Editing Text context, which in turn borrows key bindings from the In Windows context.

Note: It is not recommended to promote a key binding to a context which it extends. For example, it is not recommended to move an Editing Text key binding to the In Dialogs and Windows context. This may have unexpected results.

It is possible for some key bindings to work in dialogs. Those key bindings are assigned to the In Dialogs and Windows context. One example of such a key binding is the key binding for "cut". It is possible to change these key bindings. For example, it is possible to have Ctrl+X as cut in dialogs, but Ctrl+W as cut in windows.

 

Platform and Locale

Key bindings also vary by platform and locale. On the Macintosh platform, Command+S is assigned to Save, instead of the usual Ctrl+S. On Chinese locales (zh), Alt+/ is assigned to Content Assist, instead of the usual Ctrl+Space.

The current platform and locale is determined when Eclipse starts, and does not vary over the course of an Eclipse instance.

 

Customizing Key bindings

With multi-stroke key sequences, configurations, and contexts, there are a lot of things to keep in mind when customizing key bindings. To make things easier, all key customization is done on the Keys preference page.

Select Window > Preferences > Workbench > Keys for the Keys preference page.

Keys preference page showing Emacs as the active configuration and the Close command selected

In this example, we have chosen to select Emacs as the active configuration, and have selected the command Close from the list of commands. Information on this command, along with its current key bindings, is shown.

Notice that Close has three key sequences assigned to it: Ctrl+F4 and Ctrl+W in the Default configuration and Ctrl+X K in the Emacs configuration. Both are assigned in the In Windows context. Thus, if the user sets the active configuration to Default, Ctrl+F4 and Ctrl+W will be assigned to Close while Ctrl+X K will not. If the user sets the active configuration to Emacs, however, Ctrl+X K will be assigned to Close. As well, because the Emacs configuration also borrows key bindings from the Default configuration, Ctrl+F4 and Ctrl+W will also be assigned to Close provided that these key binding have not been assigned to another command in the Emacs configuration. In this example, "Ctrl+W" is bound to Cut in the Emacs key configuration.

Below the list of key sequences assigned to Close, there is a place to add or remove key bindings. By default, it selects the context as In Windows.

We type in the key sequence Ctrl+W, and the 'Add' button becomes enabled. Also, a list of all the commands to which the key sequence Ctrl+W is already assigned is show below the Add button. We can see that Ctrl+W is currently assigned to the command Cut in the In Windows and Dialogs context. We click the 'Add' button to assign Ctrl+W to Close.

Now we see Ctrl+W added to the list of key sequences assigned to Close. Notice the small 'change' graphic

Small graphic representing 'change' indicating that this key binding changes an existing key binding. Our new key binding, makes sure that the Ctrl+W key binding for Close works in the Emacs key configuration. The binding to Cut still exists, but will only work in dialogs (i.e., when we are "in dialogs and windows", but not "in windows"). We can remove this change at any time by selecting our new key binding and clicking the 'Remove' button. The previous assignment of Ctrl+W to Cut will automatically be restored.

Keys preference page showing the new assignment, including 'change' graphic

By selecting the command Cut, we can see the results of our change. Notice the graphic

Small graphic representing 'minus' indicating that the key binding was removed. We can restore this key binding at any time by selecting it here and clicking the 'Restore' button, effectively removing the new key binding we added in the previous step.

Keys preference page showing the Cut command

Perhaps we choose to assign another key to Cut in the Emacs configuration, say Ctrl+Alt+W. Adding this in the previous manner above produces the following. Notice the small 'plus' graphic

Small graphic representing 'plus' indicating that the key binding was added by the user, and was previously unassigned:

Keys preference page adding the key sequence Ctrl+Alt+W to Close

 

The Dynamic Nature of Key bindings

Key bindings are provided by plug-ins, and in Eclipse, plug-ins can be added or removed. This can cause key bindings declared by these plug-ins to be added or removed. Eclipse stores custom key bindings in a way to automatically compensate for this. Consider the example above where Ctrl+Alt+W was assigned to Cut in the Emacs configuration. Consider the user installs a new plug-in which assigns Ctrl+Alt+W to a particular command. Eclipse will preserve the user's assignment to Cut, but show the key binding with the small 'change' graphic instead of the 'plus' graphic.

 

Conflict Resolution

There are only a finite number of simple, common key strokes available to assign to a multitude of commands. We have seen that configuration, context, platform, and locale all partition key sequence assignments into domains where they don't conflict with one another. Consider the case for Ctrl+B above if contexts did not exist. One plug-in would assign Ctrl+B to Build, the other plug-in would assign Ctrl+B to Make Bold Text. How would Eclipse properly resolve this conflict?

Though conflicts are drastically reduced by employing the above mechanisms, they can still occur. Two plug-ins, independent of one another, could assign the same key sequence to different commands with the same context, configuration, platform, and locale. Consider if a plug-in assigned Ctrl+F4 in the In Windows context and Default configuration to one of its commands. This directly conflicts with Eclipse assigning Ctrl+F4 to the close command in the same context and configuration.

This is a conflict. It wouldn't be proper to invoke both commands, nor would it be proper to simply choose one of the two commands to receive the key stroke. The only proper thing to do is to ignore both key bindings, making Ctrl+F4 effectively useless in this context and configuration.

Conflicts of this nature are shown in the Keys preference page. Notice the red text and the word "[conflict]":

These types of conflicts can be resolved by the user explicitly assigning the key sequence to one of the commands.

Another type of conflict can be caused by multiple-key stroke key sequences. For example, in the Emacs configuration, there are many multiple-key stroke key sequences beginning with the key stroke Ctrl+X. Ctrl+H K is assigned to Close. Ctrl+X H is assigned to Select All.

As previously mentioned, the Emacs configuration borrows key bindings from the Standard configuration. In the standard configuration, Ctrl+X is assigned to Cut. Though the Emacs configuration doesn't explicitly redefine Ctrl+X, pressing Ctrl+X is required as part of many of its key bindings. In the Emacs configuration, when one presses Ctrl+X, one is half way to entering one of many possible assigned key sequences. One would not expect the Cut action to be invoked at this time.

For this type of conflict, the rule is that the Ctrl+X key sequence assigned to Cut would be ignored. Otherwise, it would not be possible to complete many of the key bindings in the Emacs configuration.

Related concepts
Accessibility Features in Eclipse
Changing the key bindings
Online help system

Related references
Font and color settings in Eclipse