Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get key value of a key pressed

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'.

Edit: I'm aware that Ascii code of key '1'.

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.

like image 688
bl0b Avatar asked Oct 02 '12 12:10

bl0b


People also ask

How do you figure out what key is being pressed?

The Windows on-screen keyboard is a program included in Windows that shows an on-screen keyboard to test modifier keys and other special keys. For example, when pressing the Alt , Ctrl , or Shift key, the On-Screen Keyboard highlights the keys as pressed.

What is e keyCode === 13?

Keycode 13 is the Enter key.

How do you see which key is being pressed JavaScript?

Attach an event to the input box. like onkeypress event. Call a function when that event happens and pass the event parameter in it. In the called function, identify the key pressed.

What properties can we use to access the value of the pressed key and the key's code?

The KeyboardEvent interface's key read-only property returns the value of the key pressed by the user, taking into consideration the state of modifier keys such as Shift as well as the keyboard locale and layout.


3 Answers

As it's told in comment it's ASCII code. To get it as character you can do:

alert(String.fromCharCode(evt.keyCode));
like image 118
Leri Avatar answered Oct 06 '22 00:10

Leri


For those who google it now, like I am

$('input').on('keydown', function(e) {
  console.log(e.key);
});​
like image 30
tarasikarius Avatar answered Oct 06 '22 01:10

tarasikarius


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/

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"
    }

};
like image 28
Salketer Avatar answered Oct 06 '22 00:10

Salketer