【发布时间】:2014-01-01 20:39:13
【问题描述】:
对于我正在开发的文本编辑器,我已将默认操作覆盖为 HOME 键。但是在某些情况下,我仍然想使用原始的 HOME 动作(跳转到行首)。我怎样才能触发这个原始动作?
示例代码:
//Add the homekey and its action to the text-editor:
Key homeKey = KeyStroke.getKeyStroke("HOME");
editor.getInputMap().put(homeKey, new HomeCommand());
...
private class HomeCommand extends AbstractAction {
@Override
public void actionPerformed(ActionEvent ev) {
if(doSomethingSpecial())
performSpecialHomeAction();
else
performRegularHomeAction(); //How to get the regular action??
}
}
注意:我正在寻找优雅的解决方案,我不想编写自己的 HomeKey-Action 版本。
我用谷歌搜索并尝试了几种方法,但都没有奏效:
- 动作 a = area.getActionMap().get(KeyStroke.getKeyStroke("HOME")); 结果为空。
- 通过 Robot 类模拟按下 Home 键。然而,这只会触发我自己的自定义动作。
【问题讨论】:
-
你试过 super.performRegularHomeAction(); ?
-
@MadProgrammer 该函数不存在。如果您查看代码,您会看到我正在替换普通的 HomeAction-Class
-
为什么在放
HomeCommand之前没有一个regularHomeAction 的引用? -
我怎样才能得到它?如下:Action a = area.getActionMap().get(KeyStroke.getKeyStroke("HOME"));结果为空。
-
@MadProgrammer 是这个related
标签: java swing action key-bindings keyevent