4 Replies Latest reply on Oct 29, 2009 8:19 AM by Thiagu Mariappan

    how to invoke a me method while click enter key

    Thiagu Mariappan Novice

      hi every one


      i need a text box with ajax support like on enter key press or one tab key press.
      so it will invoke the method wile click the enter key or the tab key.
      this is my sample code


      <h:inputText value="#{actionBean.searchString}">
      <a4j:support event="onblur" action="#{actionBean.search}">
      </h:inputText>
      



      so how to do that.
      which event is suitable for my requirment.






        • 1. Re: how to invoke a me method while click enter key
          Leo van den berg Master

          Hi,


          Use the onkeyup or -down events to handle the keyboard events. Be aware that it creates an Ajax-request for every click, so it is wise to add a queue to capture/maximize the key-events you would like to handle. See the RichFaces manual, which includes a very good piece of documentation about the queues.


          Leo


          • 2. Re: how to invoke a me method while click enter key
            Tim Evers Master

            It doesn't need to be complex to do what you are asking for.
            No need for queues, and you can avoid an ajax request per keypress.


            a4j:support tags have the onsubmit attribute. If the javascript put on this line returns false then the ajax request will not fire. So, all you have to do is check what key was pressed and if is not enter or tab return false. (I'm not sure if the event is passed into that javascript or not, but I'm sure you can work it out :)


            Now, all that being said. You should probably have a look at queues anyway if you are using ajax, because otherwise eventually you are going to get concurrent access exceptions.

            • 3. Re: how to invoke a me method while click enter key
              Tim Evers Master

              Oh I should have said, use the onkeyup event in your a4j:support tag


              So, you'll end up with something like this :S (you'll need to figure out the javascript part yourself as this would only work in IE I think.)


              <h:inputText ....>
                  <a4j:support 
                      event="onkeyup" 
                      onsubmit="if (window.event.keyCode == 9 || window.event.keyCode == 13) {return true;} return false;"
                      action="#{whatever your action is}"/>
              </h:inpuText
              



              Something like that. But, you will probably want to work on that javascript a bit. It is not tested and not cross browser compatible.


              Hope all goes well

              • 4. Re: how to invoke a me method while click enter key
                Thiagu Mariappan Novice

                thank you for all replies.
                the above script is not working in firefox,
                so we need to use window.event.which instead of window.event.keyCode.