javascript 减法(-)键码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1898129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript subtract(-) keycode
提问by kmunky
ok, i need my code to check if minus/subtract/- was pressed, if it was pressed i want an alert box to pop. i tried with both 109and 189key codes but i still don't get the desired result. although i press "-"i don't get that alert box
好的,我需要我的代码来检查减/减/-是否被按下,如果被按下,我想要一个警告框弹出。我尝试了两个109和189关键代码,但我仍然没有得到想要的结果。虽然我按"-"我没有收到那个警告框
回答by Dave Ward
回答by user5943894
1.event.keyCode = 109 is '-' on numpad
1.event.keyCode = 109 是数字键盘上的“-”
2.event.keyCode = 189 is'-' in alphabate keybord key on chrome
2.event.keyCode = 189 is'-' in alphabate keybord key on chrome
3.event.keyCode= 173 is '-' in alphabate keybord key on firefox & on chrome 173 keycord is Mute On|Off
3.event.keyCode= 173 是 '-' 在 alphabate keybord 键上 firefox 和 chrome 173 键绳是静音开|关
回答by Tim Down
Don't do this in a keydownevent handler - you put yourself at the mercy of different browsers' ideas about key codes and potential variation between key codes for different keyboard types. Do it in a keypressevent and then you can get the character code instead, which is what you actually want.
不要在keydown事件处理程序中执行此操作- 您让自己受制于不同浏览器关于键代码的想法以及不同键盘类型的键代码之间的潜在变化。在一个keypress事件中做,然后你可以得到字符代码,这就是你真正想要的。
document.onkeypress = function(evt) {
evt = evt || window.event;
var charCode = evt.which || evt.keyCode;
var charStr = String.fromCharCode(charCode);
if (charStr == "-") {
alert("Minus!");
}
};
All the information you could ever need about JavaScript key events: http://unixpapa.com/js/key.html
您可能需要的有关 JavaScript 关键事件的所有信息:http: //unixpapa.com/js/key.html
回答by Rubens Farias
You can discover keyCodes this way:
您可以通过以下方式发现 keyCode:
回答by Matt
Post some code. This works for me:
贴一些代码。这对我有用:
document.onkeydown = function (e) {
if (e.keyCode === 109 || e.keyCode === 189) {
alert('minus sign pressed');
}
};
Full list of key codes here: http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx
完整的关键代码列表:http: //www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx
回答by Jeff Fairley
People are moving away from event.keyCodeand event.charCodedue to inconsistent implementations across browsers. Instead, use event.code. This is a stringfield.
人们正在远离event.keyCode和event.charCode跨浏览器由于不一致的实现。相反,使用event.code. 这是一个string领域。
Most browsers support Array.includes, so I tend to do the following:
大多数浏览器都支持Array.includes,因此我倾向于执行以下操作:
if (['-', 'Minus', 'NumpadSubtract'].includes(e.code)) {
// do something
}
回答by johnnyArt
I hope this works for you, It detects users pressed keys and if it's the one your looking for it displays the alert, you may change the actual key to be any other key.
我希望这对您有用,它会检测用户按下的键,如果它是您要查找的键,它会显示警报,您可以将实际键更改为任何其他键。
function detectSubstract(e)
{
var evtobj=window.event? event : e //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode
var actualkey=String.fromCharCode(unicode)
if (actualkey=="-")
{
alert('You pressed the minus key')
}
}
document.onkeypress=detectSubstract

