Send keyboard and mouse events to Flash Movie

2019-05-16 21:48发布

I'm looking for a way to "inject" some events into a flash movie running on a browser. I know about ActionScript's ExternalInterface.addCallback function, however I'm trying to achieve this with any "random" flash from the web. Eg. send a "SPACE" keyboard event to a youtube video.

3条回答
趁早两清
2楼-- · 2019-05-16 22:23

Unless Flash is configured to listen for the events you're trying to send it, my guess is that what you want to do isn't possible. But, as my wife will surely tell you, I'm wrong a lot.

查看更多
孤傲高冷的网名
3楼-- · 2019-05-16 22:38

I tried creating a (WebKit) DOM event and sending it to the Flash OBJECT element. I also tried all of the child nodes of that element to be sure.

function fireEvent(target) {
   var evt = document.createEvent("Events");
   evt.initEvent("keypress", true, true);
   evt.view = window;
   evt.altKey = false;
   evt.ctrlKey = false;
   evt.shiftKey = false;
   evt.metaKey = false;
   evt.keyCode = 32;
   evt.charCode = ' ';
   target.dispatchEvent(evt);
}

It didn't work. My answer is: no. You can't do this, at least not from Javascript.

If you really want to do it, I think you'll have to create an application with an embedded browser control (WebKit, Gecko, etc.). Then you could call the plugin API directly to synthesize the user input events. This is how Boxee appears to works, for example.

查看更多
成全新的幸福
4楼-- · 2019-05-16 22:48

You can't do this to any random swf on any website because of security limitations. You need to have access to the DOM, which can only be done by Javascript Injections, unless you are the one with the swf embedded in the site, with allowScriptAccess="always" in the html template. MySpace, for example, allows you to add any random swf to the html page, but you can't do that javascript stuff because they force allowScriptAccess to always be false (among other things) to prevent any javascript/dom access.

If this is just for you to make your own life easier while browsing, you can use Firebug for Firefox. Check out this JQuerify Bookmarklet and Video, Hacking Digg with Firebug and JQuery. It shows you how to dynamically modify the webpage your on using javascript, and save those commands as shortcuts! Super cool.

So you could save a JQuerify command like:

$("#youtube_player").play();

...or whatever the api is for starting a youtube video in their html page, if that's even possible.

Hope that helps, Lance

查看更多
登录 后发表回答