THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
Code Block |
---|
//...(class declarations)
final static String KEYPRESS_PARAM = "keyPressed";
//...
private void handleA(){}//Desired behavior when 'a' is pressed on the client
private void handleB(){}//Desired behavior when 'b' is pressed on the client
public void someSetupMethod(){
body.add(new AjaxEventBehavior("onkeypress"){
@Override
protected CharSequence getCallbackScript(boolean onlyTargetActivePage)
{
return generateCallbackScript("wicketAjaxGet('"+getCallbackUrl(onlyTargetActivePage)+"&"+KEYPRESS_PARAM+"='+wicketKeyCode(event)");
}
@Override
protected void onEvent(AjaxRequestTarget target) {
String paramValue = RequestCycle.get().getRequest().getParameter(KEYPRESS_PARAM);
try{//The key parameter is the pressed keychar's ascii value
int key = Integer.parseInt(paramValue);
switch(key){
case 'a':
handleA();
break;
case 'b':
handleB();
break;
}
}
catch(NumberFormatException e){//Catches the possibly null parameter, or malformation.
return;//You might handle this a bit more responsibly.
}
}
});
//...
}
|