A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: text input & enter/return

  1. #1
    Junior Member
    Join Date
    Aug 2008
    Posts
    2

    Question text input & enter/return

    I was wondering how i could code this so when i hit the enter key in the text input field it does the same as clicking the send button.

    I have googled this for hours and i cant seem to find something that works.

    This is for a TelnetSocket using AS3

    You can download the files i have Here (adobe live docs site) **look for the TelnetSocket file

    Code:
    package 
    {
    	import flash.display.Sprite;
    	import flash.utils.ByteArray;
    	import flash.events.MouseEvent;
    	import com.example.programmingas3.socket.Telnet;
    	
    	public class TelnetSocket extends Sprite 
    	{
            private var telnetClient:Telnet;
    
    		public function TelnetSocket() {
    			setupUI();
    		}
    		private function connect(e:MouseEvent):void {
    			telnetClient = new Telnet(serverName.text, int(portNumber.text), output);
    		}
    		private function sendCommand(e:MouseEvent):void {
    			var ba:ByteArray = new ByteArray();
    			ba.writeMultiByte(command.text + "\n", "UTF-8");
    			telnetClient.writeBytesToSocket(ba);
    			command.text = "";
    		}
    		private function setupUI():void {
    			loginBtn.addEventListener(MouseEvent.CLICK,connect)	
    			sendBtn.addEventListener(MouseEvent.CLICK,sendCommand); //<-- Here is the "Send" button
    		}		
    	}
    }
    I would appreciate any help thanks guys.

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    You can try this. 13 is the code for the enter button.

    addEventListener (KeyboardEvent.KEY_DOWN, keyDownHandler);
    function keyDownHandler (event:KeyboardEvent):void
    {
    if(event.keyCode==13)
    {
    trace("yes");
    }
    }

    When you type in the textinput and then press the enter key it will trace.
    Last edited by cancerinform; 08-05-2008 at 11:07 AM.
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Junior Member
    Join Date
    Aug 2008
    Posts
    2
    i asked the same question last night on the adobe boards here was the answer that i got. (it works!)

    Code:
    package 
    {
    	import flash.display.Sprite;
    	import flash.utils.ByteArray;
    	import flash.events.MouseEvent;
    	import flash.events.KeyboardEvent;  // NEW
    	import com.example.programmingas3.socket.Telnet;
    		
    	public class TelnetSocket extends Sprite 
    	{
    		private var telnetClient:Telnet;
    	
    		public function TelnetSocket() {
    			setupUI();
    		}
    		private function connect(e:MouseEvent):void {
    			telnetClient = new Telnet(serverName.text, 
    						int(portNumber.text), output);
    		}
    		private function sendCommand():void {
    			var ba:ByteArray = new ByteArray();
    			ba.writeMultiByte(command.text + "\n", "UTF-8");
    			telnetClient.writeBytesToSocket(ba);
    			command.text = "";
    		}
    		private function clickHandler(e:MouseEvent):void {
    			sendCommand();
    		}
    		private function keyHandler(e:KeyboardEvent)
    		{
    		if(e.keyCode==13 && (!(e.altKey || e.ctrlKey)) && e.target==command.textField)
    		{
    		sendCommand();
    		}
    		}
    		private function setupUI():void {
    			loginBtn.addEventListener(MouseEvent.CLICK,connect)	
    			sendBtn.addEventListener(MouseEvent.CLICK,clickHandler);
    			this.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
    		}		
    	}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center