在RCP中绑定Action的快捷键

软件中有些功能的使用很频繁,或者有些功能需要能够非常快速的响应,前者如查找,后者如锁住屏幕功能。这样的功能就需要提供可以帮助快速操作的快捷键。比如在很多软件中常见的CTRL+F,会打开查找对话框。

在ipass中需要实现按住CTRL+L立刻锁屏的功能,效果类似notes的F5。

定义ApplicationActionBarAdvisor类,继承ActionBarAdvisor,RCP的系统Action都在ApplicationActionBarAdvisor中定义,创建。实现protected void makeActions(final IWorkbenchWindow window),在其中创建自己定义的类,例如: Action lockAction = new LockAction(window);
            register(lockAction);   

第二句必须执行,它是将此Action注册到系统的键盘帮定服务上,同时保存此Action,在系统关闭的时候释放此Action资源。LockAction的代码如下:

 

public class LockPassGateViewAction extends Action {
 public static final String ID = "com.mi.pwdgate.action.LockPassGateViewAction";

 public LockPassGateViewAction() {
  setText("Lock");
  // The id is used to refer to the action in a menu or toolbar
   setId(ID);
  // Associate the action with a pre-defined command, to allow key  bindings.
   setActionDefinitionId("pwdgate.lock.passgateview");
   setImageDescriptor(ImageShop.getDescriptor("security"));
   ImageShop.getDescriptor("Lock PassGate View");
 }

 public void run() {
   //locks the screen

 }
}

在plugin.xml文件中加入如下部分:

<extension
         point="org.eclipse.ui.commands">
      <category
            name="PassGate"
            id="passgate.category">
      </category>
      <command
            name="Lock Screen"
            description=""
            id="pwdgate.lock.passgateview">
      </command>
   </extension>
   <extension
         point="org.eclipse.ui.bindings">
      <key
            commandId="pwdgate.lock.passgateview"
            schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
            sequence="CTRL+L">
      </key>
   </extension>

bindings中,key项的commandId指向要绑定的command的id,同时Action要设定自己指向的command的id.即 setActionDefinitionId("pwdgate.lock.passgateview");

这样就可以在程序运行时,按住CTRL+L锁住屏幕。

此方法只是在rcp开发时可用,如果开发plugin,应为无法取到ActionBarAdvisor,也就无法将自己的Action注册到系统中。这是就要利用ActionSet的extension来实现此功能。在eclipse中生成Plug-in with a popup menu 的example,就可以看到怎样用ActionSet的extension来为自己的Action绑定快捷键。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值