如何确保浏览器中的所有键盘事件均由一个处理程序接收

发布于 2024-09-30 07:43:57 字数 825 浏览 4 评论 0原文

我遇到了有关键盘事件(例如 keydown 事件)的问题。该应用程序是完全使用 JavaScript 和 DOM 操作构建的单页 Web 应用程序。

所有键盘处理均由添加到文档中的单个处理程序处理。

document.addEventListener("keydown", handler, true);

然后,该处理程序对键盘事件进行解码,并将内部键代码委托给应用程序框架。

DOM 树仅由 DIV 元素和文本节点组成。

现在,当添加具有外部 HTML 文档的 iframe,并且它包含可以接收焦点的元素时,原始处理程序将被绕过。将关键事件流恢复到原始处理程序的唯一方法是将焦点设置到主文档。

因此,iframe 添加的新文档似乎绕过了原始文档中的捕获/冒泡事件链。

这已经在chrome中进行了测试以供参考,但实际应用程序运行在机顶盒中的嵌入式Web浏览器上,该Web浏览器是基于Webkit的。

机顶盒没有鼠标界面,因此唯一的输入是来自遥控器的按键事件。这给我们带来了一个问题,因为我们不想在使用 iframe 加载其他页面时失去对关键事件的控制。

使用事件处理程序进行 DOM 操作并检测何时添加 iframe 并将主键事件处理程序添加到新插入的文档是唯一的解决方案吗?或者是否有一种方法可以确保按键事件处理程序始终接收浏览器窗口内的所有按键事件。

编辑

我刚刚意识到事件处理程序可以添加到窗口对象中,我也会尝试一下。

编辑

将按键事件处理程序添加到窗口对象不会改变任何内容。

I have encountered a problem regarding keyboard events such as the keydown event. The application is a single page webapp built entirely using JavaScript and DOM manipulation.

All keyboard processing is handled by a single handler added to the document using.

document.addEventListener("keydown", handler, true);

That handler then decodes the keyboard event and delegates an internal keycode to the application framework.

The DOM tree consists only of DIV elements and text nodes.

Now when an iframe with an external HTML document is added, and it contains elements that can receive focus the original handler is bypassed. The only way to restore the key event flow to the original handler is to set focus to the main document.

So it seems like the new document added by the iframe bypasses the capture/bubble event chain from the original document.

This has been tested in chrome for reference, but the actual application runs on an embedded web browser in a set top box, the web browser is based on Webkit.

In the set top box there is no mouse interface so the only input is key events from the remote. This causes a problem for us since we do not want to loose control over the key events when we load other pages using an iframe.

Is the only solution to use an event handler for DOM manipulation and detect when iframes are added and add the main key event handler to the newly inserted document? Or is there a method to ensure that a key event handler always receives all key events within a browser window.

EDIT

I just realized that event handlers can be added to the window object, I will try that as well.

EDIT

Adding the key event handler to the window object did not change anything.

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

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

发布评论

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

评论(1

楠木可依 2024-10-07 07:43:57

通过使用以下解决方法解决:

    document.addEventListener("DOMNodeInserted", function (event) {
        if(event.type === "DOMNodeInserted") {
            if(event.srcElement.nodeName === "IFRAME") {
                event.srcElement.addEventListener("keypress", function(kevent) {
                    document.dispatchEvent(kevent);
                    }, true);
             }
         }
    },true);

这会在捕获阶段将关键事件从子 iframe 传播到主文档中。

Solved by using the following workaround:

    document.addEventListener("DOMNodeInserted", function (event) {
        if(event.type === "DOMNodeInserted") {
            if(event.srcElement.nodeName === "IFRAME") {
                event.srcElement.addEventListener("keypress", function(kevent) {
                    document.dispatchEvent(kevent);
                    }, true);
             }
         }
    },true);

This propagates key events from child iframes into the main document during the capture phase.

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