Javascript 全局键盘处理,没有听到 AZ 键?

发布于 2024-08-11 00:01:56 字数 601 浏览 4 评论 0原文

我正在尝试使用 Javascript 来拦截键盘事件,这样我就可以在 Flash 应用程序中执行 CMD-W 来执行“关闭窗口”之类的操作,这样浏览器就无法使用它们。

嗯,我能够监听 ALT、CTRL 和 CMD onKeyDown/onKeyPress 事件,但我无法监听其他任何事件...以下是 Flex 项目的 index.html 文件中的代码:


<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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖伴 2024-08-18 00:01:56

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

浏览器可能不允许您处理 CTRL-Q,以便用户始终可以关闭浏览器,即使打开了一些恶意站点。

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.

卖梦商人 2024-08-18 00:01:56

我想象 Flash 影片正在处理关键事件并防止它们在文档树中传播。为什么不在 Flash 本身中处理事件呢?

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?

桃气十足 2024-08-18 00:01:56

你确定Flash没有阻止它吗?您是否尝试过在没有 Flash 的页面上运行代码?

您应该尝试将事件附加到窗口而不是文档

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文