为什么 GWT 中的右键单击捕获在 IE 中不起作用?

发布于 2024-09-12 07:34:59 字数 1627 浏览 1 评论 0原文

我正在尝试捕获小部件上的右键单击,以弹出我自己的上下文菜单而不是浏览器的上下文菜单。有一些关于此的参考资料,但最受欢迎的一个这里< /a> 有点过时,尽管一些注释包含更新的代码片段。

我已经将一些内容拼凑在一起,并且可以在 Chrome 和 FF 中运行,但不能在 IE 中运行。在 IE 中,它不显示默认浏览器上下文菜单,但也不显示我的菜单。我刚刚开始接触 GWT,所以我假设我没有使用正确类型的处理程序或事件做正确的事情。我还使用 gwt-graphics 模块,这就是我扩展的 Rectangle 类的来源,以防相关。

这是我的代码:

public class RectangleRightClickable extends Rectangle {

public RectangleRightClickable(int x, int y, int width, int height) {
    super(x, y, width, height);
    sinkEvents(Event.ONCONTEXTMENU);
}

public void onBrowserEvent(Event event) {
    GWT.log("onBrowserEvent");
    event.stopPropagation();
    event.preventDefault();
    GWT.log("event type : " + DOM.eventGetType(event));
    switch(DOM.eventGetType(event)) {
    case Event.ONCONTEXTMENU:
        if (DOM.eventGetButton(event) == Event.BUTTON_RIGHT) {
            GWT.log("Event.BUTTON_RIGHT", null);
            showMenu();
        }
        break;
    default:
        GWT.log(event.toString());
        break;
    }
}   

protected void showMenu() {
    final RectangleRightClickable parent = this;
    final PopupMenu popMenu = new PopupMenu();
    popMenu.addMenuItem(new Label("Add thing"));
    popMenu.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
        public void setPosition(int offsetWidth, int offsetHeight) {
            int left = parent.getX() + parent.getWidth();
            int top = parent.getY() + parent.getWidth();
            popMenu.setPopupPosition(left, top);
        }
    });

}
}

I'm trying to capture right-clicks on a widget, to popup my own context menu instead of the browser's. There are a couple references on this, but the most popular one here is a little dated, although some of the comments contain more recent code snippets.

I've pieced together bits and I've got it working in Chrome and FF but not IE. In IE it doesn't display the default browser context menu, but it doesn't display my menu. I'm just getting into GWT so I'm assuming I'm not doing something right with the right kinds of handlers or events. I'm also using the gwt-graphics module, that's where the Rectangle class that I'm extending comes from, in case that's relevant.

Here's my code:

public class RectangleRightClickable extends Rectangle {

public RectangleRightClickable(int x, int y, int width, int height) {
    super(x, y, width, height);
    sinkEvents(Event.ONCONTEXTMENU);
}

public void onBrowserEvent(Event event) {
    GWT.log("onBrowserEvent");
    event.stopPropagation();
    event.preventDefault();
    GWT.log("event type : " + DOM.eventGetType(event));
    switch(DOM.eventGetType(event)) {
    case Event.ONCONTEXTMENU:
        if (DOM.eventGetButton(event) == Event.BUTTON_RIGHT) {
            GWT.log("Event.BUTTON_RIGHT", null);
            showMenu();
        }
        break;
    default:
        GWT.log(event.toString());
        break;
    }
}   

protected void showMenu() {
    final RectangleRightClickable parent = this;
    final PopupMenu popMenu = new PopupMenu();
    popMenu.addMenuItem(new Label("Add thing"));
    popMenu.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
        public void setPosition(int offsetWidth, int offsetHeight) {
            int left = parent.getX() + parent.getWidth();
            int top = parent.getY() + parent.getWidth();
            popMenu.setPopupPosition(left, top);
        }
    });

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕花眠 2024-09-19 07:34:59

在 GWT 谷歌群组列表中得到了这个回复,它有效:

addDomHandler(new ContextMenuHandler() 
{ 
    @Override 
    public void onContextMenu(ContextMenuEvent event) 
    { 
        showMenu(); 
        event.preventDefault(); 
    } 
}, ContextMenuEvent.getType()); 

Got this response on the GWT google groups list, which worked:

addDomHandler(new ContextMenuHandler() 
{ 
    @Override 
    public void onContextMenu(ContextMenuEvent event) 
    { 
        showMenu(); 
        event.preventDefault(); 
    } 
}, ContextMenuEvent.getType()); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文