简体   繁体   English

按住'ctrl + enter'可以为按键事件提供不同的键码,而不仅仅是直接'输入' - 但仅限于Windows

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

I have the following Javascript: 我有以下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. 在这种情况下,Foo是一个输入框。

Under Windows, and Windows only - holding down the CTRL modifier changes the keyCode from 13 to 10 . 在Windows和Windows下 - 按住CTRL修饰符可将keyCode从13更改为10 So if I do Enter vs CTRL + Enter , I see Enter (13) and Ctrl+Enter (10) in the console. 因此,如果我执行Enter vs CTRL + Enter ,我会在控制台中看到Enter (13)Ctrl+Enter (10) Mac OS and Linux don't do this regardless of browser. 无论浏览器如何,Mac OS和Linux都不会这样做。

Why is this? 为什么是这样?

Fiddle to play with at http://jsfiddle.net/K6NhF/ http://jsfiddle.net/K6NhF/玩弄小提琴

There a lot of inconsistencies in different browsers, here's an article describing them JavaScript Madness: Keyboard Events 不同浏览器中存在很多不一致之处,这里有一篇描述JavaScript疯狂:键盘事件的文章

Also there's a library for normalizing keycodes in javascript keycode.js 还有一个用于规范化javascript keycode.js中的密钥代码的库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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