使用 preventDefault 覆盖打开新选项卡
Using preventDefault to override opening new tab
我正在尝试为键盘输入创建一个事件处理程序,同时专注于网页的某个元素。
我试图检测的两个快捷方式是 Ctrl+Shft+T
和 Ctrl+T
。您可能知道在 Firefox 和 Chrome 中,这些是 "reopen closed tab" 和 "new tab" 的快捷方式。我有以下测试代码:
<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
被阻止)。我认为这可能与 Ctrl
在 T
之前被推送有关,但 Shft+Ctrl+T
也被正确覆盖。
我也尝试观看 keypress
事件,但也没有用。
有没有办法抑制打开新标签页的行为,或者我应该建议使用不同的快捷方式?
我在 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
或我所知道的任何方式覆盖受保护的键盘输入。
我正在尝试为键盘输入创建一个事件处理程序,同时专注于网页的某个元素。
我试图检测的两个快捷方式是 Ctrl+Shft+T
和 Ctrl+T
。您可能知道在 Firefox 和 Chrome 中,这些是 "reopen closed tab" 和 "new tab" 的快捷方式。我有以下测试代码:
<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
被阻止)。我认为这可能与 Ctrl
在 T
之前被推送有关,但 Shft+Ctrl+T
也被正确覆盖。
我也尝试观看 keypress
事件,但也没有用。
有没有办法抑制打开新标签页的行为,或者我应该建议使用不同的快捷方式?
我在 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
或我所知道的任何方式覆盖受保护的键盘输入。