dcsimg
A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: How to press 'Enter' Key and call a function?

  1. #1
    Member
    Join Date
    Jun 2009
    Posts
    63

    Question How to press 'Enter' Key and call a function?

    Hi

    I have a text chat program which let users input text message.

    I want them able to input text by pressing 'Enter' key on keyboard.

    Code:
    if(Key.isDown(Key.ENTER)){
          addTextMessage();
        }
    It has compiled error on AS3. How should I fix it?

  2. #2
    Senior Member
    Join Date
    Nov 2005
    Posts
    192
    http://code.google.com/p/bigroom/wiki/KeyPoll
    http://bigroom.googlecode.com/files/KeyPoll.zip

    put the KeyPoll.as file in the folder with your main swf.

    From your document class, the main one, do

    Code:
    import flash.ui.keyboard;
    
    ...
    public static var Key:KeyPoll= new KeyPoll(stage);
    to call it from your otherclasses/functions you use.
    Code:
    if("Document class name".Key.isDown(Keyboard.ENTER)){
         call function
    }
    Alternatively, you can make your own Keys.as class that simply has the key codes for the key code you need like this:

    Code:
    package 
    {
    	
    	public class Keys 
    	{
    		
    		public function Keys() 
    		{
    			
    		}
    		public var Left:int = 37;
    		public var Up:int = 38;
    		public var Right:int = 39;
    		public var Down:int = 40;
    	}
    	
    }
    Use another public static variable for Keys like the KeyPoll.

    EDIT: I forgot you need to put stage in the KeyPoll parameters
    Last edited by Flyingcow; 07-02-2009 at 03:55 PM.

  3. #3
    Member
    Join Date
    Jun 2009
    Posts
    63
    When I write

    import flash.ui.keyboard;
    var Key:KeyPoll= new KeyPoll(stage);
    if(Key.isDown(Keyboard.ENTER)){
    doConnectKey();
    }

    It has compile error to say no such class.

    If I write
    import uk.co.bigroom.input.KeyPoll;

    It has no compile error. But there is no response when I press enter.

  4. #4
    Senior Member
    Join Date
    Nov 2005
    Posts
    192
    You need to put the KeyPoll.as file i linked in the same folder with your main swf, or the path of the class that uses it.

    If you did do that, please post the compile error.

  5. #5
    Member
    Join Date
    Jun 2009
    Posts
    63
    Finally, I got it run

    Here are the steps:

    1. put the as file in the directory \uk\co\bigroom\input\KeyPoll.as with your fla file because it is in a package uk.co.bigroom.input

    2.
    Code:
    import flash.display.Sprite;  
    import flash.events.Event;  
    import flash.ui.Keyboard;  
    import uk.co.bigroom.input.KeyPoll;
    
    var key:KeyPoll;    	      
    key = new KeyPoll(this.stage);      
    stage.addEventListener(Event.ENTER_FRAME, enterFrame);
    function enterFrame(ev:Event):void {      
    if(key.isDown(Keyboard.ENTER))  //For Enter key
    {        
    	callFunction();       
    } 
    }
    Thanks for help

  6. #6
    Member
    Join Date
    Apr 2009
    Posts
    41

    how to call functions in sequence

    I have set of 16 functions in AS2.0
    I need them to execute in a sequence so that when function-1 overs then it calls function-2 when function-2 overs then it calls function-3 and so on. SUch that it behave like movie.
    There is no fixed interval of time for execution of function.
    Each has its own time.
    Each function has some set of work that it perform on reading from XML file.
    any thoughts,
    thanking you

  7. #7
    Member
    Join Date
    Jun 2009
    Posts
    63
    Can you do that like:

    function1()
    {
    execute job 1;

    function2();
    }

    function2()
    {
    execute job 2;

    function3();
    }

    function3()
    {
    execute job 3;
    stop();
    }

  8. #8
    Member
    Join Date
    Apr 2009
    Posts
    41

    functional call

    I have tried to do it...but it do in a while ....
    All function get executed at a time.
    Before completion one other starts.
    Any other way to do it.

    Thanks.

  9. #9
    Member
    Join Date
    Jun 2009
    Posts
    63
    Firstly, I think you can add some trace point to do debug

    e.g. function1()
    {
    trace("function1 starts");

    execute code;
    ......

    trace("function1 end");

    function2();
    }

    another way is put those functions in different frames. after excute all functions in one frame and gotoAndStop(2);

    Or could you post your code here if problem has not solved.

  10. #10
    Senior Member
    Join Date
    Nov 2005
    Posts
    192
    Quote Originally Posted by wowptc View Post
    Finally, I got it run

    Here are the steps:

    1. put the as file in the directory \uk\co\bigroom\input\KeyPoll.as with your fla file because it is in a package uk.co.bigroom.input

    2.
    Code:
    import flash.display.Sprite;  
    import flash.events.Event;  
    import flash.ui.Keyboard;  
    import uk.co.bigroom.input.KeyPoll;
    
    var key:KeyPoll;    	      
    key = new KeyPoll(this.stage);      
    stage.addEventListener(Event.ENTER_FRAME, enterFrame);
    function enterFrame(ev:Event):void {      
    if(key.isDown(Keyboard.ENTER))  //For Enter key
    {        
    	callFunction();       
    } 
    }
    Thanks for help
    No problem,
    But you didn't need to make the the directories uk.co.bigroom.input.KeyPoll;
    Just edit the KeyPoll.as so that it's package matches your root document or wherever you put it

    for example
    c:/flash/Game.fla
    c:/flash/Game.as
    c:/flash/util/KeyPoll.as

    Code:
    ***KeyPoll.as***
    package util
    {
    	
    ...
    	public class KeyPoll 
    	{
    Code:
    ***Game.as****
    package
    {
         import util.*;
         ...
         public class Game extends Sprite{
         ...

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