I have a JSF ajax keyup event linked to an event listner in a backing bean.
The code in the JSF file is like below.
<h:inputText id="txtDescription" value="#{institutionController.current.description}" disabled="#{institutionController.modifyControlDisable}" >
<f:ajax event="keyup" listener="#{institutionController.changeDetailsEvent}" />
</h:inputText>
The code in the backing bean is like below.
public void changeDetailsEvent(AjaxBehaviorEvent event) {
}
I want to achieve different logic depending on the key presses, like shown is pseudocode below.
public void changeDetailsEvent(AjaxBehaviorEvent event) {
If (event.key = Key.enter) {
do something;
} else if (event.key = Key.Escape) {
so something else;
} else {
do nothing;
}
}
Can someone please tell me how this is done in the backing bean?
The
AjaxBehaviorEventdoesn’t contain any information about the JavaScripteventobject. You need to pass the desired information along yourself. This can be achieved by a hidden input field whose value is to be prefilled by JavaScript. For example,(please note that the
idof the hidden field is included inexecuteso that it get submitted along on the ajax request, please also note that thebindingis used to be able to dynamically obtain the generated client ID indocument.getElementById()in order to set the key code value, you could alternatively also hardcode the client ID if it’s fixed)with
You can find an overview of all valid
keyCodevalues in the Mozilla DOM reference.