DOM

KeyboardEvent.key プロパティ

 編集:2022.10.13 by T.A.

[Edge]。読取専用。→

KeyboardEvent が発生したとき、押されていたキーの値を文字列で示します。Shiftキーなどの修飾キーやキーボードのロケールやレイアウトを考慮した値で返します。

構文
KeyboardEvent.key
KeyboardEvent KeyboardEvent

文字列。

keydown イベントで各種表示

HTML:

<SPAN id=IdTest1 contenteditable>[SPAN要素。ここで編集操作…]</SPAN><BR>
<INPUT id=IdTest2 type=text><BR>
<SPAN id=IdLog></SPAN>

JavaScript:

function Job(eve){ //keydown イベントの処理:
  D=eve;
  IdLog.innerHTML="key:"+ eve.key +"<BR>"
	+"code:"+ eve.code +"<BR>"
	+"shiftKey:"+ eve.shiftKey +"<BR>"
	+"ctrlKey:"+ eve.ctrlKey +"<BR>"
	+"altKey:"+ eve.altKey +"<BR>"
	+"metaKey:"+ eve.metaKey;  //表示。
}
IdTest1.onkeydown=Job;  //イベント割り当て。
IdTest2.onkeydown=Job;  //イベント割り当て。

Element:keydowninnerHTML


テスト