2 Replies Latest reply on May 28, 2010 5:04 AM by mettehummel

    How to get a4j:support for just some key press and not for all.

    mettehummel

      I'm using a4j:support event="onkeyup" actionListener="myActionListener" to do some stuff every time a key is pressed. It works fine, but I only want to do my stuff when some keys are pressed and not for all. How do I do that? I was hoping that the actionEvent would contain information about which key was pressed, but I can't seem to find that information. Can I limit the keys in the a4j:support ?

       

      Mette

        • 1. Re: How to get a4j:support for just some key press and not for all.
          bkersten

          Hi Mette,

           

          first of all bad news for direct usage cause AFAIK:

          • u can't limit <a4j:support event="onkeyup" .../> for certain keys
          • the ActionEvent object passed to your ActionListener method on server side does not contain information on the key pressed

           

          But (if u are not afraid of some lines of JS) u could:

          • use the a4j:jsFunction tag to trigger your actionListener
          • trigger your own JS function for onkeyup and call the jsFunction defined above if a certain key was pressed

           

          such as:

           

          <a4j:jsFunction name="callMyActionListener" actionListener="myActionListener">
          

           

          and

           

          function keyUpFunction( event ){
              
               if (  event.keyCode == Event.KEY_TAB
                  || event.keyCode == 73                 // e.g. letter "i", JS keyCodes can be found on web
                  || event.keyCode == Event.KEY_UP ){
          
                  callMyActionListener();
              }
          }
          

           

          and the onkeyup event for the tag u wanted to use a4j:support for

           

          <h:inputText onkeyup="keyUpFunction" />
          

           

          Con: some lines of JS

          Pro: compared to a server side check within the actionListener of which key was pressed this even reduces traffic and #roundtrips as an ajax request is only triggered if your desired key was pressed.

           

          Regards

          Benjamin

          • 2. Re: How to get a4j:support for just some key press and not for all.
            mettehummel

            Solved my problem. Just a small addition was needed of the event as a parameter to the function.

            Thanks!

             

            <h:inputText onkeyup="keyUpFunction(event)" />