Pietro Apollo
Pietro Apollo

Reputation: 21

updateAjaxAttributes is never invoked

on stackoverflow I found for Wicket some code which should respond to user's key press on the page; I only made some little adaptations. The problem is that the updateAjaxAttributes method and the onEvent method are never invoked when the user presses some keys on the page. The code is below:

    add(new AjaxEventBehavior("keydown")
    {
        @Override
        protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
            super.updateAjaxAttributes(attributes);

            IAjaxCallListener listener = new AjaxCallListener(){
                @Override
                public CharSequence getPrecondition(Component component) {
                    //this javascript code evaluates wether an ajaxcall is necessary.
                    //Here only by keyocdes for TAB and ENTER 
                    return  "var keycode = Wicket.Event.keyCode(attrs.event);" +
                            "if ((keycode == 9) || (keycode == 13))" +
                            "    return true;" +
                            "else" +
                            "    return false;";
                }
            };
            attributes.getAjaxCallListeners().add(listener);

            //Append the pressed keycode to the ajaxrequest 
            attributes.getDynamicExtraParameters()
                .add("var eventKeycode = Wicket.Event.keyCode(attrs.event);" +
                    "return {keycode: eventKeycode};");

            //whithout setting, no keyboard events will reach any inputfield
            attributes.setAllowDefault(true);
        }

        @Override
        protected void onEvent(AjaxRequestTarget target) {
            //Extract the keycode parameter from RequestCycle
            final Request request = RequestCycle.get().getRequest();
            final String jsKeycode = request.getRequestParameters()
                            .getParameterValue("keycode").toString("");

            target.appendJavaScript("alert('from wicket ajax. you pressed "+jsKeycode+"')");
        }
    });

Could someone tell me why these methods are never invoked and write me some working code? Thanks in advance, Pietro

Upvotes: 0

Views: 246

Answers (1)

Pietro Apollo
Pietro Apollo

Reputation: 21

add(new AjaxEventBehavior("onkeydown")

"onkeydown" is needed

Upvotes: 1

Related Questions