So, event.code
may match a wrong character for unexpected layout. Same letters in different layouts may map to different physical keys, leading to different codes. Luckily, that happens only with several codes, e.g. keyA
, keyQ
, keyZ
(as we’ve seen), and doesn’t happen with special keys such as Shift
. You can find the list in the
영어 사용국가 키보드와 독일의 키보드의 레이아웃이 다르기 때문에 event.code값은 같아도 event.key값은 다른 현상이 발생함.
event.code값 자체는 영미권 사용국가의 키보드 layout에 연결되어 있음.
Preventing the default action on keydown
can cancel most of them, with the exception of OS-based special keys. For instance, on Windows Alt+F4 closes the current browser window. And there’s no way to stop it by preventing the default action in JavaScript.
OS-based 키 다운 이벤트는 event.preventDefault()로 막을 수 없다.
switch (event.key) {
case "Down": // IE/Edge specific value
case "ArrowDown":
// Do something for "down arrow" key press.
break;
case "Up": // IE/Edge specific value
case "ArrowUp":
// Do something for "up arrow" key press.
break;
case "Left": // IE/Edge specific value
case "ArrowLeft":
// Do something for "left arrow" key press.
break;
case "Right": // IE/Edge specific value
case "ArrowRight":
// Do something for "right arrow" key press.
break;
case "Enter":
// Do something for "enter" or "return" key press.
break;
case "Esc": // IE/Edge specific value
case "Escape":
// Do something for "esc" key press.
break;
default:
return; // Quit when this doesn't handle the key event.
}