GEF-通过绑定键盘事件实现快捷键功能
2014-02-16 01:03
399 查看
本文中涉及到的例子可以在以下URL下载。导入eclipse项目即可,我的环境的Eclipse4.3.1
http://download.csdn.net/detail/hoslay1/6925761
如果有问题请联系我QQ:195307039说明来意即可。
以下Editor代表外部的编辑器,EditPart代表GEF的EditPart
GEF中如果希望在画板上通过快捷键来操作EditPart如:delete或者Ctrl+Z等等
可以通过KeyHandler来实现,以下是实现方式:
在Editor的configureGraphicalViewer方法中,创建一个KeyHandler对象,并设置其与Action的绑定,最后将KeyHandler对象设置到viewer上。
keyHandler的put方法就是将键盘行为与action进行绑定
KeyStroke.getPressed取得的是某个单键按下的行为
KeyStroke.getReleased取得的是组合按键按下的行为
getActionRegistry().getAction可以取得在这个Editor上生效的Action
最后viewer.setKeyHandler(keyHandler);将这组绑定设置到viewer上
DIRECT_EDITAction定义在当前Editor中,而其它的Action定义在父类GraphicalEditor中
Action必须要先创建并且注册才能在这里被取出来
这里贴上父类的代码:
GEFActionConstants.DELETE等就是这些Action的ID属性,通过这个ID属性就能找到这个Action,自定义的Action的获取就能通过那个ID。
http://download.csdn.net/detail/hoslay1/6925761
如果有问题请联系我QQ:195307039说明来意即可。
以下Editor代表外部的编辑器,EditPart代表GEF的EditPart
GEF中如果希望在画板上通过快捷键来操作EditPart如:delete或者Ctrl+Z等等
可以通过KeyHandler来实现,以下是实现方式:
在Editor的configureGraphicalViewer方法中,创建一个KeyHandler对象,并设置其与Action的绑定,最后将KeyHandler对象设置到viewer上。
@Override protected void configureGraphicalViewer() { super.configureGraphicalViewer(); viewer = getGraphicalViewer(); //设置partFactory viewer.setEditPartFactory(new PartFactory()); //创建键盘处理的句柄 KeyHandler keyHandler = new KeyHandler(); //按delete时删除action,发起DELETE类型的事件,然后捕获 keyHandler.put(KeyStroke.getPressed(SWT.DEL, 127, 0), getActionRegistry().getAction(GEFActionConstants.DELETE)); //按F2时编辑 keyHandler.put(KeyStroke.getPressed(SWT.F2, 0), getActionRegistry().getAction(GEFActionConstants.DIRECT_EDIT)); //按Ctrl+Z撤销 keyHandler.put(KeyStroke.getReleased('',122,SWT.CTRL), getActionRegistry().getAction(GEFActionConstants.UNDO)); //按Ctrl+Y反撤销 keyHandler.put(KeyStroke.getReleased('',121,SWT.CTRL), getActionRegistry().getAction(GEFActionConstants.REDO)); //按Ctrl+A全选 keyHandler.put(KeyStroke.getReleased('',97,SWT.CTRL), getActionRegistry().getAction(GEFActionConstants.SELECT_ALL)); //按F3弹出一个dialog,快捷键也可以基于此类的实现或者是GraphicalViewerHandler //keyHandler.put(KeyStroke.getPressed(SWT.F3, 0), getActionRegistry().getAction("TestDialog")); // GraphicalViewerKeyHandler可实现自定义快捷键以及键盘移动等功能 viewer.setKeyHandler(keyHandler); }
keyHandler的put方法就是将键盘行为与action进行绑定
KeyStroke.getPressed取得的是某个单键按下的行为
KeyStroke.getReleased取得的是组合按键按下的行为
getActionRegistry().getAction可以取得在这个Editor上生效的Action
最后viewer.setKeyHandler(keyHandler);将这组绑定设置到viewer上
DIRECT_EDITAction定义在当前Editor中,而其它的Action定义在父类GraphicalEditor中
Action必须要先创建并且注册才能在这里被取出来
这里贴上父类的代码:
protected void createActions() { ActionRegistry registry = getActionRegistry(); IAction action; action = new UndoAction(this); registry.registerAction(action); getStackActions().add(action.getId()); action = new RedoAction(this); registry.registerAction(action); getStackActions().add(action.getId()); action = new SelectAllAction(this); registry.registerAction(action); action = new DeleteAction((IWorkbenchPart) this); registry.registerAction(action); getSelectionActions().add(action.getId()); action = new SaveAction(this); registry.registerAction(action); getPropertyActions().add(action.getId()); registry.registerAction(new PrintAction(this)); }
GEFActionConstants.DELETE等就是这些Action的ID属性,通过这个ID属性就能找到这个Action,自定义的Action的获取就能通过那个ID。
相关文章推荐
- 原生js实现必应自动搜索功能(事件绑定和事件代理和键盘事件),亲测chrome实现
- Flex(AS 3.0)实现快捷键功能--监听键盘事件
- 通过全局鼠标键盘事件实现应用程序的自动锁定
- js事件实现通过键盘移动图片
- JAVA实现通过绑定邮箱找回密码功能
- ExtJS监听键盘事件:回车键实现登录功能
- 基于jQuery实现类似Google+圈子选择功能插件(支持键盘事件)
- vim通过事件驱动实现实时自动保存功能
- 如何绑定android点击事件--跳转到另一个页面并实现关闭功能?
- 用程序来模拟Alt+PrtSc的键盘事件,实现截屏功能
- 原生js实现下拉框功能(支持键盘事件)
- 编写一个通讯录,功能与以前的诺基亚手机一样,可以通过键盘进行箭头的移动,回车进入选项,用“W”键代表手机手机左键,“backspace”键代表返回上一级。能够实现基本的人性化的增删改查及文件的导入导出
- ES6中javascript实现函数绑定及类的事件绑定功能详解
- Flex中通过扩展List类重载protected keyDownHandler()事件使键盘导航(keyboard navigation)功能无效的例子
- vue.js通过子组件通信和父子组件通信实现动态表单动态绑定事件(适合交互系统统一使用)
- jQuery实现的事件绑定功能基本示例
- vue.js--基础 事件结合双向数据绑定实现todolist,增加和删除功能
- js实现快捷键绑定按钮点击事件
- 如何通过配置来实现IP+MAC+端口绑定功能
- vue.js--基础 事件结合双向数据绑定实现todolist 待办事项 已经完成 和进行中,键盘事件