Skip to content

Instantly share code, notes, and snippets.

@josephj
Last active December 14, 2015 10:49
Show Gist options
  • Save josephj/5074587 to your computer and use it in GitHub Desktop.
Save josephj/5074587 to your computer and use it in GitHub Desktop.
SWF swallows any JavaScript key events while it's focused. This demo illustrates how to make key events always work while SWF exists on page.
<!DOCTYPE html>
<html>
<head>
<title>SWF & JavaScript Key Event</title>
<style>
#focusable-link {
position: absolute;
left: -1000em;
top: -1000em;
}
#logger {
font-size: 11px;
font-family: Verdana;
position: absolute;
left: 450px;
top: 100px;
}
</style>
</head>
<body>
<h1>SWF and JavaScript Key Event</h1>
<div id="demo"></div>
<ul id="logger"></ul>
<script src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
<script>
var attrs = {},
linkEl,
params = {};
// Uses SWFObject Renders Player.
// It makes sure you won't have a duplicated ID.
attrs.id = "player"; // Defines player's ID.
params.allowScriptAccess = "always"; // Needed to allow player to call functions.
swfobject.embedSWF(
"https://www.youtube.com/v/Zhawgd0REhA?enablejsapi=1&playerapiid=ytplayer&version=3",
"demo", "425", "356", "8", null, null, params, attrs
);
// Injects an invisible link as a focus target.
linkEl = document.createElement("a") ;
linkEl.href = "javascript:void(0);";
linkEl.setAttribute("id", "focusable-link");
document.body.appendChild(linkEl);
// Shows all key events.
document.onkeyup = function (e) {
e = e || window.event;
var item = document.createElement("li");
item.innerHTML = "keyup event, keyCode = " + e.keyCode;
document.getElementById("logger").appendChild(item)
};
// Always makes sure user can't focus on the SWF.
setTimeout(function () {
if (document.getElementById("player") === document.activeElement) {
document.getElementById("focusable-link").focus();
}
setTimeout(arguments.callee, 100);
}, 100);
</script>
</body>
</html>
@josephj
Copy link
Author

josephj commented Mar 4, 2013

It also causes keyboard shortcuts fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment