如何在面板上的任何位置检测按键?

时间:2014-04-07 16:59:14

标签: java gwt gxt

我的屏幕有多个搜索参数,并在使用分页的表格中显示结果。用户希望能够向上翻页或向下翻页以更改桌面上的页面而无需触摸他们的鼠标。我将如何在GWT中这样做?

3 个答案:

答案 0 :(得分:2)

将KeyUpHandler添加到您的视图中。检测键码是否等于向上箭头或向下箭头。如果是,请更改页码并刷新表格。

更新:

这是将KeyUpHandler添加到Composite小部件的方法:

myPanel.addDomHandler(new KeyUpHandler() {

    @Override
    public void onKeyUp(KeyUpEvent event) {
        if (event.getNativeKeyCode() == KeyCodes.KEY_DOWN) {
            // Reduce page number
        }
    }
}, KeyUpEvent.getType());

答案 1 :(得分:0)

public void onModuleLoad() {
Element body = getElementByTagName("body");
        Event.sinkEvents(body, Event.ONKEYDOWN);
        Event.setEventListener(body, new EventListener() {

            @Override
            public void onBrowserEvent(Event event) {
                System.out.println("ok");
                 if(Event.ONKEYDOWN == event.getTypeInt()) {
                     Window.alert("ok");
                      System.out.println("KeyDown");
                 }

            }
        });
}

/**
 * Gets an element by its tag name; handy for single elements like HTML,
 * HEAD, BODY.
 * @param tagName The name of the tag.
 * @return The element with that tag name.
 */
public native static Element getElementByTagName(String tagName) /*-{
  var elem = $doc.getElementsByTagName(tagName);
  return elem ? elem[0] : null;
}-*/;

答案 2 :(得分:0)

试试这个

    Event.addNativePreviewHandler(new Event.NativePreviewHandler() {

        @Override
        public void onPreviewNativeEvent(final NativePreviewEvent event) {
            if (event.getTypeInt() == Event.ONKEYUP) {
                if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_LEFT) {
                    System.out.println("left key is pressed");
                } else if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_RIGHT) {
                    System.out.println("right key is pressed");
                } else {
                    System.out.println(event.getNativeEvent().getKeyCode() + " is pressed");
                }
            }
        }
    });

    DOM.sinkEvents(composite.getElement(), Event.ONKEYUP);