Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » How to register a key binding to a IAction
How to register a key binding to a IAction [message #289405] Thu, 04 August 2005 12:37 Go to next message
Eclipse UserFriend
Originally posted by: kruttik.a.tcs.com

Hi,
I want to add a key binding to my customized IAction class.
for eg: Suppose i want to invoke my action on pressing "ALT+1"
heres the code i wrote but its not working.
This code has a class NewKey which extends action and another class which
instantiates this action and adds it to the text editor.

public class NewKey extends Action{
String code="";
public NewKey(String name,String c){
super(name);
code=c;
}
public void addConfig(int keycode){
super.setAccelerator(keycode);

}

public void run(){
try{
System.out.println("RUN Key activated");
PrintWriter pr= new PrintWriter(new
FileOutputStream("c:\\acbk.txt"));
pr.println("RUN Key activated");
}catch(Exception e){
e.printStackTrace();
}

}

and this code instantiates this class

IWorkbench wb = PlatformUI.getWorkbench();
ICommandManager icm= wb.getCommandSupport().getCommandManager();
KeySequence keyseq=KeySequence.getInstance("ALt+1");
String com=icm.getPerfectMatch(keyseq);
NewKey nk=new NewKey("TrialAction","TrialActionSuccessFul");
if(com==null){
System.out.println("Registering..");
nk.addConfig(SWT.ALT|'1');
nk.setActionDefinitionId("TrialAction");
nk.setDescription("TrialAction");
nk.setId("TrialAction");
nk.setEnabled(true);
System.out.println("Registered..");
}else
MessageDialog.openInformation(shell,"HOT KEY","Match is: "+com+"
Already Exist");
IEditorPart editor= window.getActivePage().getActiveEditor();
((ITextEditor)editor).setAction("TryAction",(IAction)nk);

This should ideally register the action to the keybinding but its not
happeneing. I tried checking if at all the action is registered and i
found it is.heres the code
if(((ITextEditor)editor).getAction("TryAction")!=null){
MessageDialog.openInformation(shell,"HOT KEY","IAction is not null");
MessageDialog.openInformation(shell,"HOT KEY","Accelerator : "+
((ITextEditor)editor).getAction("TryAction").getAccelerator());
}

The Message box shows Accelerator as 65585 which is correct (ALT+1 =
(1<<16)+49) but whne i press ALT+1 nothin happens.

HELP!
Re: How to register a key binding to a IAction [message #289412 is a reply to message #289405] Thu, 04 August 2005 14:03 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: sunil_kamath.nohotspammail.com

"Kruttik Aggarwal" <kruttik.a@tcs.com> wrote in message
news:24a6f32a4d4d382a33c14ab109aa7140$1@www.eclipse.org...
> Hi,
> I want to add a key binding to my customized IAction class.
> for eg: Suppose i want to invoke my action on pressing "ALT+1"
> heres the code i wrote but its not working.
> This code has a class NewKey which extends action and another class which
> instantiates this action and adds it to the text editor.
>
> [snip]
>
Did you read the javadoc for IAction.setAccelerator()?
" This method should no longer be used for actions in the Eclipse workbench.
IWorkbenchCommandSupport and IWorkbenchContextSupport provide all the
functionality required for key bindings. If you set an accelerator using
this method, then it will not work in the workbench if it conflicts any
existing key binding, or if there is a different key binding defined for
this action's definition id. The definition id should be used instead --
referring to the command in the workbench from which the key binding should
be retrieved. "

All your questions can be answered by rtfm.
---
Sunil
Re: How to register a key binding to a IAction [message #289418 is a reply to message #289412] Thu, 04 August 2005 14:41 Go to previous messageGo to next message
Vincent Etter is currently offline Vincent EtterFriend
Messages: 72
Registered: July 2009
Member
Sunil Kamath a écrit :
> Did you read the javadoc for IAction.setAccelerator()?
> " This method should no longer be used for actions in the Eclipse workbench.
> IWorkbenchCommandSupport and IWorkbenchContextSupport provide all the
> functionality required for key bindings. If you set an accelerator using
> this method, then it will not work in the workbench if it conflicts any
> existing key binding, or if there is a different key binding defined for
> this action's definition id. The definition id should be used instead --
> referring to the command in the workbench from which the key binding should
> be retrieved. "
>
> All your questions can be answered by rtfm.
> ---
> Sunil
>

I have the same problem. I read this in the javadoc and had a look at
IWorkbenchCommandSupport and IWorkbenchContextSupport but all thier
methods are deprecated...

So what's the solution ?
Re: How to register a key binding to a IAction [message #289419 is a reply to message #289418] Thu, 04 August 2005 14:55 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: douglas.pollock.magma.ca

Vincent Etter wrote:
> So what's the solution ?

* @deprecated Please use <code>ICommandService</code> and
* <code>IHandlerService</code> instead.
Re: How to register a key binding to a IAction [message #289422 is a reply to message #289419] Thu, 04 August 2005 15:17 Go to previous messageGo to next message
Vincent Etter is currently offline Vincent EtterFriend
Messages: 72
Registered: July 2009
Member
Douglas Pollock a écrit :
> * @deprecated Please use <code>ICommandService</code> and
> * <code>IHandlerService</code> instead.
>
>

Lol... I didn't see that ! Thanks a lot !!

Vincent
Re: How to register a key binding to a IAction [message #289424 is a reply to message #289419] Thu, 04 August 2005 15:21 Go to previous messageGo to next message
Vincent Etter is currently offline Vincent EtterFriend
Messages: 72
Registered: July 2009
Member
I look at these classes and didn't see anything to solve my problem ;) !
Could anyone explain me how they work ? Thanks !

Vincent
Re: How to register a key binding to a IAction [message #289427 is a reply to message #289424] Thu, 04 August 2005 15:57 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: sunil_kamath.nohotspammail.com

"Vincent Etter" <vincent.etter@gmail.com> wrote in message
news:dctbkf$kca$2@news.eclipse.org...
>I look at these classes and didn't see anything to solve my problem ;) !
>Could anyone explain me how they work ? Thanks !
>
> Vincent
Use the org.eclipse.ui.contexts to define a context for your commands.
Use the org.eclipse.ui.commands extension to define your command category
and commands.
Use the org.eclipse.ui.bindings extension to bind keys to the command- make
sure to set the commandId and the contextId.
Create your action and set its action definition id to the command id. If
you are defining it in the plugin.xml, make sure to set the definitionId.
In your editor, override initializeKeyBindingScopes() and call
setKeyBindingScopes with your contextId.
Then the action will fire when you press the key combination when your
editor is the active part.
---
Sunil
Re: How to register a key binding to a IAction [message #289451 is a reply to message #289427] Fri, 05 August 2005 07:55 Go to previous messageGo to next message
Vincent Etter is currently offline Vincent EtterFriend
Messages: 72
Registered: July 2009
Member
Sunil Kamath a écrit :
> Use the org.eclipse.ui.contexts to define a context for your commands.
> Use the org.eclipse.ui.commands extension to define your command category
> and commands.
> Use the org.eclipse.ui.bindings extension to bind keys to the command- make
> sure to set the commandId and the contextId.
> Create your action and set its action definition id to the command id. If
> you are defining it in the plugin.xml, make sure to set the definitionId.
> In your editor, override initializeKeyBindingScopes() and call
> setKeyBindingScopes with your contextId.
> Then the action will fire when you press the key combination when your
> editor is the active part.
> ---
> Sunil
>
>

Ok... It seems very complicated compared to the "@Ctrl+E" or
setAccelerator() methods... Is there a snippet that illustrates this
method ? Thanks !
Re: How to register a key binding to a IAction [message #289467 is a reply to message #289451] Fri, 05 August 2005 12:10 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: douglas.pollock.magma.ca

Vincent Etter wrote:
> Ok... It seems very complicated compared to the "@Ctrl+E" or
> setAccelerator() methods... Is there a snippet that illustrates this
> method ? Thanks !

Yes, it is more complicated. But, unfortunately, this is what happens when
you've got different accelerator schemes (and other fun stuff).


<org.eclipse.ui.commands>
<command
id="commandId"
name="Name"
description="Description"
categoryId="categoryId" />
<!-- Use an existing category, if possible. If not, then you have
to define the category as well.
-->
<category id="categoryId" name="Name" description="Description" />
</org.eclipse.ui.commands>

<org.eclipse.ui.bindings>
<key
sequence="CTRL+E"
commandId="commandId"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration" />
<!-- If you are developing for MacOS X as well, then M1+E may be
more appropriate. Please read the schema documentation for
the sequence attribute.
-->
</org.eclipse.ui.bindings>



d.
Re: How to register a key binding to a IAction [message #289469 is a reply to message #289467] Fri, 05 August 2005 12:19 Go to previous messageGo to next message
Vincent Etter is currently offline Vincent EtterFriend
Messages: 72
Registered: July 2009
Member
Douglas Pollock a écrit :
>
> Yes, it is more complicated. But, unfortunately, this is what happens when
> you've got different accelerator schemes (and other fun stuff).
>
>
> <org.eclipse.ui.commands>
> <command
> id="commandId"
> name="Name"
> description="Description"
> categoryId="categoryId" />
> <!-- Use an existing category, if possible. If not, then you have
> to define the category as well.
> -->
> <category id="categoryId" name="Name" description="Description" />
> </org.eclipse.ui.commands>
>
> <org.eclipse.ui.bindings>
> <key
> sequence="CTRL+E"
> commandId="commandId"
> schemeId="org.eclipse.ui.defaultAcceleratorConfiguration" />
> <!-- If you are developing for MacOS X as well, then M1+E may be
> more appropriate. Please read the schema documentation for
> the sequence attribute.
> -->
> </org.eclipse.ui.bindings>
>
>
>
> d.
>

Thank you for your answer and your example ;) !
Re: How to register a key binding to a IAction [message #289478 is a reply to message #289467] Fri, 05 August 2005 13:21 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: kruttik.a.tcs.com

Hi,

I already implemented this method. The problem with thgis method is that
all the entries need to be made to the plugin.xml.

I want to create key bindings programmatically i.e it will be decided at
runtime which keys associate with what action.

so basically i dont have any static information on commands and key
bindings, everything is dynamic, thats why i wanted to use some way to
attach keys programmatically.

Please tell me if u have a way
Re: How to register a key binding to a IAction [message #289505 is a reply to message #289478] Fri, 05 August 2005 21:46 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: james.willans.xactium.com

Hi Kruttik,

We too, please subscribe to and vote for bug 84623.

James

Kruttik Aggarwal wrote:

> Hi,

> I already implemented this method. The problem with thgis method is that
> all the entries need to be made to the plugin.xml.

> I want to create key bindings programmatically i.e it will be decided at
> runtime which keys associate with what action.

> so basically i dont have any static information on commands and key
> bindings, everything is dynamic, thats why i wanted to use some way to
> attach keys programmatically.

> Please tell me if u have a way
Re: How to register a key binding to a IAction [message #289506 is a reply to message #289505] Fri, 05 August 2005 22:10 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: sunil_kamath.nohotspammail.com

"James Willans" <james.willans@xactium.com> wrote in message
news:61003aa57b0adfdd8fcba3f6d25d6e49$1@www.eclipse.org...
> Hi Kruttik,
>
> We too, please subscribe to and vote for bug 84623.
>
> James
>
> Kruttik Aggarwal wrote:
>
>> Hi,
>
>> I already implemented this method. The problem with thgis method is that
>> all the entries need to be made to the plugin.xml.
>
>> I want to create key bindings programmatically i.e it will be decided at
>> runtime which keys associate with what action.
>
>> so basically i dont have any static information on commands and key
>> bindings, everything is dynamic, thats why i wanted to use some way to
>> attach keys programmatically.
>
>> Please tell me if u have a way
>

Have you investigated if it can be done programatically using
IBindingService.savePreferences(), KeyBinding and KeySequence?
You can create a KeySequence, assign it to a KeyBinding. KeyBinding can then
be assigned to a ParametrizedCommand which can be
created from a Command.
Then IBindingService can be used to save the preferences.
See how the KeysPreferencePage does it.
---
Sunil
Re: How to register a key binding to a IAction [message #289530 is a reply to message #289506] Sun, 07 August 2005 22:13 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: james.willans.xactium.com

Hi Sunil,

Thanks for your suggestion. I can't figure out how the last part of your
proposal will work. I can define a KeyBinding and I can define a
ParameterizedCommand with a Command, I can use the IBindingService to get
bindings for my ParameterizedCommand. But as far as I can tell there is
no programmatic way of introducing my new key binding into the
IBindingService. Am I missing something?

James

> Have you investigated if it can be done programatically using
> IBindingService.savePreferences(), KeyBinding and KeySequence?
> You can create a KeySequence, assign it to a KeyBinding. KeyBinding can then
> be assigned to a ParametrizedCommand which can be
> created from a Command.
> Then IBindingService can be used to save the preferences.
> See how the KeysPreferencePage does it.
> ---
> Sunil
Re: How to register a key binding to a IAction [message #289531 is a reply to message #289530] Sun, 07 August 2005 22:18 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: sunil_kamath.nohotspammail.com

"James Willans" <james.willans@xactium.com> wrote in message
news:00cca468c3b27dbfbe5fedabdbf65166$1@www.eclipse.org...
> Hi Sunil,
>
> Thanks for your suggestion. I can't figure out how the last part of your
> proposal will work. I can define a KeyBinding and I can define a
> ParameterizedCommand with a Command, I can use the IBindingService to get
> bindings for my ParameterizedCommand. But as far as I can tell there is
> no programmatic way of introducing my new key binding into the
> IBindingService. Am I missing something?
>
> James
>
>> Have you investigated if it can be done programatically using
>> IBindingService.savePreferences(), KeyBinding and KeySequence?
>> You can create a KeySequence, assign it to a KeyBinding. KeyBinding can
>> then be assigned to a ParametrizedCommand which can be
>> created from a Command.
>> Then IBindingService can be used to save the preferences.
>> See how the KeysPreferencePage does it.
>> ---
>> Sunil
>
IBinding[] bindings = IBindingService.getBindings();
Find your binding. Replace it in the array.
IBindingService.savePreferences(IBindingService.getActiveSch eme(),bindings);

That may do it.
---
Sunil
Re: How to register a key binding to a IAction [message #289548 is a reply to message #289531] Mon, 08 August 2005 07:53 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: prachi.chitnis.tcs.com

Hi,

As far as i could see, the API suggested by Sunil is not available with
Eclipse 3.0.
I guess a bug has already been filed at
https://bugs.eclipse.org/bugs/show_bug.cgi?id=40057

Any ideas????
Re: How to register a key binding to a IAction [message #289570 is a reply to message #289548] Mon, 08 August 2005 13:31 Go to previous message
Eclipse UserFriend
Originally posted by: sunil_kamath.nohotspammail.com

"Prachi Chitnis" <prachi.chitnis@tcs.com> wrote in message
news:6c3605e23c442d186c1d3e8374a5d8e8$1@www.eclipse.org...
> Hi,
>
> As far as i could see, the API suggested by Sunil is not available with
> Eclipse 3.0. I guess a bug has already been filed at
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=40057
>
Nope, the binding service stuff seems to be new with 3.1
---
Sunil
Previous Topic:Key binding for Navigator view popup menu action
Next Topic:Key binding for action in Eclipse 3.1 ?
Goto Forum:
  


Current Time: Thu Apr 25 21:16:34 GMT 2024

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

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

Back to the top