JS MAC覆盖键盘快捷键
默认的MAC键盘快捷键在Safari中,我想覆盖它们。例如cmd+s
,cmd+n
let isMeta = false;
window.onkeydown = e => {
if(e.key == "Meta"){
isMeta = true;
}else if(e.key == 's' && isMeta){
e.preventDefault();
console.log('s');
}else if(e.key == 'n' && isMeta){
e.preventDefault();
console.log('n');
}
};
window.onkeyup = e => {
if(e.key == "Meta"){
isMeta = false;
}
};
测试结果:cmd+s
成功了,当我按快捷方式时,可以防止保存操作,并<代码> S 已记录。但是ctrl+n
将调用默认打开新选项卡,而n
未记录。
我想知道为什么会发生这种情况,一个人为什么起作用。
default Mac keyboard shortcut in safari, I want to overwrite them. such as cmd+s
, cmd+n
let isMeta = false;
window.onkeydown = e => {
if(e.key == "Meta"){
isMeta = true;
}else if(e.key == 's' && isMeta){
e.preventDefault();
console.log('s');
}else if(e.key == 'n' && isMeta){
e.preventDefault();
console.log('n');
}
};
window.onkeyup = e => {
if(e.key == "Meta"){
isMeta = false;
}
};
testing result: cmd+s
succeeded, when I press the shortcut, saving action is prevented and s
is logged. but ctrl+n
will call default opening new tab and n
is not logged.
I wonder why this happens, why one works but the other.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论