Flash: KeyboardEvent keycodes work in Flash Player, but not the browser

Flash: KeyboardEvent keycodes work in Flash Player, but not the browser,flash,firefox,safari,keyboard,keycode,Flash,Firefox,Safari,Keyboard,Keycode,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:Keyboar

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;
        }
    }

#1

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).


#2

Could you post the HTML/JS that you use to embed the SWF in the page?

#3

I'm just publishing the HTML via Flash CS5's settings. I'm not custom writing anything or using SWFObject on my own, or anything like that.

#4

Interesting: clicking the SWF doesn't make it work in Safari, and only partially in Firefox (the arrow keys work, but not the spacebar).