Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript subtract(-) keycode

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 109 and 189 key codes but i still don't get the desired result. although i press "-" i don't get that alert box

like image 309
kmunky Avatar asked Dec 13 '09 23:12

kmunky


People also ask

What is e KeyCode === 13?

key 13 keycode is for ENTER key.

What is charCode in JavaScript?

The charCode property returns the Unicode character code of the key that triggered the onkeypress event. The Unicode character code is the number of a character (e.g. the number "97" represents the letter "a").


7 Answers

The JavaScript charCodes, which you test for during a keypress event, are ASCII. 109 is the correct keyCode, if used in a keydown or keyup event.

"-" has a charCode of 45.

like image 159
Dave Ward Avatar answered Sep 28 '22 18:09

Dave Ward


1.event.keyCode = 109 is '-' on numpad

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

like image 40
user5943894 Avatar answered Sep 28 '22 19:09

user5943894


Don't do this in a keydown event 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 keypress event and then you can get the character code instead, which is what you actually want.

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

like image 45
Tim Down Avatar answered Sep 28 '22 20:09

Tim Down


You can discover keyCodes this way:

  • Javascript Char Codes (Key Codes)
  • Detecting keystrokes
like image 31
Rubens Farias Avatar answered Sep 28 '22 19:09

Rubens Farias


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

like image 23
Matt Avatar answered Sep 28 '22 20:09

Matt


People are moving away from event.keyCode and event.charCode due to inconsistent implementations across browsers. Instead, use event.code. This is a string field.

Most browsers support Array.includes, so I tend to do the following:

if (['-', 'Minus', 'NumpadSubtract'].includes(e.code)) {
    // do something
}
like image 43
Jeff Fairley Avatar answered Sep 28 '22 19:09

Jeff Fairley


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
like image 31
johnnyArt Avatar answered Sep 28 '22 20:09

johnnyArt