使用KeyNav为UI Component添加键盘事件

    这两天项目需要为一些组件添加快捷键的操作. 可是有些组件比提供键盘事件的支持,但是Ext GWT中提供了KeyNav这个类来解决这个问题。

    在这里先看一下API中对该类的描述:

GXT API KeyNav类描述 写道
public class KeyNav<E extends ComponentEvent>
extends BaseObservable
implements Listener<E>
Provides a convenient wrapper for normalized keyboard navigation. Provides an easy way to implement custom navigation schemes for any UI component.

   该类为常用键盘响应事件提供了一个封装,为任何UI组件提供了响应键盘时间的解决方案。[根据自己的理解的意思翻译过来的]

   

    在看下构造方法:

GXT API KeyNav 类构造方法描述 写道
Constructor Summary
KeyNav()
Creates a new KeyNav without a target component.
KeyNav(Component target)
Creates a new key nav for the specified target.

    KeyNav(Component target) : 为指定的目标添加键盘事件监听(直译过来应该是: 键盘导航)

 

看到这里基本上知道KeyNav这个类如何使用了..

    首先定义个KeyNav对象, 然后为该KeyNav对象指定UI组件。 然后重写KeyNav对象的相关方法即可..

 

下面一段代码段说明如何为FormPanel组件添加键盘事件:

FormPanel simple = new FormPanel();

KeyNav<ComponentEvent> keyNav = new KeyNav<ComponentEvent>(simple){
		@Override
		public void handleEvent(ComponentEvent ce) {
			//Info.display("key", "" + ce.getKeyCode());
				
			// 屏蔽其他特殊按键
			if(!(ce.isAltKey() || ce.isRightClick() || 
				ce.isShiftKey() || ce.isSpecialKey())) {
					
			// Ctrl + S 执行保存操作
			(ce.isControlKey() && ce.getKeyCode() == 83) {
				//Info.display("key", "save");
			}
					
			// Ctrl + X 执行新建操作
			if(ce.isControlKey() && ce.getKeyCode() == 88) {
				//Info.display("key", "new");
						
			}
		}												
	}
};

 Ok ...  键盘添加完成...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值