给rcp系统添加全局快捷键

文章转自:http://www.blogjava.net/chengang/archive/2006/04/28/43873.html
一个RCP系统,给它添加一个象Eclipse中Ctrl+Shift+O一样的全局快捷键,怎么加?
参考了RCP的Email项目示例,给我RCP项目中MonitorAction(显示一个监视器的命令)添加一个快捷键Ctrl+1。简单把添加的过程概述如下:

1、首先在plugin.xml中添加两个扩展点如下
  1. <extension   
  2.        point="org.eclipse.ui.bindings">   
  3.     <key   
  4.           commandId="org.eclipse.ui.file.exit"  
  5.           schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"  
  6.           sequence="CTRL+X"/>   
  7.     <key   
  8.           commandId="adminconsole.monitor"  
  9.           schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"  
  10.           sequence="CTRL+1"/>   
  11.  </extension>   
  12.   
  13.  <extension   
  14.        point="org.eclipse.ui.commands">   
  15.     <category   
  16.           id="adminconsole.perspective"  
  17.           name="Perspective"/>   
  18.     <command   
  19.           categoryId="adminconsole.perspective"  
  20.           id="adminconsole.monitor"  
  21.           name="Open Monitor Perspective"/>   
  22.  </extension> 

说明:
。org.eclipse.ui.file.exit是另一个退出系统命令,是复用了Eclipse本身的那个,它和本例无关。
。commandId要求唯一
。cmmands扩展点的category是一个组,估计不要也可以,最后还是加上吧。
。bindings扩展点中的commandId对应于cmmands扩展点中的id
2、创建一个类,专门保存所有commandId

Java代码 复制代码
  1. public interface ICommandIds {   
  2.     public static final String CMD_MONITOR = "adminconsole.monitor";   
  3. }   
  4.     
public interface ICommandIds {
    public static final String CMD_MONITOR = "adminconsole.monitor";
}
  


Java代码 复制代码
  1. private static class MonitorAction extends Action {   
  2.     public MonitorAction() {   
  3.         setText("监视");   
  4.         // The id is used to refer to the action in a menu or toolbar   
  5.         setId(ICommandIds.CMD_MONITOR);   
  6.         // Associate the action with a pre-defined command, to allow key bindings.   
  7.         setActionDefinitionId(ICommandIds.CMD_MONITOR);   
  8.     }   
  9. }  
    private static class MonitorAction extends Action {
        public MonitorAction() {
            setText("监视");
            // The id is used to refer to the action in a menu or toolbar
            setId(ICommandIds.CMD_MONITOR);
            // Associate the action with a pre-defined command, to allow key bindings.
            setActionDefinitionId(ICommandIds.CMD_MONITOR);
        }
    }


3、创建Action类,在构造函数里用两个方法注册一下


4、在ApplicationActionBarAdvisor的makeActions方法,给monitorAction对象加上这样一句

Java代码 复制代码
  1. register(monitorAction);  

注意:如果你没有在plugin.xml和makeActions做上面讲过的设置,加上这一句将可能导致
你的RCP系统无法启动。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值