抑制键按非字符键? [英] Suppressing keyPress for non-character keys?

查看:57
本文介绍了抑制键按非字符键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以防止按下F1之类的键吗?

Is there any way to prevent a key like F1 from being pressed?

经过短暂搜索,我找到了这个网站:

After a short search, I found this website:

这样,可以抑制按下"a"之类的键(它不会放在文本框中),但是诸如"tab","F1"之类的键仍在起作用,即焦点确实发生了变化,并且我使用的是Google Chrome,Chrome帮助网站会弹出.

This way one can suppress keys like 'a' being pressed (it does not get put in the textbox), but keys like 'tab', 'F1' etc. are still working, i.e. the focus does change and, as I'm using Google Chrome, the Chrome help website does pop up.

我特别是在谈论Google Chrome;该解决方案也不必在其他浏览器中也可以使用.

I'm specifically talking about Google Chrome; the solution does not have to work in other browsers too.

这有可能吗?如果可以,怎么办?

Is this possible at all, and if so, how?

谢谢.

推荐答案

keypress.因此,浏览器可能不会在退格键, F1 ,向下键等上触发事件.

keypress is not necessarily triggered when the keypress is not a character. So the browser may not trigger an event on backspace, F1, the down key, etc.

尝试取消keydown上的事件:

element.addEventListener('keydown', function(e) {
    if (e.which === 112) { // F1 pressed
        e.preventDefault(); // cancel the event
    }
}

请注意,这将适用于Chrome和其他符合标准的浏览器,但不适用于Internet Explorer< 9.

Note that this will work in Chrome and other standards-compliant browsers, but not in Internet Explorer <9.

这篇关于抑制键按非字符键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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