Solved

Flash 8: Custom Class Listening to Key Events

Posted on 2007-03-26
6
188 Views
Last Modified: 2010-04-03
How can I listen to root key events from inside a custom class I created?

Details:
I created a custom class that handles audio playback of MP3 files. I need to be able to handle key events from outside the class in that class. How can I create a key event handler inside the class that listens and reacts to keyboard events on _root?

Thanks,

JB
0
Comment
Question by:jsound
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 18

Expert Comment

by:Antonio Estrada
ID: 18794848
Hello JB, you can use them just like you'd normally use them just by adding a key listener. Something like this:

class myClass {
      public function myFunc():Void {
            var KeyPress:Boolean;
            var keyListener:Object = new Object();
            
            keyListener.onKeyDown = function() {
                  KeyPress = true;
            }
            keyListener.onKeyUp = function() {
                     KeyPress = false;
            }
            Key.addListener(keyListener);
             
               if(KeyPress) {
                    trace("Key pressed");
               }
       }
}

Good luck :)
0
 

Author Comment

by:jsound
ID: 18794971
Yeah, I did something similar, which didn't work either. Has got to do with something not passing the key press event properly. I just simply don't get any results out of the code (yours or mine).

Since the Key class is a top-level class, there shouldn't be anything else I need to do add the listener to the Key class.

I'm doing this within a Flash Slide Presentation project, whereby the custom class is handling the audio playback, depending on the keys that are pressed. The keys need to respond globally, in other words, not based on the current slide.

I create a key handler function in my class, but it didn't work, thus the question in case I overlooked something.

Any further words of wisdom you may have?

Thanks,

JB
0
 
LVL 18

Accepted Solution

by:
Antonio Estrada earned 500 total points
ID: 18794977
Oh, forgot about something important, it's supposed to be within an event (onRelease, onDrag, onEtc) in order to work correctly. So ignore my post above, because I forgot those lines, instead check this one:

class myClass {
      
        function myClass() {
              trace("instance created");  
        }
        
      public function myFunc(X:MovieClip):Void {
            var KeyPress:Boolean;
            var keyListener:Object = new Object();

          keyListener.onKeyDown = function() {
                  KeyPress = true;
            }
            keyListener.onKeyUp = function() {
                     KeyPress = false;
            }
            Key.addListener(keyListener);
           
                  X.onRollOver = function() {            
                        if(KeyPress) {
                               trace("Key pressed");
                        }
                  }
       }
}

Main timeline's code:

import myClass;
var Z = new myClass();
Z.myFunc(myMovieClip);

So anytime you are doing a rollOver on "myMovieClip", we can retrieve the key value. This works like a work-around since you can add a MC of the same width/height of your movie and always retrieve key presses.

Good luck!
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:jsound
ID: 18795003
Ah, I'll give that one a try. What I actually did was to add an empty movie clip to the _root and then add an onKeyDown function to the movie clip. Didn't work, so I will try your solution.

I'll let you know what comes of it.

JB
0
 

Author Comment

by:jsound
ID: 18797217
That did it (with some modifications to fit my scenario). Thanks.

It turned out that it was a problem where the function for the key listener was called.

All that was needed was to call the function from the init statement for the class to add the key listener to the class. Again, this is for my specific setup where the keys need to respond globally and not for a specific movie clip (target).

Here is the basic code that did it for me:

      public function addKeyListener():Void {
          var keyListener:Object = new Object();
          keyListener.onKeyDown = keyHandler;
          Key.addListener(keyListener);
      }
      
      private function keyHandler():Void {
           trace("Handling key");
      }
      
0
 
LVL 18

Expert Comment

by:Antonio Estrada
ID: 18797823
Ah true, you were talking about the whole movie. Well it's pretty nice you got it, and I'm glad I was able to help :).
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question