将 jQuery .keydown() 限制为仅特定键 [英] Limit jQuery .keydown() to Only specific Keys

查看:40
本文介绍了将 jQuery .keydown() 限制为仅特定键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WordPress jQuery 为我的网站构建一些快捷方式.

I am using WordPress jQuery to build some shortcuts for my site.

它按预期工作,但在按下其他键时不会停止.

It works as intended, however it doesn't stop when other keys are pressed.

例如,如果我按下 f 它会执行必要的任务.但是如果我按 CTRL + f 那么它也会完成任务.

For example, if I press, f it performs the necessary task. But if I press CTRL + f then also it does the task.

事实上,我尝试过其他键,例如 fp 并且也有效.

Infact, I tried, other keys like f and p and that too worked.

我希望它只适用于特定的键.Incase 任何其他按键,它不应该运行.它应该在点击 f 时运行,而不是在 CTRL + f 上运行.

I want it to only work on the specific key. Incase of any other key press, it should not run. It should run on clicking f, but NOT run on CTRL + f.

我在 Windows 7 和 8 上的 Chrome 92.0.4515.159 和 Chrome Beta 版 94.0.4606.31 上对此进行了测试.

I tested this on Chrome 92.0.4515.159 and Chrome Beta Version 94.0.4606.31 on Windows 7 and 8.

    jQuery(window).keydown(function(e) {
        if( e.which === 27){
            jQuery('.SearchCreationsClose').click();
        }
        if(e.key == "f"){
            e.preventDefault();
            jQuery('.SearchCreationsIcon').click();
        }
    });

推荐答案

我看到了@diego 的回答,明白您想要实现快捷键,以便它们仅限于特定的组合键,额外的按键将停止该功能.

I saw the answer from @diego and understand that you want to implement shortcuts such that they are limited to a specific key combination, extra keypress will stop the function.

解决方案真的很好,但阅读评论有两个主要问题,1.) 当 window 失去焦点时它会停止,以及 2.) event.preventDefault不起作用

The solution was really good, but reading comments there are 2 main problems, 1.) It stops when the window goes out of focus, and 2.) event.preventDefault doesn't not work

我有一个解决方案,它可以在没有超时功能的情况下工作,从而解决 e.preventDefault 问题.我使用不同的方法来实现它.

I have a solution, that works without the timeout function, making it solve the e.preventDefault problem. And I use a different approach to make it happen.

    var MyKeys= 0;

    jQuery(window).focus(function(){
        MyKeys = 0;
    });

    jQuery(window).keyup(function(e){
        if(MyKeys > 0){
            MyKeys--
        }
        else {
            MyKeys = 0;
        }
    });

    jQuery(window).keydown(function(e){
        if (!e.repeat) {
            MyKeys++
        }
        if(e.which == 70 && MyKeys === 1){
            e.preventDefault();
            console.log('f is pressed')
        }
        if(e.which == 70 && (e.ctrlKey || e.metaKey) && MyKeys === 2){
            e.preventDefault();
            console.log('ctrl+f is pressed')
        }
    });

很确定它解决了防止默认问题..

Pretty sure that it solves the prevent default problem..

而且我认为它也可以解决 Alt + Tab 问题...(尚未测试但有信心)

And I think it will solve the Alt + Tab problem too... (Haven't tested but am confident)

它并不完美.只有一个小的限制,如果用户在按下某个键的情况下进入您的 Windows,他必须先抬起该键才能使用快捷方式.他不能在按住 CTRL 然后按 f 的情况下聚焦.

It isn't perfect. There is only a small limitation, if a user comes focuses into your Windows with a key already pressed, he would have to lift the key before being able to use the shortcut. He cants just focus in with CTRL pressed and then press f.

这篇关于将 jQuery .keydown() 限制为仅特定键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆