A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Keypress....I know, it's been posted a million times...but..

  1. #1
    Member
    Join Date
    Jan 2003
    Posts
    60

    Keypress....I know, it's been posted a million times...but..

    Hey,

    I'm sorry, I know this has been posted a million times, but I am just not understanding the concept of this keypress code.

    I am simply just trying to get a user to be able to press the button with the mouse, or use a keypress.

    The trouble I am having is that I can't figure out how to get the keypress to work for a letter such as "T" or "D" for example.

    I have this code...

    on (release) {
    _root.gotoAndStop("TOOLS MENU 01");
    }

    But I am at a loss past that.

    Thanks for your help!

    Jeff

  2. #2
    Uses MX 2004 Pro Quixx's Avatar
    Join Date
    Nov 2004
    Location
    U.S.
    Posts
    877
    I'm not sure if MX uses listeners, but if you're using MX 2004 you can (and probably should) use a Key listener instead of using the onEnterFrame method, which I'm showing you below.

    Code:
    stop();
    onEnterFrame = function () {
    	if (Key.isDown(84)) {
    		_root.gotoAndStop("TOOLS MENU 01");
    		delete this.onEnterFrame;
    	}
    };
    Just put the above code on the actions panel of the frame your button is on. Do not put the code on the button itself, because it won't work and you'll get an error message.

  3. #3
    Member
    Join Date
    Jan 2003
    Posts
    60
    Hey,

    Thanks that works perfectly. You suggested that I use a Key listener instead...is there a reason for that?

    I am trying to use the following code.'

    onClipEvent(keyDown){
    trace(Key.getCode());
    }

    But it gets this error.

    **Error** Symbol=KeyTrace, layer=Layer 1, frame=1:Line 1: Clip events are permitted only for movie clip instances
    onClipEvent(keyDown){

    Total ActionScript Errors: 1 Reported Errors: 1

  4. #4
    Uses MX 2004 Pro Quixx's Avatar
    Join Date
    Nov 2004
    Location
    U.S.
    Posts
    877
    Originally posted by crazylegsmurphy
    You suggested that I use a Key listener instead...is there a reason for that?
    Yes,

    I suggested you use a Key listener, because unlike an onEnterFrame event handler, the Key listener only checks the code within it when a button has been hit. The onEnterFrame event handler is checking to see if you are hitting a key every frame rotation. With alot of code within an onEnterFrame event handler, you can start to get some laggy results. So using a listener is ideal in some cases.

    If you're using MX 2004 you could use this listener example:

    Code:
    stop();
    var myListener:Object = new Object();
    myListener.onKeyDown = function() {
    	if (Key.isDown(84)) {
    		_root.gotoAndStop("TOOLS MENU 01");
    	}
    };
    Key.addListener(myListener);

  5. #5
    Member
    Join Date
    Jan 2003
    Posts
    60
    Ahhh,

    I see what you did...Awesome, thanks so much!!

  6. #6
    Uses MX 2004 Pro Quixx's Avatar
    Join Date
    Nov 2004
    Location
    U.S.
    Posts
    877
    No problem.

  7. #7
    Member
    Join Date
    Jan 2003
    Posts
    60
    Ok, so I got that all working, but now I have a new problem.

    I have a button (SCREEN) that when the key (E) is pressed it goes to frame lable (SETUP-SCREEN 01)

    That works perfect...but...when I get to that frame, the keypress' still work from the last frame.

    This is the code I have on the first frame.

    // SETUP MENU
    keyListener1 = new Object()
    keyListener1.onKeyDown = function() {
    if (Key.isDown(71)) {// G (PASON GAS)
    _root.gotoAndStop("SETUP MENU 01");
    }
    if (Key.isDown(83)) {// S (SHUTDOWN)
    _root.gotoAndStop("SETUP MENU 01");
    }
    if (Key.isDown(67)) {// C (COMM PORT 2)
    _root.gotoAndStop("SETUP MENU 01");
    }
    if (Key.isDown(79)) {// O (TRACE ORDER)
    _root.gotoAndStop("SETUP MENU 01");
    }
    if (Key.isDown(69)) {// E (SCREEN)
    _root.gotoAndStop("SETUP-SCREEN 01");
    }
    if (Key.isDown(80)) {// P (PULSES)
    _root.gotoAndStop("SETUP MENU 01");
    }
    if (Key.isDown(68)) {// D (DIAGNOSTICS)
    _root.gotoAndStop("SETUP MENU 01");
    }
    if (Key.isDown(87)) {// W (WELL INFO)
    _root.gotoAndStop("SETUP MENU 01");
    }
    if (Key.isDown(73)) {// I (MAIL STATS)
    _root.gotoAndStop("SETUP MENU 01");
    }
    if (Key.isDown(88)) {// X (EXIT)
    _root.gotoAndStop("MAIN SCREEN");
    }

    }
    Key.addListener(keyListener1)
    And this is the code for SETUP-SCREEN 01

    // SETUP MENU - SCREEN
    keyListener2 = new Object()
    keyListener2.onKeyDown = function() {

    if (Key.isDown(72)) {// H (HELP)
    _root.gotoAndStop("SETUP MENU 01");
    }
    if (Key.isDown(88)) {// X (EXIT)
    _root.gotoAndStop("MAIN SCREEN");
    }

    }
    Key.addListener(keyListener2)
    Anyone have any ideas?

    Thanks.

  8. #8
    Uses MX 2004 Pro Quixx's Avatar
    Join Date
    Nov 2004
    Location
    U.S.
    Posts
    877
    I didn't realize you were going to be doing something that elaborate, otherwise I would've suggested you use switch right from the start. Below is a conversion of your script above using switch to handle the button tests within a Key listener. Once a correct button has been hit, the listener handling the code will delete itselt and send the user to the correct frame. The listener will become enabled again once the user returns back to that frame.

    The code for the first frame:
    Code:
    stop();
    var listenerObj:Object = new Object();
    listenerObj.onKeyDown = function() {
    	switch (String.fromCharCode(Key.getAscii())) {
    	case "g" :
    	case "s" :
    	case "c" :
    	case "o" :
    	case "p" :
    	case "d" :
    	case "w" :
    	case "i" :
    		_root.gotoAndStop("SETUP MENU 01");
    		Key.removeListener(listenerObj);
    		break;
    	case "e" :
    		_root.gotoAndStop("SETUP-SCREEN 01");
    		Key.removeListener(listenerObj);
    		break;
    	case "x" :
    		_root.gotoAndStop("MAIN SCREEN");
    		Key.removeListener(listenerObj);
    		break;
    	default :
    		break;
    	}
    };
    Key.addListener(listenerObj);
    The code for the "SETUP-SCREEN 01" frame:
    Code:
    stop();
    var keyListener2:Object = new Object();
    keyListener2.onKeyDown = function() {
    	switch (String.fromCharCode(Key.getAscii())) {
    	case "h" :
    		_root.gotoAndStop("SETUP MENU 01");
    		Key.removeListener(keyListener2);
    		break;
    	case "x" :
    		_root.gotoAndStop("MAIN SCREEN");
    		Key.removeListener(keyListener2);
    		break;
    	default :
    		break;
    	}
    };
    Key.addListener(keyListener2);
    NOTE: If you wish the code to allow for uppercase letter hits (If the user has the "Caps Lock" key toggled, or shift down), you'll have to include those letters in the case. For example, if you'd like to allow both the lower case "x" and upper case "X", you need:
    Code:
    case "x" :
    case "X" :

  9. #9
    Uses MX 2004 Pro Quixx's Avatar
    Join Date
    Nov 2004
    Location
    U.S.
    Posts
    877

    Sorry for the double post...

    I forgot to attach an example file to that last post...

  10. #10
    Member
    Join Date
    Jan 2003
    Posts
    60
    SWEET! Thanks Quixx!

    It seems to have worked perfectly. I just wanted let you know what I had to change to get it to work with my movie.

    The Code I used.

    // SETUP MENU - SCREEN 01
    keyListener_SETUPScreen01 = new Object()
    keyListener_SETUPScreen01.onKeyDown = function() {
    switch (String.fromCharCode(Key.getAscii())) {
    case "h" :// H (HELP)
    _root.gotoAndStop("");
    Key.removeListener(keyListener_SETUPScreen01);
    break;
    case "x" :// X (EXIT)
    _root.gotoAndStop("MAIN SCREEN");
    Key.removeListener(keyListener_SETUPScreen01);
    break;
    default :
    break;
    }
    };
    Key.addListener(keyListener_SETUPScreen01);
    I had to replace the

    var listenerObj:Object = new Object();
    listenerObj.onKeyDown = function() {
    Because for some reason it wouldn't work with my flash movie.

    Thanks again!

    Jeff

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