这两天项目需要为一些组件添加快捷键的操作. 可是有些组件比提供键盘事件的支持,但是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.
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()
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 ... 键盘添加完成...