This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • Identifying the key that was pressed in a serverside key handler
Skip to end of metadata
Go to start of metadata

This is a slight extension of Passing HTTP parameters to Ajax callback methods to answer a specific usecase.

Stipulating that you have an element with wicket:id 'body':

From Java code able to access that element within scope:

//...(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.
					 }
				}	
	    	});
//...
}
  • No labels