5

While I know that capturing keys due to the e.keyCode vs e.charCode is not trivial, I thought that jQuery would pretty much be able to normalize most of those inconsistencies.

However while answering this question I found out that the character # seems to have very inconsistent keyCodes (and of course this is true for several other codes also, mostly depending on the browser and keyboardlayout I guess).

Chrome and IE yielded 191, Firefox 163 on my computer, another user reported 222. Chromes window.event even reported U+00BF as keyIdentifier - which according to unicode tables should be ¿.

Do you know any consistent way to determine such symbols like the # with inconsistent keyCodes without doing something nasty like the following:

$('input').keydown(function (e) {
        if (e.which == 191 || e.which == 163 || e.which == 222){
            // hope you got the right key
            e.preventDefault();
        }
});

Fiddle for your pleasure.

Community
  • 1
  • 1
Christoph
  • 50,121
  • 21
  • 99
  • 128
  • 1
    Uhm... `#` is a key `3` with `shift` modifier. There is no key `#` at all – zerkms Jun 19 '13 at 09:58
  • 3
    @zerkms I guess that depends on your keyboard layout:-p – Christoph Jun 19 '13 at 09:59
  • still - it's **key**down. There is no key `#` – zerkms Jun 19 '13 at 10:01
  • In the fiddle I get 16 when I press the shift key and 51 when I press the 3 key. – JJJ Jun 19 '13 at 10:03
  • @zerkms I don't know what you are talking about. A key is a part of the keyboard, and mine is equipped with a key holding a `#` symbol. Perhaps you should get a proper keyboard? But enough smack talk, every userinput generates a keycode, so `#` will too. The question is how to *reliably* detect that. – Christoph Jun 19 '13 at 10:03
  • 2
    Keycodes from `keyup/down` represent a key on a keyboard, not a character. This code doesn't depend on used language, rather it depends on the manufacturer of the keyboard/JS implementation. You can get a _character_ code when using `onkeypress`. – Teemu Jun 19 '13 at 10:07
  • @Teemu keypress is not part of the official standard, that's why I tried to avoid it. – Christoph Jun 19 '13 at 10:17
  • I see... Though I've always used it when characters have been needed, without any troubles. Btw, it seems I've (scandinavian) a similar keyboard with zerkms, for example `[` appears by hitting `AltGr + 8`. – Teemu Jun 19 '13 at 10:23
  • 3
    @Christoph @zerkms Keyboards differ around the world, and between manufacturers. The keyboard I'm typing this on has a `#` key, next to enter (http://www.trusted-pc-components.co.uk/images/fujitsu-siemens-amilo-m7440-uk-keyboard.jpg). My MacBook Air's keyboard doesn't (http://www.digitaltrends.com/wp-content/uploads/2012/06/Apple-MacBook-Air-11-6-inch-2012-review-keyboard-display.jpg). (On the Mac, `#` is 3 with an alt modifier, rather than shift.) – Paul D. Waite Jun 19 '13 at 10:25

2 Answers2

7

This works for me in Chrome and Firefox with a US keyboard:

$('[id$=txtClient]').keypress(function (e) {
    if (String.fromCharCode(e.which) == '#') {
        e.preventDefault();
    }
});

keypress is the only event that will give you reliable info on the character that was entered.

Demo: http://jsfiddle.net/elclanrs/ebcet/9/

elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • I read about keypress being unreliable, that's why I tried to avoid this: `as the keypress event isn't covered by any official specification, the actual behavior encountered when using it may differ across browsers, browser versions, and platforms.` But it works for me, too. Also that `String.fromCharCode` is really neat! – Christoph Jun 19 '13 at 10:13
  • 1
    I'm not sure if jQuery does some magic to the `keypress` event but has always worked for me in IE8+ and all modern browsers. – elclanrs Jun 19 '13 at 10:14
  • I'll wait for another answer that perhaps can achieve the same without keypress, otherwise of course I will accept your's as I'm delighted by the fromCharCode;). – Christoph Jun 19 '13 at 10:16
0

Have you tried using the keypress event ?

The documentation warns about possible differences in behavior between platforms.

In Firefox at least, e.which corresponds to the ascii code of the typed character after transformation :

$('#txtClient').keypress(function (e) {
    console.log('keypress:', e.which);
    if (e.which == 35) {
        return false;
    }
});

updated fiddle

LeGEC
  • 46,477
  • 5
  • 57
  • 104