Vaadin: MouseDown/MouseUp and KeyDown/KeyUp evens

六眼飞鱼酱① 提交于 2019-12-11 17:18:58

问题


Is it possible to handle MouseDown/MouseUp and KeyDown/KeyUp evens with Vaadin? I've found forum thread with the same question and looks like the answer is no, but it was 5 years ago - I hope something changed with later releases. Still I can't find anything in API. Maybe there's some workaround for intercepting such evens?


回答1:


Well, after couple of days I came up with the acceptable (for me) solution. Required component has to be wrapped with extension-interceptor (credits to @petey for an idea in the comments) with KeyDownHandler inside. But the trick is not to add to the component itself (because it can miss triggering), but to the RootPanel. So here's a working example.

Extension:

public class InterceptorExtension extends AbstractExtension {

    private boolean shiftKeyDown;

    public InterceptorExtension(Tree tree) {
        super.extend(tree);
        registerRpc((InterceptorExtensionServerRpc) state -> shiftKeyDown = state);
    }

    public boolean isShiftKeyDown() {
        return shiftKeyDown;
    }

}

ServerRpc:

public interface InterceptorExtensionServerRpc extends ServerRpc {

    void setShiftKeyDown(boolean state);

}

Connector:

@Connect(InterceptorExtension.class)
public class InterceptorExtensionConnector extends AbstractExtensionConnector {

    @Override
    protected void extend(final ServerConnector target) {
        final InterceptorExtensionServerRpc rpcProxy = getRpcProxy(InterceptorTreeExtensionServerRpc.class);
        final RootPanel rootPanel = RootPanel.get();
        rootPanel.addDomHandler(new KeyDownHandler() {
            @Override
            public void onKeyDown(KeyDownEvent event) {
                if (event.isShiftKeyDown()) {
                    rpcProxy.setShiftKeyDown(true);
                }
            }
        }, KeyDownEvent.getType());
        rootPanel.addDomHandler(new KeyUpHandler() {
            @Override
            public void onKeyUp(KeyUpEvent event) {
                if (!event.isShiftKeyDown()) {
                    rpcProxy.setShiftKeyDown(false);
                }
            }
        }, KeyUpEvent.getType());
    }

}

Then whenever you want you can get Shift-button state on the server-side via InterceptorExtension#isShiftKeyDown.



来源:https://stackoverflow.com/questions/45056632/vaadin-mousedown-mouseup-and-keydown-keyup-evens

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!