Keycode detection on AZERTY vs. QWERTY

你说的曾经没有我的故事 提交于 2021-01-28 07:00:20

问题


How can I in JavaScript detect the typing of a question mark on AZERTY keyboard ? On QWERTY keyboard a question mark produces the code 191, but on AZERTY it seems to produce code 188 (comma on QWERTY). Or should I distinguish between both keyboards in JavaScript, but how ?


回答1:


The fastest solution I can think of is to compare the key with the actual question mark, so something like this would be a good solution.

document.addEventListener('keydown', function(event) {
    if (event.key && event.key === '?') {
        // your code goes here
    }
}, true);



回答2:


If you want to detect the character being typed, use KeyboardEvent.key, not KeyboardEvent.code -- the key property will contain either the character that was typed (like "?"), or a string like "Shift" or "ArrowUp" for special keys. The location of the key on the keyboard won't affect the result.

$("#f").on("keydown", function(ev) {
  $(this).val(ev.key);
  return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="f" autocomplete="off">


来源:https://stackoverflow.com/questions/45624118/keycode-detection-on-azerty-vs-qwerty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!