GEF KeyHandler

本文介绍如何在Eclipse图形编辑器中通过代码实现注册操作、生成右键菜单及设置快捷键响应,包括具体步骤和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[b]第一段代码[/b]: 注册action
		this.actionRegistry = this.getActionRegistry();

IAction action = null;

action = new UndoAction(this.page.getEditor());
actionRegistry.registerAction(action);

这段代码的意义是: 往ActionRegistry中注册声明的action,当然只是注册,具体怎么用还要看右键菜单或Editor的actionbar怎么用了.

[b]第二段代码[/b]: 给GraphicalViewer区域生成右键菜单
		ContextMenuProvider provider = new GefAreaContextMenuProvider(getGraphicalViewer(), actionRegistry);
getGraphicalViewer().setContextMenu(provider);

每次在GraphicalViewer上点击右键的时候会生成右键菜单,具体怎么生成的就需要看ContextMenuProvider.buildContextMenu是怎么写的了.


[b]第三段代码[/b]: 初始化一个KeyHandler.
private KeyHandler sharedKeyHandler;
protected KeyHandler getCommonKeyHandler() {
if (sharedKeyHandler == null) {
sharedKeyHandler = new KeyHandler();
sharedKeyHandler.put(KeyStroke.getPressed(SWT.DEL, 127, 0), getActionRegistry().getAction(ActionFactory.DELETE.getId()));
}
return sharedKeyHandler;
}

这段代码是指:创建一个KeyHandler,表示,当用户按下Delete键的时候,调用getActionRegistry().getAction(ActionFactory.DELETE.getId()));这个action. 注:这步和Delete键绑定的action不能为空. 也就是在上面这段代码中,ActionFactory.DELETE必须已经在ActionRegistry中注册过了. 也就是第一段代码必须在这段代码前面.[当然,你也可以绑定自己的action,不一定非要在actionRegistry中注册]


[b]第四段代码[/b]: 给GraphicalViewer添加KeyHandler
viewer.setKeyHandler(new GraphicalViewerKeyHandler(viewer).setParent(getCommonKeyHandler()));

这步:就是给GraphicalViewer真正添加上面定义的KeyHandler.
setParent().. 这个的意思就是除了GraphicalViewerKeyHandler中自己截获的按钮事件外,还需要监听额外的keyCode按钮事件. 执行过程会先在GraphicalViewerKeyHandler中查找,当找不到,再在parent中查找是否对当前的key进行了监听.

[i]流水帐,别介意. 哈哈[/i]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值