on Jul 18th, 2008Tip: Suppressing Keybindings

I was helping a colleague today with a key binding issue that I think is fairly common. To illustrate the problem, let’s look at what happens when you press CMD+N in the RCP mail sample:

Oh no, why did I get the new project wizard!?

What!? New Project Wizard? That’s not what I wanted! In this case, I actually wanted a ‘New Message’ to pop up instead of that new wizard. I don’t want any of my end-users ever seeing this dialog! What’s the problem here? Well, in simple terms, Eclipse is using its own default key configuration scheme if you don’t set one. So by default, you may get strange results like above or even conflicts with existing key bindings.

What’s the solution?

Well, there are a few ways to go about this but I think the simplest one for most people is to simply create your own key configuration scheme and instruct Eclipse to use it. To do this, you first need to create your own scheme:

<extension point="org.eclipse.ui.bindings">
<scheme
id="mail.scheme"
name="My Mail Scheme">
</scheme>
</extension>

After that, make sure your bindings are instructed to use this scheme:

<extension
point="org.eclipse.ui.bindings">
<key
commandId="mail.open"
schemeId="mail.scheme"
sequence="M1+N">
</key>
<key
commandId="mail.openMessage"
schemeId="mail.scheme"
sequence="M1+3">
</key>
<key
commandId="org.eclipse.ui.file.exit"
schemeId="mail.scheme"
sequence="M1+X">
</key>
</extension>

Finally, you need to create a plugin_customization.ini if you don’t have one already for your application and put this in there:

org.eclipse.ui/KEY_CONFIGURATION_ID=mail.scheme

That’s it. Once you do that, you should have key binding bliss:

Alright, that\'s what I want!

Thanks for listening and let me know if this helps. Here is the sample project for reference.

13 Responses to “Tip: Suppressing Keybindings”

  1. Nicolas Bihanon 18 Jul 2008 at 1:44 pm

    Hey, thanks for the tip :)

    I was struggling from 2 p.m. because I forgot the plugin_customization.ini thing ! You’re tip is just right in time ! :)

    By the way, I still don’t understand why a new RCP application get all theses Keybindings by default.

    Nicolas

  2. Pascalon 18 Jul 2008 at 5:13 pm

    In addition to this, crafting the target or the launch configuration such that the right plug-ins are included would also help. After all if the wizard was not included pressing the key would do nothing.

  3. jmanon 18 Jul 2008 at 11:55 pm

    You’re the man! You’ve saved my life. ;-)

  4. Matthiason 19 Jul 2008 at 3:13 am

    Thanks Chris for this wonderfult tip!

  5. Tom Seidelon 19 Jul 2008 at 4:53 am

    For such issues, I prefer Equinox Transformer Hooks with a XSLT Transformation of the key-binding contributing plugin.xml.
    I really look forward for the related SoC project (http://code.google.com/soc/2008/eclipse/appinfo.html?csaid=93F987E6FAAE926F)

    Cheerz

  6. Lars Vogelon 19 Jul 2008 at 6:59 am

    Very nice and helpful tip. Thank you.

  7. [...] http://code9.com/2008/07/18/tip-suppressing-keybindings/ [...]

  8. zxon 19 Jul 2008 at 7:42 am

    So Pascal, your case would be ideal but when you have things that are in a plug-in that you need, you’re kind of stuck. You can manually choose to edit the plugin.xml or like Tom said, you can use Equinox Transforms. The reason I didn’t point out Equinox Transforms is that it’s a more advanced topic and for the majority of people that hit this specific problem, the approach I outlined is good enough.

    Tom, Bartosz is making some good progress on the project so hopefully we should see something in PDE around the 3.5M2 time frame.

  9. Randy Hudsonon 01 Aug 2008 at 1:29 pm

    This solution doesn’t really *remove* the “New…” action from the RCP app. CTRL+3 would still find it. But wait, since you’ve defined an empty keybinding configuration, many otherwise useful keybindings like CTRL+3 may be missing now.

    If you can’t get rid of the IDE plug-in, another way to take control of CTRL+N is to declare a more specific context which extends the context associated with the existing CTRL+N binding. Maybe “my.mail.application” which extends “windowsAndDialogs”. Your app window then enables that context, and your keybinding for “New Message” should take precedence over the one for “File->New…”, since it is more specific.

  10. zxon 04 Aug 2008 at 9:15 am

    @Randy, correct, that is a downfall with this approach. Setting a parent maybe a nicer way of doing this. However, most people wouldn’t care if it appeared in Ctrl+3 as Ctrl+3 shouldn’t be active with a blank key configuration.

  11. [...] Key Bindings [...]

  12. Corey Straubon 07 Oct 2008 at 1:07 pm

    I am trying to do what you described above and having some trouble. I have created my own scheme and key bindings using that scheme but cannot switch over to the scheme. I have created the plugin_customization.ini file as well but not sure if i am adding it to my project properly. All i am doing is creating it and drag and dropping it on the on the project in eclipse to add it. Is there anything else that needs to be done to get the .ini file to used? Otherwise i am not sure what my error is.

  13. zxon 09 Oct 2008 at 7:31 am

    @Corey, did you figure it out yet ;)?

    Make sure when you export that you remember to include the plugin_customization.ini file at the root of your application.

    Another route is to set this preference while your application is starting up. For example in your WorkbenchWindowAdvisor class.

Trackback URI | Comments RSS

Leave a Reply