我希望为按钮添加处理程序或监听器,每次按键盘键都会响应它甚至不关注它。我怎么能这样做?
答案 0 :(得分:1)
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
Event.addNativePreviewHandler(new NativePreviewHandler() {
@Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_DELETE) { //needs an int code
//fire an event to your button or call your method
event.getNativeEvent().stopPropagation(); //optional
event.getNativeEvent().preventDefault(); //optional
}
}
});
我在onModuleLoad()方法中使用这样的代码来捕获按键事件并将其从浏览器中隐藏。您也可以使用
import com.google.gwt.user.client.Element;
Element as = (Element) Element.as(event.getNativeEvent().getEventTarget());
as.getTagName();
检查它的来源(例如标记名称为“input”,“textarea”),并根据该事件处理。