Holding down "ctrl + enter" gives a different key code for a keypress event than just direct '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 field.

On Windows and only Windows - CTRL modifier containment changes keyCode from 13 to 10 . Therefore, if I do Enter vs CTRL + Enter , I see Enter (13) and Ctrl+Enter (10) in the console. Mac OS and Linux do not do this regardless of browser.

Why is this?

Try playing with http://jsfiddle.net/K6NhF/

+6
source share
1 answer

There are many inconsistencies in different browsers, their article is described here JavaScript Madness: Keyboard Events

There is also a library for normalizing keycodes in javascript keycode.js

+1
source

Source: https://habr.com/ru/post/969906/


All Articles