7 Replies Latest reply on Nov 19, 2009 10:45 AM by Westchamp24

    Enter event for textbox

    J_hari

      I am creating a chat application,

      In the textbox when we press enter, the function for send message is called.

       

      I have seen in other chat applications that when we press 'Shift + Enter' it doesnot call the send function, it uses the normal enter key... ie, it will place the cursor in the next line....

       

      How can we implement it in Flex?

      Is it possible in TextInput or TextArea???

        • 1. Re: Enter event for textbox
          Subeesh Arakkan Level 4

          Hi,

           

          Use TextArea and listen for the keyDown event

           

                        private function onKeyDown(event:KeyboardEvent):void
                        {
                              if(event.keyCode == 13)//enter key 
                              {
                                   if(event.shiftKey)//if shift pressed
                                   {
                                        //do nothing
                                   }
                                   else
                                   {
                                        //write code to send message
                                   }
                              }                   
                        }
                   ]]>
              </mx:Script>
             <mx:TextArea keyDown="onKeyDown(event)" width="400" height="200"/>
          
          • 2. Re: Enter event for textbox
            Shivik

            Haven't tried this myself, but did you see here...

             

            http://j.mp/3aXxmd

             

            Hope this helps in some way

            ShiVik

            • 3. Re: Enter event for textbox
              J_hari Level 1

              What is &nbsp???

               

              This is code which we wrote as per your solution, bt now when we press enter, it goes to next line and at the same time it calls the function to send message(also the text is not getting cleared even though we have written code to clear it), and also when we click Shift+Enter, nothing happens, new line is not created.....

               

              <mx:TextArea width="146" height="40" id="TextArea_Send" x="0" text="hello" keyDown="Send_Enter(event)"/>

               

              public function Send_Enter(ev:KeyboardEvent):void
                      {
                          if(ev.keyCode==13) //enter key
                          {
                              if(ev.shiftKey)
                              {
                                 
                              }
                              else
                              {
                                  SendButtonClick();  // function for sending the message
                              }
                          }
                          else ;
                      }

              • 4. Re: Enter event for textbox
                Subeesh Arakkan Level 4

                Hi,

                 

                ndsp is the escape code for space ignore it

                 

                It is works for me check again

                 

                private function onKeyDown(event:KeyboardEvent):void
                            {
                                if(event.keyCode == 13)//enter key
                                {
                                    if(!event.shiftKey){
                                        Alert.show('send');
                                    }
                                }               
                            }

                • 5. Re: Enter event for textbox
                  J_hari Level 1

                  Sorry, it also didn't work...

                  • 6. Re: Enter event for textbox
                    Shivik Level 1

                    The code supplied by Subeesh earlier does work. May be you can get more help if you provide the particular snippet of your code for us to take a look, if in case you are doing something different.

                     

                    Thanks and Regards

                    ShiVik

                    • 7. Re: Enter event for textbox
                      Westchamp24

                      TextInput has an "enter" event.  Per Adobe's documentation "Dispatched when the user presses the Enter key."  Should be what are looking for.