JavaScript Keycode 46 是 DEL 功能键还是 (.) 句号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2859587/
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 Keycode 46 is DEL Function key or (.) period sign?
提问by JOBG
Im writing some logic in JavaScript using jquery, where i must check the input content against a REGEX pattern ex:
我使用 jquery 在 JavaScript 中编写一些逻辑,其中我必须根据 REGEX 模式检查输入内容,例如:
"^[a-zA-Z0-9_]*$" //Alpha-numeric and _
The logic is almost done, i just have a little problem filtering the function key DEL, my logic goes like this:
逻辑差不多完成了,我只是在过滤功能键 DEL 时遇到了一点问题,我的逻辑是这样的:
var FunctionsKey = new Array(8, 9, 13, 16, 35, 36, 37, 39, 46);
function keypressValidation(key) {
if (config.regexExp != null) {
if ($.inArray(key, FunctionsKey) != -1) {
return true;
}
else {
var keyChar = String.fromCharCode(key);
return RegexCheck(keyChar);
}
}
return true;
}
If the KeyCode is one of those in the array, i let it pass, if not i get the char and compare it against the REGEX. The problem is: in some Browsers the DEL and '.' (period sign) have the same key Code 46.
如果 KeyCode 是数组中的一个,我让它通过,如果不是,我得到字符并将它与 REGEX 进行比较。问题是:在某些浏览器中,DEL 和 '.' (句号)具有相同的密钥代码 46。
So is there a better logic to filter the function keys or must i write a condition for that case, maybe removing the 46 from the array and try to convert it to char and if is (.) let it go to the Regex function if not let it pass? The other question will be are there more shared Key Codes in some browsers?
那么是否有更好的逻辑来过滤功能键,或者我必须为这种情况写一个条件,也许从数组中删除 46 并尝试将其转换为字符,如果是 (.) 如果不是,则让它转到正则表达式函数让它过去?另一个问题是在某些浏览器中是否有更多的共享密钥代码?
EDIT: My suggested solution wont work because it doesn't matter which key the user pressed (DEL or period) i always get (.) as CHAR at least on OPERA and FF =(.
编辑:我建议的解决方案不起作用,因为用户按下哪个键(DEL 或句点)并不重要,我总是将 (.) 作为 CHAR 至少在 OPERA 和 FF =(。
采纳答案by Mark Schultheiss
110 is the decimal key code, 46 is the DEL key.
110是十进制键码,46是DEL键。
For some fun: put this in to see what you hit! EDIT: added a focused event
为了一些乐趣:把它放进去看看你击中了什么!编辑:添加了一个重点事件
/* handle special key press */
$(document).ready(function() {
function checkAKey(e) {
var shouldBubble = true;
switch (e.keyCode) {
// user pressed the Tab
case 9:
{
alert("Tab hit, no bubble");
shouldBubble = false;
break;
};
// user pressed the Enter
case 13:
{
alert("Enter");
break;
};
// user pressed the ESC
case 27:
{
alert("Escape");
break;
};
};
/* this propogates the jQuery event if true */
return shouldBubble;
};
$("*").keydown(function(e) {
return checkAKey(e);
});
});
OR
或者
$(document).ready(function() {
/* handle special key press */
function checkFieldKey(e, me) {
var shouldBubble = true;
switch (e.keyCode) {
// user pressed the Enter
case 13:
{
$(me).blur();
$("#somewhereElse").focus();
shouldBubble = false;
break;
};
};
/* this propogates the jQuery event if true */
return shouldBubble;
};
/* user pressed special keys while in Selector */
$("#myField").keydown(function(e) {
return checkFieldKey(e, $(this));
});
});
回答by Bhaumik
Decimal or dot key code on keypad is 190.. decimal on numpad is 110.
小键盘上的十进制或点键代码是 190.. 小键盘上的十进制是 110。
cheers..!!
干杯..!!
回答by vcoppolecchia
@Mark Schultheiss'answer is really good, I'll add some more to it: when you need to push the DELbutton on the keyboard outside the input element (that is when a text field loses focus) you have to intercept it. This can be done like so:
@Mark Schultheiss 的回答非常好,我会添加更多内容:当您需要DEL在输入元素外按下键盘上的按钮时(即文本字段失去焦点时),您必须拦截它。这可以像这样完成:
$("#selector-for-a-textbox, body").keydown(function(event){
if(event.keyCode==46){
// do something here
}
});
回答by muTheTechie
The difference is: On keypress "DELETE" key and "." key returns the keycode 46.
区别在于:在按键上按下“DELETE”键和“.”键。键返回键码 46。
So write the code on keyup or key down, then "DELETE" key works well.
所以在keyup或key down上写代码,然后“DELETE”键效果很好。
To check http://javascript.info/tutorial/keyboard-events
检查http://javascript.info/tutorial/keyboard-events
Wrong code
错误代码
window.addEventListener("keypress", dealWithKeyboard, false);
Right code:
正确的代码:
window.addEventListener("keydown", dealWithKeyboard, false);
Final code
最终代码
window.addEventListener("keydown", dealWithKeyboard, false);
function dealWithKeyboard(e) { // CHECK KEYPRESS EVENT
if(e.keyCode ==46){
delete_object();}
}
its working well for me.
它对我来说效果很好。
回答by deepeshb
if we want n digits before decimal and n digits after decimal we can simply use ,
如果我们想要小数点前 n 位数字和小数点后 n 位数字,我们可以简单地使用,
var validate = function(e) {
var beforedecimal=5;
var afterDecimal=4;
var t = e.value;
var n = t.includes(".");
if(n== true){
e.maxLength = 15;
e.value = (t.indexOf(".") >= 0) ? (t.substr(0, t.indexOf(".")) + t.substr(t.indexOf("."), afterDecimal)) : t;
}else if(n == false){
var a=t.length;
e.maxLength = beforedecimal;
if(a>=beforedecimal){
if(event.keyCode == 46){
e.maxLength = beforedecimal+1;
}
}
}
}
<input type="text" id="resultText" onkeypress="validate(this)" />

