20

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

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
kmunky
  • 15,383
  • 17
  • 53
  • 64

7 Answers7

35

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.

Dave Ward
  • 59,815
  • 13
  • 117
  • 134
  • 1
    thanks, where can i find a complete list of the javascript keycodes? i found one or two but there i found "-" as 109 and 189 – kmunky Dec 13 '09 at 23:10
  • 2
    http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx – Matt Dec 13 '09 at 23:10
  • ok, i fount a good one :P http://forums.devarticles.com/javascript-development-22/very-helpful-key-codes-list-72985.html – kmunky Dec 13 '09 at 23:12
  • Why worry about the actual value of the character code? Why not just convert it to a string using `String.fromCharCode`? – Tim Down Dec 14 '09 at 16:41
  • Because it's quite a bit faster. – Dave Ward Dec 14 '09 at 17:04
  • Fair enough, although unless you have loads of keypress event handlers I think it's unlikely to make much of a difference. – Tim Down Dec 14 '09 at 22:07
  • It's one of those things that may be premature optimization, but why knowingly degrade the performance of a heavily called function if you don't have to. If readability is an issue, you can always comment it without impacting performance. – Dave Ward Dec 14 '09 at 22:11
13

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

user5943894
  • 131
  • 1
  • 2
  • I appreciate your insight to how Firefox handles `keyCode 189`. Seems silly to me that there aren't set standards for stuff like this. `keyCode 189` is a `-` on every browser I test with except Firefox. Reminds me of how IE likes to be a browser trailblazer, regardless of the incompatibility issues that arise from their wild slinging of the machete. Thanks again for your help. – fmc Nov 03 '16 at 02:23
9

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

Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • Actually `keydown` is a better answer than `keypress`. If you look at the compatibility list (http://www.quirksmode.org/js/keys.html) `keypress` didn't work in some older versions of Firefox – Mottie Dec 14 '09 at 02:51
  • @fudgey: I think you're misreading those compatibility tables. The `charCode` and `which` properties of the `keypress` event have been happily reporting character codes correctly in Firefox since I first tried it, which I think was pre-1.0. What you may be picking out from those tables is that Firefox always reports 0 for the `keyCode` property of `keypress` events, which really isn't a problem here. – Tim Down Dec 14 '09 at 09:31
  • @TimDown Thank you. Chrome worked with onkeydown and key codes 109 and 189 but Firefox did not. After switching to the onkeypress and to the method you described, detecting '-' works well everywhere. – Akseli Palén Sep 02 '13 at 21:34
7

You can discover keyCodes this way:

Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
6

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

Matt
  • 43,482
  • 6
  • 101
  • 102
5

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
}
Jeff Fairley
  • 8,071
  • 7
  • 46
  • 55
  • In Firefox and Chrome, it was necessary to change `.includes(e.code)` to `.includes(e.originalEvent.code)` when used in conjunction with `keydown`. – knot22 Nov 11 '19 at 01:17
3

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
johnnyArt
  • 4,327
  • 5
  • 29
  • 28