22 Replies Latest reply on Jun 23, 2007 6:24 AM by Greg Dove

    Use Any key to activate a button

    jake0037
      HI,

      I was wondering if there is anyway to activate a button by hitting any key?

      Currently I have this code but it doesnt seem to work,

      var keyListener:Object = new Object();
      Key.addListener(keyListener);
      keyListener.onKeyDown = function() {
      processInput();
      };

      my_btn.onPress = processInput;
      function processInput() {
      gotoAndStop("Scene 2", 1);
      }

      Any help will be muchly appreciated
        • 1. Re: Use Any key to activate a button
          Greg Dove Level 4
          You didn't clarify exactly what was not working...

          The keylistener should be working- it fires processInput... It works for me. In the Flash IDE you might have to disable keyboard shortcuts in the menu to see it working with all keys... and in a browser the flash movie needs to have the focus to respond to the keypresses.
          The button also works... but it may not do what you want, because the 'this' reference is to the button. I assume that's the nature of your problem... if so try this (you will see from the trace output what's happening between the keypress and the button press - using the timeline variable should fix it):


          • 2. Re: Use Any key to activate a button
            jake0037 Level 1
            HI,

            I cant get the button to activate. I want the button the change scene when any key on the keyboard is hit.

            I have used this code previously for a specific key but is there anyway to get it to work by hitting any key?

            If it helps i'm using MX 2004
            • 3. Re: Use Any key to activate a button
              Greg Dove Level 4
              Did you put a trace inside processInput to see if the keyListener calls it at all?

              trace('processed input');

              It works fine for me... although I haven't actually created a "Scene 2" and tested that part. I changed my publish settings to flash player 7/as2 - which is the same as MX2004 and it works fine.
              Make sure you don't have your publish settings set for as1. It won't work if that's the case.



              • 4. Re: Use Any key to activate a button
                jake0037 Level 1
                Hi GWD

                This is what i get from the output of the trace but the button doesnt go to scene 2.

                processed input
                what do I think "this" is :_level0
                if I used variable timeline its :_level0

                Is this what i should be getting?
                • 5. Use Any key to activate a button
                  Greg Dove Level 4
                  EDIT : IGNORE THIS POST
                  Ahh... so scene 2 is inside your button.... that explains it. Its working... but what is happening is that inside your processInput function your gotoAndStop is looking in different timelines. You can see the difference in the traces between when the keyListener calls it and when your button onPress handler calls it.

                  I had assumed you wanted to move the button's parent timeline on... but it was the button's timeline that you wanted to change. (I just re-read your earlier post - you did say that but I missed it, sorry)


                  try changing this:
                  keyListener.onKeyDown = function() {
                  processInput();
                  };

                  to this:
                  keyListener.onKeyDown = function() {
                  my_btn.onPress();
                  };

                  and you can get rid of the timeline variable.

                  inside processInput:

                  make it :
                  this.gotoAndStop("Scene 2", 1)
                  • 6. Re: Use Any key to activate a button
                    jake0037 Level 1
                    Hi GWD,

                    I have made the changes you suggest still nothing, maybe i've done something wrong?

                    • 7. Use Any key to activate a button
                      Greg Dove Level 4
                      EDIT: IGNORE THIS POST
                      Thought a little more about this. Its probably easier and more intuitive to leave everything as you had it originally and just change:

                      gotoAndStop("Scene 2", 1);

                      to:

                      my_btn.gotoAndStop("Scene 2", 1);
                      • 8. Use Any key to activate a button
                        Greg Dove Level 4
                        Sorry I don't use scenes... can you have a scene inside your button? Are you trying to move the playhead inside your button? I can't understand that part.
                        • 9. Re: Use Any key to activate a button
                          jake0037 Level 1
                          The scene isnt in the button its in the whole project, if that makes sense
                          • 10. Re: Use Any key to activate a button
                            Greg Dove Level 4
                            I just checked livedocs... it can't be inside your button

                            gotoAndStop([scene:String], frame:Object) : Void

                            quote:


                            Sends the playhead to the specified frame in a scene and stops it. If no scene is specified, the playhead is sent to the frame in the current scene.You can use the scene parameter only on the root Timeline, not within Timelines for movie clips or other objects in the document.


                            • 11. Re: Use Any key to activate a button
                              jake0037 Level 1
                              Yeah its not in the button, is there someway i can attach the file so we can all see what i'm trying to do?
                              • 12. Re: Use Any key to activate a button
                                Greg Dove Level 4
                                Sorry I was a little confused with what you were trying to do.

                                Well I just tested it again. Using your original code it works for me publishing to flash player 7.

                                Did you check your publish settings?:
                                It should say actionscript 2.0 for the actionscript version on the flash tab.

                                • 13. Re: Use Any key to activate a button
                                  Greg Dove Level 4
                                  Not here - you can't attach stuff to the forum, you can only post links. Do you have somewhere you can put it online... some free hosting space somewhere for example. (mediaMax, esnips, googlepages etc)
                                  • 14. Use Any key to activate a button
                                    jake0037 Level 1
                                    LINK REMOVED
                                    • 15. Re: Use Any key to activate a button
                                      Greg Dove Level 4
                                      I just tried it and using your original code... it works.

                                      Pressing a key I see the circle in scene2
                                      typing a single letter in the text field I see the circle
                                      clicking the button I see the circle.

                                      I'm using flash CS3, but the publish settings are for flash player 7/as2 ... so it should in theory be the same as mx2004. I can't figure it out.
                                      • 16. Re: Use Any key to activate a button
                                        Greg Dove Level 4
                                        It was working in the IDE in test movie mode. But wasn't in the html page. I've got to go for an hour or so but I'll take another look when I get back.
                                        • 17. Re: Use Any key to activate a button
                                          Greg Dove Level 4
                                          OK... well for me to get it to work properly in html I had to move the code to the second frame, leaving frame one blank. It wasn't obeying the stop(); command. Don't know why !

                                          And the rest is working too. But because the flash doesn't automatically get the focus inside a html page you need to click on the flash area once to give it the focus (e.g. so if can listen to onKeyDown). Is this the problem that you're experiencing?
                                          In html page, key events don't work until flash has focus.

                                          If so, then this is old and looks like IE only... but it might help as a starting point. I don't know what the cross-browser solution is.
                                          http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_15586&sliceId=1
                                          • 18. Use Any key to activate a button
                                            jake0037 Level 1
                                            Uhm, ok, but when i was running this just in the flash player why doesnt it work?

                                            Not even when i click the button does it work, i just dont understand why it doesnt work
                                            • 19. Re: Use Any key to activate a button
                                              Greg Dove Level 4
                                              I wish I could tell you. It works for me...

                                              I don't think this will make a difference (it didn't for me) but its the last thing I can think of.. set up the listener before adding it.

                                              stop();
                                              var keyListener:Object = new Object();
                                              keyListener.onKeyDown = function() {
                                              processInput();
                                              };
                                              Key.addListener(keyListener);


                                              function processInput() {
                                              gotoAndStop("Scene 2", 1);
                                              }
                                              my_btn.onPress = processInput;

                                              Like I said.. this made no difference for me...so its a long shot.
                                              Beyond that perhaps others will know....(and sorry but I'm stuck at that point)


                                              • 20. Use Any key to activate a button
                                                jake0037 Level 1
                                                Thanks for all your help, i just tried that long shot and it works.

                                                Thanks GWD for all your help thanks heaps
                                                • 21. Re: Use Any key to activate a button
                                                  Greg Dove Level 4
                                                  Don't give up.. Someone else may well know. Please don't assume that because I try to answer things I'm an expert -particularly for timeline based movement etc - I tend to do everything on the one frame.
                                                  But there are others here who are experts!

                                                  The issue for me here was that I couldn't replicate your problem with your original code (when I put it back inside the fla you posted). So perhaps its a player version issue rather than a swf version issue. I'm using CS3 so its player 9 even though I'm publishing the swf version as 7. You could try it perhaps using player 8 or 9. And I did encounter that weird thing with the stop() command not working on the first frame.

                                                  Anyhow good luck with finding the answer.
                                                  • 22. Re: Use Any key to activate a button
                                                    Greg Dove Level 4
                                                    Oops. Just saw your answer. Well I'm pleased it works!