Javascript Global Keyboard Handling, not hearing A-Z Keys?

瘦欲@ 提交于 2019-12-25 01:46:13

问题


I am trying to use Javascript to intercept keyboard events, so I can do CMD-W for "close-window" and whatnot, inside a Flash application, so the Browser doesn't get to use them.

Well, I am able to listen for ALT, CTRL, and CMD onKeyDown/onKeyPress events, but I am not able to listen to anything else... Here is the code, in the index.html file from a Flex Project:


<script language="JavaScript" type="text/javascript">
document.onkeydown = function(event) {applicationKeyboardHandler(event)}
document.onkeypress = function(event) {applicationKeyboardHandler(event)}
function applicationKeyboardHandler(event) {
    alert("Key Pressed")
}
</script>

I would like to make it so it could listen to any key press, not just alt/ctrl/cmd. What am I missing?


回答1:


Like Tim, I guess Flash/Flex is swallowing the key events. Since Alt etc are Meta Keys, they don't fire a keypress event in Flex and are passed to JS. On the other hand, certain gestures (e.g. Ctrl+A on some browsers) are prevented to be ever received by Flash. I imagine, that for the same reason (security) these are also prevented from beeing handled by JS. Which key gestures are protected is highly browser dependend.

Probably the browser won't allow you to handle CTRL-Q so that the user can always close his browser, even when having some malicous sites open.




回答2:


I imagine the Flash movie is handling the key events and preventing them propagating up the document tree. Why not handle the events in the Flash itself?




回答3:


Are you sure Flash is not blocking it? Have you tried to run your code on a page without Flash on it?

You should try attaching the events to window and not document



来源:https://stackoverflow.com/questions/1713447/javascript-global-keyboard-handling-not-hearing-a-z-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!