Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » How can I add keybindings programmatically from within eclipse?
How can I add keybindings programmatically from within eclipse? [message #290410] Thu, 25 August 2005 08:48 Go to next message
Tom Hofmann is currently offline Tom HofmannFriend
Messages: 770
Registered: July 2009
Senior Member
I want to programmatically add keybindings to eclipse. I have no problem
to define commands (using ICommandService) and to create new
KeyBindings. However, IBindingService does not allow to add keybindings,
except for the following way, which seems like a hack:

IBindingService bindingSvc...
Binding binding= my new binding;

Binding[] oldBindings= bindingSvc.getBindings();
Binding[] newBindings= new Binding[oldBindings.length + 1];
System.arraycopy(oldBindings, 0, newBindings, 0, oldBindings.length);
newBindings[oldBindings.length]= binding;

bindingSvc.savePreferences(bindingSvc.getActiveScheme(), newBindings);

Is there a way to add key bindings at all to the eclipse workbench?

Thanks, Tom
Re: How can I add keybindings programmatically from within eclipse? [message #290471 is a reply to message #290410] Thu, 25 August 2005 16:37 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: douglas.pollock.magma.ca

Tom Eicher wrote:
> I want to programmatically add keybindings to eclipse. I have no problem
> to define commands (using ICommandService) and to create new
> KeyBindings. However, IBindingService does not allow to add keybindings,
> except for the following way, which seems like a hack:
>
> IBindingService bindingSvc...
> Binding binding= my new binding;
>
> Binding[] oldBindings= bindingSvc.getBindings();
> Binding[] newBindings= new Binding[oldBindings.length + 1];
> System.arraycopy(oldBindings, 0, newBindings, 0, oldBindings.length);
> newBindings[oldBindings.length]= binding;
>
> bindingSvc.savePreferences(bindingSvc.getActiveScheme(), newBindings);
>
> Is there a way to add key bindings at all to the eclipse workbench?

To be honest, programmatically adding commands is possible but not really
encouraged. Key bindings almost certainly never be dynamically added. For
reasons why, I'd recommend taking a look at the contributions proposal
( http://dev.eclipse.org/viewcvs/index.cgi/~checkout~/platform -ui-home/R3_1/contributions-proposal/requestForComments.html).

I've left the window open for non-Eclipse developers to manipulate the
system. If you are developing code for the Eclipse SDK, I would ask that
you not programmatically add commands, contexts or bindings.

The contract for savePreferences does not specify that it will do anything
for bindings that are not of type USER. Bindings of type USER will be
written to the preference store. The fact that it does more at the moment
is not part of the contract, and could be changed in the future.



d.
Re: How can I add keybindings programmatically from within eclipse? [message #290546 is a reply to message #290471] Fri, 26 August 2005 10:49 Go to previous messageGo to next message
Tom Hofmann is currently offline Tom HofmannFriend
Messages: 770
Registered: July 2009
Senior Member
Douglas Pollock wrote:
> To be honest, programmatically adding commands is possible but not really
> encouraged. Key bindings almost certainly never be dynamically added.

I figured I was asking for trouble...

> I've left the window open for non-Eclipse developers to manipulate the
> system. If you are developing code for the Eclipse SDK, I would ask that
> you not programmatically add commands, contexts or bindings.

Hm, I would like to offer commands (and bindings) that trigger an action
(content assist in my case) that is parameterized by a certain
contribution to my extension point.

I can contribute a parameterizable command instead of creating commands
dynamically.

I don't see how I could automatically assign default key bindings
though. The solution will be that I contribute key bindings (for the
above parameterized command) for the extensions I control, and leave it
to other extenders whether they want to add a key binding or not.

-tom
Re: How can I add keybindings programmatically from within eclipse? [message #290662 is a reply to message #290546] Mon, 29 August 2005 14:23 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: douglas.pollock.magma.ca

Tom Eicher wrote:
> I can contribute a parameterizable command instead of creating commands
> dynamically.

Yes, this is the preferred way.


> I don't see how I could automatically assign default key bindings
> though. The solution will be that I contribute key bindings (for the
> above parameterized command) for the extensions I control, and leave it
> to other extenders whether they want to add a key binding or not.

This is also the preferred way of doing things. For example, this is how
the new parameterized show view command is being handled.

Oh, and ... We already have too many key bindings! Do we really need
more? :)



d.
Re: How can I add keybindings programmatically from within eclipse? [message #290685 is a reply to message #290662] Tue, 30 August 2005 08:10 Go to previous messageGo to next message
Tom Hofmann is currently offline Tom HofmannFriend
Messages: 770
Registered: July 2009
Senior Member
Douglas Pollock wrote:
> Tom Eicher wrote:
>>I can contribute a parameterizable command instead of creating commands
>>dynamically.
>
> Yes, this is the preferred way.
>
>>I don't see how I could automatically assign default key bindings
>>though. The solution will be that I contribute key bindings (for the
>>above parameterized command) for the extensions I control, and leave it
>>to other extenders whether they want to add a key binding or not.
>
> This is also the preferred way of doing things. For example, this is how
> the new parameterized show view command is being handled.
>
> Oh, and ... We already have too many key bindings! Do we really need
> more? :)

Yeees. Me likes naice baindings!

(actually no - there aren't any keys left...)

The problem now is that the action contribution story is still
IAction-based:

1) There is no way I can contribute menu entries for parameterized
commands to, say, org.eclipse.ui.editorActions.

Solution:
- To handle the keyboard shortcut, add a global handler
(org.eclipse.ui.handlers) which finds out the target editor on its own.
- To get the menu entries, programmatically add custom actions for
each parameterization.

2) Actions in menus do not show the keyboard shortcut, since the actions
only know their command, but not the parameterization (this is
https://bugs.eclipse.org/bugs/show_bug.cgi?id=90959)

-tom
Re: How can I add keybindings programmatically from within eclipse? [message #290686 is a reply to message #290471] Tue, 30 August 2005 08:37 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: james.willans.xactium.com

> To be honest, programmatically adding commands is possible but not really
> encouraged. Key bindings almost certainly never be dynamically added. For
> reasons why, I'd recommend taking a look at the contributions proposal
>
( http://dev.eclipse.org/viewcvs/index.cgi/~checkout~/platform -ui-home/R3_1/contributions-proposal/requestForComments.html).

> I've left the window open for non-Eclipse developers to manipulate the
> system. If you are developing code for the Eclipse SDK, I would ask that
> you not programmatically add commands, contexts or bindings.

> The contract for savePreferences does not specify that it will do anything
> for bindings that are not of type USER. Bindings of type USER will be
> written to the preference store. The fact that it does more at the moment
> is not part of the contract, and could be changed in the future.

Douglas,

The proposal looks good in terms of simplifying the story, however it
seems to me that we will still only be able to define key bindings
programmatically in the context of the workbench? In our RCP technology
we have a requirement to dynamically be able to assign key bindings (see
bug 84623), presumably we are not going to be able to do this under the
new proposal. I would request that the proposal is extended to allow the
explicit enablement of programmatic commands and contexts either:

a) On the understanding that this impacts the dynamic unloading of
plug-ins and the user must accept the consequence.

b) They are programmatically defined in such a way that the plug-in
unloader can calculate which plug-in they came from (is this really so
difficult?).

James
Re: How can I add keybindings programmatically from within eclipse? [message #290707 is a reply to message #290685] Tue, 30 August 2005 13:40 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: douglas.pollock.magma.ca

Tom Eicher wrote:
> The problem now is that the action contribution story is still
> IAction-based:

This should hopefully be resolved for 3.2.



d.
Re: How can I add keybindings programmatically from within eclipse? [message #290708 is a reply to message #290686] Tue, 30 August 2005 13:49 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: douglas.pollock.magma.ca

James Willans wrote:
> The proposal looks good in terms of simplifying the story, however it
> seems to me that we will still only be able to define key bindings
> programmatically in the context of the workbench? In our RCP technology
> we have a requirement to dynamically be able to assign key bindings (see
> bug 84623), presumably we are not going to be able to do this under the
> new proposal. I would request that the proposal is extended to allow the
> explicit enablement of programmatic commands and contexts either:

The proposal specifically forbids adding key bindings programmatically, and
gives reasons for doing this. Providing a facility for doing this using
the workbench's binding service is out of that proposal's scope.


> a) On the understanding that this impacts the dynamic unloading of
> plug-ins and the user must accept the consequence.

^ plug-in developer


> b) They are programmatically defined in such a way that the plug-in
> unloader can calculate which plug-in they came from (is this really so
> difficult?).

(c) The user is capable of interacting with this dynamic bindings in a
predictable way. That is, a user changes a binding that is normally added
dynamically after the registry and preferences are parsed.


"https://bugs.eclipse.org/bugs/show_bug.cgi?id=84623#c6". This is the way
in which I would suggest approaching this problem. However, there are no
concrete plans to address this for 3.2, as there are higher priority items
that must be completed first (namely, the contributions proposal and the
component framework). It is also tricky, as the component framework
proposal should preferrably be complete before addressing this. The
component framework will likely provide a generic mechanism for replacing
services on the workbench. So, rather than design a custom way of doing
this, it would be better for the generic mechanism to be in place.



d.
Re: How can I add keybindings programmatically from within eclipse? [message #294708 is a reply to message #290707] Wed, 16 November 2005 17:17 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mike.intelliware.ca

Douglas Pollock wrote:
> Tom Eicher wrote:
>
>>The problem now is that the action contribution story is still
>>IAction-based:
>
>
> This should hopefully be resolved for 3.2.
>
>
>
> d.
>
Could you my confirm something?

We are trying to apply multi-stroke key bindings to an editor action
(i.e. editorActions/action). Actions only support accelerators (i.e.
single stroke) so we need to use the new keyBinding extension. So we:

- create a command
- create a binding on the command
- link our existing action to the command
- go

This works for actionsSets, but not for editorActions. The keys display
in the menu entry, but the don't fire the action when typed. When we
inspect the core binding registry, we see that the command doesn't have
a handler.

In short, can we declaratively link editorActions to commands? If not,
can we easily do so in code?

Thanks,

-mike
Re: How can I add keybindings programmatically from within eclipse? [message #294720 is a reply to message #294708] Wed, 16 November 2005 20:34 Go to previous message
Eclipse UserFriend
Originally posted by: douglas.pollock.magma.ca

Michael Melvin wrote:
> This works for actionsSets, but not for editorActions. The keys display
> in the menu entry, but the don't fire the action when typed. When we
> inspect the core binding registry, we see that the command doesn't have
> a handler.

http://www.magma.ca/~pollockd/despumate/bindingsHowTo.html

On a command (3.1 and later), you can specify

defaultHandler="com.yourcompany.yourplugin.YourHandler"

In 3.0.x, you can use the "handlerSubmission" element. This element is
experimental, and does not exist in any other version of Eclipse.

In 3.2 and later, you can use the "org.eclipse.ui.handlers" extension point
if you need more fine-grained control over when your handler will be
active. Otherwise, "defaultHandler" is still good.

Through code, there is IWorkbench.getAdapter(IHandlerService.class), as well
as site.getKeyBindingService().registerAction(IAction).



cheers,
d.
Previous Topic:Adding actions to the context menu on a tab of an editor
Next Topic:Export Plugin wizard does not export all class files
Goto Forum:
  


Current Time: Fri Apr 26 07:15:53 GMT 2024

Powered by FUDForum. Page generated in 0.03788 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top