简体   繁体   中英

Holding down 'ctrl+enter' gives a different keycode for keypress event than just straight 'enter' - But only on Windows

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 vs 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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM