7

I have the following Javascript:

$(function(){
    $("#foo").keypress(function (event) {
        if (event.keyCode == 13) {
            console.log(event.ctrlKey ? "Ctrl+Enter (13)" : "Enter (13)");   
        }
        else if (event.keyCode == 10) {
            console.log(event.ctrlKey ? "Ctrl+Enter (10)" : "Enter (10)");
        }
    });
});

Foo in this case is an input box.

Under Windows, and Windows only - holding down the CTRL modifier changes the keyCode from 13 to 10. So if I do Enter v.s. CTRL + Enter, I see Enter (13) and Ctrl+Enter (10) in the console. Mac OS and Linux don't do this regardless of browser.

Why is this?

Fiddle to play with at http://jsfiddle.net/K6NhF/

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
PhonicUK
  • 13,486
  • 4
  • 43
  • 62
  • In many programs they have different meanings. Often one is Submit hotkey while the other is for line break. It should also work with Linux, at least it does for me. Tested the fiddle with Fedora 20 and current Chrome. – kasoban May 27 '14 at 12:33
  • OSX is doing this for me. Chrome 34 on OSX 10.9.2 – Rory McCrossan May 27 '14 at 12:39
  • Perhaps it's a locale related thing? I got a few other people to try it out in the #jquery IRC channel and they reported getting `Ctrl+Enter (13)`. – PhonicUK May 27 '14 at 12:44
  • 2
    This isn't just windows only. This is browser/program related. Went through this issue a couple years back. It may be a more narrow problem now, but keydown/keyup have always been more reliable. An answer [**here**](http://stackoverflow.com/questions/1367700/whats-the-difference-between-keydown-and-keypress-in-net#1367865) explains the difference a lot better. Also, get more information about key events in JS [**here**](http://www.quirksmode.org/js/keys.html). Here's a [**jsFiddle**](http://jsfiddle.net/LVB3H/) for testing difference seen in key events. – SpYk3HH May 27 '14 at 13:04

1 Answers1

1

There a lot of inconsistencies in different browsers, here's an article describing them JavaScript Madness: Keyboard Events

Also there's a library for normalizing keycodes in javascript keycode.js