JS MAC覆盖键盘快捷键

发布于 2025-02-11 20:44:51 字数 756 浏览 0 评论 0原文

默认的MAC键盘快捷键在Safari中,我想覆盖它们。例如cmd+scmd+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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文