如何确定哪个“+”键被按下?
问题描述:
当按下数字键盘侧的加号键时,我有一个要求将+键作为制表键复制的请求。如何确定哪个“+”键被按下?
看来上面的字母加号键在需要shift键,加号键,其中数字是在按键板配置都具有43
数字号码,我怎么能确定哪个+键被按下?
更新: 我用这个例子中,“https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_event_key_keycode”为指出以下事实并不是这样做的正确方法。
答
每个键的键码是不同的。数字键盘= 107
,顶行= 187
你可以用它来验证它自己的键盘上:
$('#text').on('keydown', function (e) {
$('label').text(e.which);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text" />
<label></label>
您还可以使用在现代浏览器的key
财产(IE9 + )来检测哪个键被输出,而不必担心使用哪个键来生成它:
$('#text').on('keydown', function (e) {
if (e.key == '+')
console.log('You typed a plus symbol!');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text" />
<label></label>
答
这两个键产生两个不同的键码。您可以测试正确的:
$(document).on("keydown", function(evt){
switch (evt.keyCode){
case 107:
console.log("You pressed '+' on the number pad");
break;
case 187:
console.log("You pressed SHIFT '+' on main keyboard");
break;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Just click once over here to give the "document" focus and then press the + key(s)
你可以告诉你使用的代码? – eaglei22
刚刚编辑它在 –
好吧谢谢。嗯,我使用这个版本: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_event_key_keycode 他们来了43 – eaglei22