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

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

问题描述

我正在尝试使用 Javascript 来拦截键盘事件,因此我可以在 Flash 应用程序中为关闭窗口"和诸如此类的东西执行 CMD-W,因此浏览器无法使用它们.

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.

好吧,我能够监听 ALT、CTRL 和 CMD onKeyDown/onKeyPress 事件,但我无法监听其他任何事情……这是代码,位于 Flex 项目的 index.html 文件中:

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>

我想让它可以听任何按键,而不仅仅是alt/ctrl/cmd.我错过了什么?

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

推荐答案

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

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.

可能浏览器不允许您处理 CTRL-Q 以便用户可以总是关闭他的浏览器,即使打开了一些恶意网站.

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天全站免登陆