0

I have a JSF ajax keydown event linked to an event listener in a backing bean.

The code in the JSF file is below.

<p:inputText value="#{someBean.value}>
<p:ajax event="keydown" listener="#{someBean.keyDownEvent}" />
</p:inputText>

I want to get "Enter" button event code. When I click on enter I need to perform some specific event on server side. How can I get that event code using ajax call?

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
mallikarjun
  • 88
  • 1
  • 7
  • PrimeFaces has no specific key event handler at the bean side, so you need to use the onkeydown attribute and pass the key to a remoteCommand function to pass it to your bean. But why would you want to Ajax on each keydown? – Jasper de Vries May 12 '17 at 14:28
  • Where would you want to get the event code ? At the client side or in the baking bean ? – OTM May 12 '17 at 14:41
  • use 'onkeyup="jsfunction(event)" ' and check the keycode in your jsfunction and call your listener method . – Thanigai Arasu May 12 '17 at 15:11
  • the even "code" is keydown. you assigned this event to this handler. what other do you want to know? – The Bitman May 12 '17 at 15:30
  • I want to get "Enter" button event code. When I click on enter I need to perform some specific event on server side. How can I get that event code using ajax call? – mallikarjun May 12 '17 at 16:34
  • 1
    none of the answers solves your problem? – The Bitman May 15 '17 at 11:39
  • Did one of the answers solve your problem? Then please accept it. – Kukeltje Mar 07 '19 at 12:10

3 Answers3

6

Since you only want to perform an action when the Enter key is pressed, I really would not send every key to the backend using an Ajax call and there determine what key was pressed.

Simply check for enter in the front end, and if it was detected just then call the backend using a p:remoteCommand. But there is a catch here. Enter woud submit the entire form by default, so make sure to return false when the keyCode for enter (13) is found.

Then, validation. If you would happen to have multiple inputs in your form you might run into validation errors. So setup the remote command to only process itself and the input where the enter key was pressed. Optionally, you might want to update the p:message field linked to the input to show any validation errors.

This will lead to:

<p:remoteCommand name="myRemCo"
                 action="#{someBean.action}"
                 process="@this myInput"
                 update="myInputMessage"/>

<p:inputText id="myInput"
             value="#{someBean.value}"
             onkeydown="if (event.keyCode === 13) { myRemCo(); return false; }"
             required="true"/>
<p:message for="myInput" id="myInputMessage"/>

Note that I put required="true" there to demonstrate updating the message in case you press enter when the input is still empty.

See also:

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
0

If you need the code of the pressed key then here is an example. It assign an event handler to the inText input control. Here gets the code of the pressed key from the event object and call the onKeyPressed method of the controller by <p:remoteCommand>. The key code passed as a request parameter. The key code displayed in the outText output control as a test.

The facelet:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
  <h:head>
    <title>Facelet Title</title>
      <script type="text/javascript">
        function jsKeyPressedHandler(event)
        {
          var kc = event.which || event.keyCode;
          rc( [{ name : 'keyCode', value : Number( kc ) }] );
        }
      </script>

</h:head>
  <h:body>
    <h:form id="myForm">
    <p:remoteCommand name="rc" update="outText" actionListener="#{myBean.onKeyPressed}" />
    <p:inputText id="inText" value="#{myBean.text1}" onkeypress="jsKeyPressedHandler( event )"/>
    <h:outputText id="outText" value="#{myBean.text2}"/>
    </h:form>
  </h:body>

  </h:body>
</html>

The controller:

package x;

import java.io.Serializable;
import java.util.Map;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

@Named( value = "myBean" )
@ViewScoped
public class MyBean implements Serializable
{
  private String text1;
  private String text2;

  public String getText1() { return text1; }      
  public void setText1( String text1_ ) { text1 = text1_; }      
  public String getText2() { return text2; }      
  public void setText2( String text2_ ) { text2 = text2_; }      

  public void onKeyPressed()
  {
    Map<String,String> reqParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
    String c = (String) reqParams.get( "keyCode" );
    if ( text2 != null )
      text2 += c;
    else
      text2 = c;  
  }
}
The Bitman
  • 1,279
  • 1
  • 11
  • 25
0

You can use default command component of primefaces . check it out

Dennis
  • 186
  • 2
  • 9