I’m using ActionScript to listen for key presses and route to a method to handle them. It works fine in Flash Player Debugger 10.1, but does not work with the SWF in a browser.
I’ve tried it with all sorts of keys: letters, numbers, etc. But I can’t get it to work at all in the browser. I’m using Safari 5.1 and Firefox 3.6.8 on the Mac.
Here’s my relevant code:
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPressed);
public function keyPressed(k:KeyboardEvent):void
{
switch(k.keyCode)
{
case(32):
// spacebar
demoTimeline.pause();
break;
case(leftArrow):
// left arrow - 188
demoTimeline.reverse();
break;
case(rightArrow):
// right arrow - 190
demoTimeline.play();
break;
case(191):
// question mark - 191
demoTimeline.restart();
break;
}
}
The SWF object needs to have focus to receive the keyboard events. So, if you click on the SWF when it’s displayed in a browser, it should work.
For security reasons, you cannot receive keyboard input when the focus is not on the Flash object (even if it takes the whole browser window).