[Edge]。→、
要素でキーが放されたときに発生します。シフト キーなどの装飾キーも発生します。
mdn: keydown と keyup イベントは、どのキーが押されたかを示すコードを提供し、keypress はどの文字が入力されたかを示します。例えば、小文字の "a" は keydown と keyup では 65 と報告され(¶keyCode)、keypress では 97 と報告されます。大文字の "A" は、どのイベントでも 65 と報告されます。
mdn: Firefox 65 以降では、CJKT ユーザーのブラウザー間の互換性を高めるために、keyup および keydown イベントが IME 変換中に発行されるようになりました(バグ 354358)。変換操作中の keyup イベントをすべて無視するには、次のようにしてください(229 は、IME によって処理されたイベントに関連する keyCode に設定される特別な値です)。
バブリング | あり |
---|---|
キャンセル | 不可 |
インターフェイス | KeyboardEvent |
イベント ハンドラー プロパティ | onkeyup |
この例では、<input> 要素内でキーを離すたびに、KeyboardEvent.code 値を記録します。
HTML:
<input placeholder="ここをクリックして、キーを押して離してください。" size="40"> <p id="log"></p>
JavaScript:
const input = document.querySelector('input'); const log = document.getElementById('log'); input.addEventListener('keyup', logKey); function logKey(e) { log.textContent += ` ${e.code}`; }
HTML:
<INPUT id=IdTest><BR> <SPAN id=IdLog></SPAN><BR> <SPAN id=IdLog2></SPAN><BR>
JavaScript:
function Job(eve){ //keydownイベントの処理: D=eve; //=[object]:[object KeyboardEvent]。日本語入力で「あ」を入力するために[a]を押した時。IdLog.innerHTML="code:"+ eve.code +"、key:"+ eve.key; } function Job2(eve){ //keyupイベントの処理: DA=eve; //=[object]:[object KeyboardEvent]。日本語入力で「あ」を入力するために[a]を押して放した時。詳細
//isTrusted=[boolean]:true //key=[string]:Process //code=[string]:KeyA //location=[number]:0 //ctrlKey=[boolean]:false //shiftKey=[boolean]:false //altKey=[boolean]:false //metaKey=[boolean]:false //repeat=[boolean]:false //isComposing=[boolean]:false //charCode=[number]:0 //keyCode=[number]:229 //DOM_KEY_LOCATION_STANDARD=[number]:0 //DOM_KEY_LOCATION_LEFT=[number]:1 //DOM_KEY_LOCATION_RIGHT=[number]:2 //DOM_KEY_LOCATION_NUMPAD=[number]:3 //getModifierState=[function]:function getModifierState() { [native code] } //initKeyboardEvent=[function]:function initKeyboardEvent() { [native code] } //view=[object]:[object Window] //detail=[number]:0 //sourceCapabilities=[object]:[object InputDeviceCapabilities] //which=[number]:229 //initUIEvent=[function]:function initUIEvent() { [native code] } //type=[string]:keydown //…IdLog2.innerHTML="code:"+ eve.code +"、key:"+ eve.key; } IdTest.onkeydown=Job; IdTest.onkeyup=Job2;詳細
//isTrusted=[boolean]:true //key=[string]:a //code=[string]:KeyA //location=[number]:0 //ctrlKey=[boolean]:false //shiftKey=[boolean]:false //altKey=[boolean]:false //metaKey=[boolean]:false //repeat=[boolean]:false //isComposing=[boolean]:true //charCode=[number]:0 //keyCode=[number]:65 //DOM_KEY_LOCATION_STANDARD=[number]:0 //DOM_KEY_LOCATION_LEFT=[number]:1 //DOM_KEY_LOCATION_RIGHT=[number]:2 //DOM_KEY_LOCATION_NUMPAD=[number]:3 //getModifierState=[function]:function getModifierState() { [native code] } //initKeyboardEvent=[function]:function initKeyboardEvent() { [native code] } //view=[object]:[object Window] //detail=[number]:0 //sourceCapabilities=[object]:[object InputDeviceCapabilities] //which=[number]:65 //initUIEvent=[function]:function initUIEvent() { [native code] } //type=[string]:keyup //…