使用preventDefault覆盖打开新标签页

问题描述 投票:1回答:1

我正在尝试为键盘输入创建事件处理程序,同时重点放在网页的某个元素上。

我尝试检测的两个快捷方式是Ctrl+Shft+TCtrl+T。您可能已经知道,在Firefox和Chrome中,这些是“重新打开关闭的标签”和“新标签”的快捷方式。我有以下测试代码:

<script>
        var i = document.createElement("input");

        i.addEventListener('keydown', function(event) {
            event.preventDefault();
        });

        document.body.appendChild(i);
    </script>

[在Firefox和Chrome中,preventDefault()适用于Ctrl+Shft+T,但不适用于Ctrl+T(编辑:Ctrl-Shft-T似乎在Chrome中均不起作用,但某些快捷方式如Ctrl+J被阻止)。我以为可能与CtrlT之前被按下有关,但是Shft+Ctrl+T也被正确覆盖。

我也尝试观看keypress事件,但这也不起作用。

是否有办法禁止打开新标签页行为,还是我应该提出其他快捷方式?

javascript html event-handling keyboard-events
1个回答
0
投票

我在Chromium IRC中询问,并被告知某些键盘快捷键受浏览器保护,不能被javascript覆盖。

    [16:50:44] <mimi> hi, would anyone know why certain keyboard shortcuts can be overriden in javascript using preventDefault(), but others not? For example, I can prevent it from opening downloads with Ctrl+J but I can't prevent it from opening a new tab with Ctrl+T
    [16:53:09] <jbroman> mimi: I couldn't tell you the exact logic for individual shortcuts, but browsers tend to protect certain shortcuts so that users can still do things like close tabs even if an application tries to prevent it, and so that the user isn't surprised by not being able to control the browser.
    [16:53:35] <jbroman> I suspect, therefore, that it's not a bug but an intentional UI choice.
    [16:54:16] <mimi> that is quite likely, I observed the same behavior with Firefox, except some shortcuts behave differently of course
    [16:55:14] <ellyjones> there is no actual central logic to that choice
    [16:55:23] <ellyjones> we decided case-by-case which shortcuts should/shouldn't be override-able
    [16:55:39] <mimi> is there a list available anywhere?
    [17:00:12] <jbroman> not that I'm aware of, sorry
    [17:00:29] <ellyjones> unfortunately I don't think so
    [17:00:52] <mimi> well thanks for clearing it up at least
    [17:00:53] <ellyjones> the logic for whether something is high priority or not is kinda scattered around the codebase, and may depend on the platform (eg, on Mac I think you can grab ctrl-w, because cmd-w is close tab there)
    [17:02:13] <ellyjones> one of my team's planned projects for this year is to bring some sort of order to this specific piece of chaos
    [17:05:09] <jbroman> mimi: one such place appears to be https://source.chromium.org/chromium/chromium/src/+/master:chrome/browser/ui/browser_command_controller.cc;l=264;drc=a393fe54c419b9583dc37d1d0d6df8e5e6b0bf37;bpv=0;bpt=1 but I don't know this code very well
    [17:05:22] <jbroman> (and even there you have to map back from those commands and other logic to the corresponding shortcuts)
    [17:05:27] <mimi> that's gonna be well appreciated, because I could not find documentation on the subject anywhere for either chromium or firefox, and it would be a good help for webapp design
    [17:05:33] <ellyjones> jbroman: that is one of the places, yeah
    [17:05:45] <ellyjones> also any place you see AcceleratorManager::kPriorityHigh

Available here and used with consent of the people involved in the discussion

可以公平地假设Firefox中的行为类似。我还没有找到任何一个浏览器的受保护键盘命令的正式列表。

要回答这个问题:不,您不能用preventDefault或我知道的任何方式覆盖受保护的键盘输入。

© www.soinside.com 2019 - 2024. All rights reserved.