Wednesday, September 10, 2014

Determining keyboard modifiers (like Shift-Click) on a Wicket AjaxLink

If you want to determine if a user issued, say, a shift-click on an AjaxLink in Wicket 6, here's how you do it:

1) Add pass the keyboard state from javascript to the onClick function,via DynamicExtraParameters
2) Get the passed value in the onClick callback via the RequestParameters

Here's some sample code:

add(new AjaxLink("my-link") {
    @Override
    protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
        super.updateAjaxAttributes(attributes);
        attributes.getDynamicExtraParameters()
            .add("return {shiftKey: attrs.event.shiftKey};");
    }

    @Override
    public void onClick(AjaxRequestTarget target) {
        boolean shiftKey =
            RequestCycle.get().getRequest().getRequestParameters().
                getParameterValue("shiftKey").toBoolean();
        if (shiftKey) {
            ....
        }
    }         
};

No comments: