简体   繁体   中英

use of javascript to disable FF key presses for AS2 embed

For AS2 I need to allow the user to press "Alt + N" to go to the next page, but the problem is that in Windows whenever Alt is pushed it takes focus off the flash embed and the keylistener never receive it.

Researching what to do, it seems that the solution is to use Javascript to disable the default action of the ALT key in Firefox (the browser it needs to run in). I'm not sure if this is the right path, and not exactly sure how to do it.

Well, you'll need an external interface in the flash program; something like this:

function keyCodeReceptor( code ){
  switch ( code ) {
    case 67:
      // go to the next page
      break;
    // add any other keys you need to bind to "Alt+key" combination
    default:
      break;
  }
}

flash.external.ExternalInterface.addCallback( 'doKey', null, keyCodeReceptor );

Then you'll need something like the following in the HTML you're embedding the object in:

(function(){
  // Use the name or index of your embed here
  var flash = document.embeds[0];
  window.addEventListener( 'keydown', function( event ){
    if( event.altKey && event.keyCode == 67 ){
      event.preventDefault();
      event.preventCapture();
      event.preventBubble();
      flash.doKey(event.keyCode);
    }
  });
})();

Also, make sure that the embed has the allowScriptAccess attribute set to "always" .

I only tested this is Firefox (latest, Mac and Windows), so I don't know at all if it works in other browsers. Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM