软件中有些功能的使用很频繁,或者有些功能需要能够非常快速的响应,前者如查找,后者如锁住屏幕功能。这样的功能就需要提供可以帮助快速操作的快捷键。比如在很多软件中常见的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绑定快捷键。