jsound
asked on
Flash 8: Custom Class Listening to Key Events
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
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
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
I'll let you know what comes of it.
JB
ASKER
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(keyListene r);
}
private function keyHandler():Void {
trace("Handling key");
}
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(keyListene
}
private function keyHandler():Void {
trace("Handling key");
}
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 :).
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(keyListene
if(KeyPress) {
trace("Key pressed");
}
}
}
Good luck :)