使用 commandId 检索活动键绑定
Retrieve active key binding using a commandId
我目前正在开发一个 Eclipse RCP 插件,其中包含一组工具栏按钮触发操作,也可以通过键盘快捷键触发。
这是我的 plugin.xml 文件的相关部分,包括上下文、命令和快捷方式的相应键绑定:
<plugin>
<extension point="org.eclipse.ui.contexts">
<context id="notepad4e.context" name="In Notepad4e" parentId="org.eclipse.ui.contexts.window" />
</extension>
<extension point="org.eclipse.ui.commands">
<category id="notepad4e.command.category" name="Notepad4e" description="Category for Notepad4e commands" />
<command categoryId="notepad4e.command.category" id="notepad4e.command.text.bold" description="Sets style to bold" name="Bold" />
</extension>
<extension point="org.eclipse.ui.bindings">
<key commandId="notepad4e.command.text.bold" contextId="notepad4e.context" sequence="CTRL+B" schemeId="org.eclipse.ui.defaultAcceleratorConfiguration" />
</extension>
</plugin>
在我的 Java 代码中,我正确地激活了我的视图的 notepad4e.context
并激活了一个处理程序来监听相应的键盘事件。键盘快捷键似乎工作正常,用户可以在 Eclipse 的首选项中重新定义键绑定以最好地满足他的需要。
我想为工具栏按钮设置工具提示文本,其中包含有关绑定的信息,如下所示:
我无法对键绑定信息进行硬编码,因为用户可能会更改绑定,而硬编码值将不再有效。我正在尝试提出一种解决方案,以编程方式检索给定命令的键绑定。我使用 IBindingService 进行了以下三次尝试:
private String getShortcutDescription1() {
IBindingService bindingService = getViewSite().getService(IBindingService.class);
TriggerSequence triggerSequence = bindingService.getBestActiveBindingFor("notepad4e.command.text.bold");
if (triggerSequence != null) {
return triggerSequence.format();
}
return "";
}
private String getShortcutDescription2() {
IBindingService bindingService = getViewSite().getService(IBindingService.class);
TriggerSequence[] triggerSequences = bindingService.getActiveBindingsFor("notepad4e.command.text.bold");
if (triggerSequences.length > 0) {
return triggerSequences[0].format();
}
return "";
}
private String getShortcutDescription3() {
IBindingService bindingService = getViewSite().getService(IBindingService.class);
for (Binding binding : bindingService.getBindings()) {
if (binding.getParameterizedCommand() != null
&& "notepad4e.command.text.bold".equals(binding.getParameterizedCommand().getId())) {
return binding.getTriggerSequence().format();
}
}
return "";
}
前两个解决方案似乎没有找到与 notepad4e.command.text.bold
commandId 关联的任何绑定。然而,对于第三种解决方案,我确实找到了预期的绑定。尽管如此,遍历 Eclipse 的所有绑定似乎并不是一个有效的解决方案,更重要的是,如果用户在其首选项中重新定义默认快捷方式,bindingService.getBindings()
调用 returns 一个包含非活动默认绑定的集合和活动的重新定义的用户一,所以这也不会 return 我正在寻找的值。
我在这里错过了什么?我如何以编程方式检索给定 commandId 的活动键绑定?
在此先致谢,如有任何帮助,我们将不胜感激!
编辑 17/01: 前两个解决方案执行 return 想要的绑定,但它们似乎只在用户与插件交互后才这样做。这是不切实际的,因为我试图在设置插件时设置工具提示。
在此处进行测试,您似乎在激活上下文后过早地调用了 getBestActiveBindingFor
代码。
只需更改您的代码以在 Display.asyncExec
Runnable
中调用 getShortcutDescription1
即可使其正常工作。因此,您似乎需要完成一些设置才能进行此调用。
为了测试我使用了:
Display.getDefault().asyncExec(() ->
{
System.out.println("trigger " + getShortcutDescription1());
});
最后我选择了以下解决方案:
private String getShortcutDescription() {
Binding bestBinding = null;
for (Binding binding : getViewSite().getService(IBindingService.class).getBindings()) {
if (binding.getParameterizedCommand() != null
&& "notepad4e.command.text.bold".equals(binding.getParameterizedCommand().getId())) {
if (bestBinding == null) {
bestBinding = binding;
} else if (binding.getType() == Binding.USER) {
// Give higher priority to a user type binding (user has overriden default).
bestBinding = binding;
break;
}
}
}
return bestBinding == null ? "" : " " + bestBinding.getTriggerSequence().format();
}
即使用户尚未与插件交互(上下文处于活动状态但视图未处于活动状态),也会返回正确的绑定。如问题中所述,如果用户在其首选项中重新定义默认快捷方式,则上述 bindingService.getBindings()
调用 returns 一个包含非活动默认绑定和活动重新定义用户的集合。如果找到,我 select 类型为 Binding.USER
的那个。如果用户没有重新定义绑定,则只有一个类型为 Binding.SYSTEM
的绑定存在并返回。
我目前正在开发一个 Eclipse RCP 插件,其中包含一组工具栏按钮触发操作,也可以通过键盘快捷键触发。
这是我的 plugin.xml 文件的相关部分,包括上下文、命令和快捷方式的相应键绑定:
<plugin>
<extension point="org.eclipse.ui.contexts">
<context id="notepad4e.context" name="In Notepad4e" parentId="org.eclipse.ui.contexts.window" />
</extension>
<extension point="org.eclipse.ui.commands">
<category id="notepad4e.command.category" name="Notepad4e" description="Category for Notepad4e commands" />
<command categoryId="notepad4e.command.category" id="notepad4e.command.text.bold" description="Sets style to bold" name="Bold" />
</extension>
<extension point="org.eclipse.ui.bindings">
<key commandId="notepad4e.command.text.bold" contextId="notepad4e.context" sequence="CTRL+B" schemeId="org.eclipse.ui.defaultAcceleratorConfiguration" />
</extension>
</plugin>
在我的 Java 代码中,我正确地激活了我的视图的 notepad4e.context
并激活了一个处理程序来监听相应的键盘事件。键盘快捷键似乎工作正常,用户可以在 Eclipse 的首选项中重新定义键绑定以最好地满足他的需要。
我想为工具栏按钮设置工具提示文本,其中包含有关绑定的信息,如下所示:
我无法对键绑定信息进行硬编码,因为用户可能会更改绑定,而硬编码值将不再有效。我正在尝试提出一种解决方案,以编程方式检索给定命令的键绑定。我使用 IBindingService 进行了以下三次尝试:
private String getShortcutDescription1() {
IBindingService bindingService = getViewSite().getService(IBindingService.class);
TriggerSequence triggerSequence = bindingService.getBestActiveBindingFor("notepad4e.command.text.bold");
if (triggerSequence != null) {
return triggerSequence.format();
}
return "";
}
private String getShortcutDescription2() {
IBindingService bindingService = getViewSite().getService(IBindingService.class);
TriggerSequence[] triggerSequences = bindingService.getActiveBindingsFor("notepad4e.command.text.bold");
if (triggerSequences.length > 0) {
return triggerSequences[0].format();
}
return "";
}
private String getShortcutDescription3() {
IBindingService bindingService = getViewSite().getService(IBindingService.class);
for (Binding binding : bindingService.getBindings()) {
if (binding.getParameterizedCommand() != null
&& "notepad4e.command.text.bold".equals(binding.getParameterizedCommand().getId())) {
return binding.getTriggerSequence().format();
}
}
return "";
}
前两个解决方案似乎没有找到与 notepad4e.command.text.bold
commandId 关联的任何绑定。然而,对于第三种解决方案,我确实找到了预期的绑定。尽管如此,遍历 Eclipse 的所有绑定似乎并不是一个有效的解决方案,更重要的是,如果用户在其首选项中重新定义默认快捷方式,bindingService.getBindings()
调用 returns 一个包含非活动默认绑定的集合和活动的重新定义的用户一,所以这也不会 return 我正在寻找的值。
我在这里错过了什么?我如何以编程方式检索给定 commandId 的活动键绑定?
在此先致谢,如有任何帮助,我们将不胜感激!
编辑 17/01: 前两个解决方案执行 return 想要的绑定,但它们似乎只在用户与插件交互后才这样做。这是不切实际的,因为我试图在设置插件时设置工具提示。
在此处进行测试,您似乎在激活上下文后过早地调用了 getBestActiveBindingFor
代码。
只需更改您的代码以在 Display.asyncExec
Runnable
中调用 getShortcutDescription1
即可使其正常工作。因此,您似乎需要完成一些设置才能进行此调用。
为了测试我使用了:
Display.getDefault().asyncExec(() ->
{
System.out.println("trigger " + getShortcutDescription1());
});
最后我选择了以下解决方案:
private String getShortcutDescription() {
Binding bestBinding = null;
for (Binding binding : getViewSite().getService(IBindingService.class).getBindings()) {
if (binding.getParameterizedCommand() != null
&& "notepad4e.command.text.bold".equals(binding.getParameterizedCommand().getId())) {
if (bestBinding == null) {
bestBinding = binding;
} else if (binding.getType() == Binding.USER) {
// Give higher priority to a user type binding (user has overriden default).
bestBinding = binding;
break;
}
}
}
return bestBinding == null ? "" : " " + bestBinding.getTriggerSequence().format();
}
即使用户尚未与插件交互(上下文处于活动状态但视图未处于活动状态),也会返回正确的绑定。如问题中所述,如果用户在其首选项中重新定义默认快捷方式,则上述 bindingService.getBindings()
调用 returns 一个包含非活动默认绑定和活动重新定义用户的集合。如果找到,我 select 类型为 Binding.USER
的那个。如果用户没有重新定义绑定,则只有一个类型为 Binding.SYSTEM
的绑定存在并返回。