Javascript全球键盘处理,不听A-Z键? [英] Javascript Global Keyboard Handling, not hearing A-Z Keys?

查看:180
本文介绍了Javascript全球键盘处理,不听A-Z键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Javascript拦截键盘事件,因此我可以在关闭窗口执行CMD-W,而在Flash应用程序内部,所以浏览器无法使用它们。



那么,我可以听ALT,CTRL和CMD onKeyDown / onKeyPress事件,但我不能听其他任何东西...这里的代码,在来自Flex项目的index.html文件:

  
<脚本语言=JavaScripttype =text / javascript>
document.onkeydown = function(event){applicationKeyboardHandler(event)}
document.onkeypress = {applicationKeyboardHandler(event)}
函数applicationKeyboardHandler(event){
alert(Key Pressed)
}
< / script>



我想让它可以听任何键按,不只是ALT / CTRL / CMD。我错过了什么?

解决方案

像Tim,我想Flash / Flex正在吞噬关键事件。由于Alt等元键,他们不会在Flex中触发按键事件,并传递给JS。另一方面,某些手势(例如某些浏览器上的Ctrl + A)被阻止被Flash接收。我想,出于同样的原因(安全),这些也被阻止由JS来处理。哪些关键手势受到保护是高度依赖浏览器的。



可能浏览器不允许您处理CTRL-Q,以便用户可以始终关闭他的浏览器,即使有一些苹果网站打开。


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?

解决方案

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.

这篇关于Javascript全球键盘处理,不听A-Z键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆