Javascript 获取按下的键的键值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12689995/
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
Get key value of a key pressed
提问by bl0b
I don't find how to get the value of a key pressed. I currently have
我没有找到如何获取按下的键的值。我目前有
$('#info_price').bind('keydown',function(evt){
alert(evt.keyCode);
but it return '49' when I press on 1 instead of returning '1'.
但是当我按 1 而不是返回“1”时它返回“49”。
Edit: I'm aware that Ascii code of key '1'.
编辑:我知道键 '1' 的 Ascii 代码。
The final goal is to allow people to only write digit into the input. So i want to detect the non digit and not display them.
最终目标是允许人们只在输入中写入数字。所以我想检测非数字而不显示它们。
回答by Leri
As it's told in comment it's ASCII
code. To get it as character you can do:
正如评论中所说,它是ASCII
代码。要将其作为角色,您可以执行以下操作:
alert(String.fromCharCode(evt.keyCode));
回答by tarasikarius
For those who google it now, like I am
对于那些现在用谷歌搜索它的人,就像我一样
$('input').on('keydown', function(e) {
console.log(e.key);
});?
回答by Salketer
Here is a fully done code for you to work with (not mine, but I used it):
这是一个完整的代码供您使用(不是我的,但我使用了它):
http://www.selfcontained.us/2009/09/16/getting-keycode-values-in-javascript/
http://www.selfcontained.us/2009/09/16/getting-keycode-values-in-javascript/
keycode = {
getKeyCode : function(e) {
var keycode = null;
if(window.event) {
keycode = window.event.keyCode;
}else if(e) {
keycode = e.which;
}
return keycode;
},
getKeyCodeValue : function(keyCode, shiftKey) {
shiftKey = shiftKey || false;
var value = null;
if(shiftKey === true) {
value = this.modifiedByShift[keyCode];
}else {
value = this.keyCodeMap[keyCode];
}
return value;
},
getValueByEvent : function(e) {
return this.getKeyCodeValue(this.getKeyCode(e), e.shiftKey);
},
keyCodeMap : {
8:"backspace", 9:"tab", 13:"return", 16:"shift", 17:"ctrl", 18:"alt", 19:"pausebreak", 20:"capslock", 27:"escape", 32:" ", 33:"pageup",
34:"pagedown", 35:"end", 36:"home", 37:"left", 38:"up", 39:"right", 40:"down", 43:"+", 44:"printscreen", 45:"insert", 46:"delete",
48:"0", 49:"1", 50:"2", 51:"3", 52:"4", 53:"5", 54:"6", 55:"7", 56:"8", 57:"9", 59:";",
61:"=", 65:"a", 66:"b", 67:"c", 68:"d", 69:"e", 70:"f", 71:"g", 72:"h", 73:"i", 74:"j", 75:"k", 76:"l",
77:"m", 78:"n", 79:"o", 80:"p", 81:"q", 82:"r", 83:"s", 84:"t", 85:"u", 86:"v", 87:"w", 88:"x", 89:"y", 90:"z",
96:"0", 97:"1", 98:"2", 99:"3", 100:"4", 101:"5", 102:"6", 103:"7", 104:"8", 105:"9",
106: "*", 107:"+", 109:"-", 110:".", 111: "/",
112:"f1", 113:"f2", 114:"f3", 115:"f4", 116:"f5", 117:"f6", 118:"f7", 119:"f8", 120:"f9", 121:"f10", 122:"f11", 123:"f12",
144:"numlock", 145:"scrolllock", 186:";", 187:"=", 188:",", 189:"-", 190:".", 191:"/", 192:"`", 219:"[", 220:"\", 221:"]", 222:"'"
},
modifiedByShift : {
192:"~", 48:")", 49:"!", 50:"@", 51:"#", 52:"$", 53:"%", 54:"^", 55:"&", 56:"*", 57:"(", 109:"_", 61:"+",
219:"{", 221:"}", 220:"|", 59:":", 222:"\"", 188:"<", 189:">", 191:"?",
96:"insert", 97:"end", 98:"down", 99:"pagedown", 100:"left", 102:"right", 103:"home", 104:"up", 105:"pageup"
}
};
回答by Aravindhan
In javascript each key has associated with a ASCII code as follows
在 javascript 中,每个键都与一个 ASCII 代码相关联,如下所示
1-49
2-50
like this http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
像这样 http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
So you need to map this values according to the keypress event.
所以你需要根据按键事件来映射这个值。
回答by Barlas Apaydin
You can detect all key values like this:
您可以像这样检测所有键值:
Here is working jsFiddle example.
$('textarea').keydown(function(e) {
var order = e.which;
console.log(order);
});?
回答by Kirill Reznikov
Chrome and Opera today have Read only property data
in event object.
今天的 Chrome 和 Operadata
在事件对象中具有只读属性。
It is written on MDN that currently this feature is "Working Draft".
写在 MDN 上,目前这个功能是“工作草案”。
https://developer.mozilla.org/en-US/docs/Web/API/InputEvent/data
https://developer.mozilla.org/en-US/docs/Web/API/InputEvent/data
回答by Neil
The key code does not directly map to the character value. Instead, you need to look at the keypress
event, which provides you with a charCode
property. You can then use String.fromCharCode
to turn that into a string.
键码不直接映射到字符值。相反,您需要查看keypress
为您提供charCode
属性的事件。然后你可以用String.fromCharCode
它把它变成一个字符串。
回答by Vishal Suthar
To get the character of the ASCII
:
要获取 的字符ASCII
:
String.fromCharCode();
to turn ASCII
into a string.
转ASCII
成字符串。