KeyboardEvent.altKey - altキーを押しているか?
altKeyは、KeyboardEventのプロパティです。イベント発生時にaltキーが押されているか否かを返します。
概要
- 名前
- altKey
- 所属
- KeyboardEvent
- IDL
readonly attribute boolean altKey;
- 仕様書
- https://w3c.github.io/uievents/#dom-keyboardevent-altkey
説明
altキーが押されている場合はtrue、違うならfalseを返します。
デモ
KeyboardEvent.altKeyのデモです。textarea要素にkeydownイベントを設定しました。何かキーボードのボタンを押してみて下さい。
<!-- このコードは編集できます。 -->
<!DOCTYPE html>
<html>
<head>
<style>
textarea#hoge {
width: 80% ;
min-height: 140px ;
}
div#result {
white-space: pre-wrap ;
}
</style>
</head>
<body>
<textarea id="hoge"></textarea>
<hr>
<div id="result"></div>
<script>
/** try it! **/
var callbackFn = function ( event ) {
console.log( "altKey", event.altKey ) ;
resultElement.innerHTML = "" ;
resultElement.appendChild( new Text( "currentTarget: " + event.currentTarget + "(#" + event.currentTarget.id + ")" + "\n" ) ) ;
resultElement.appendChild( new Text( "ctrlKey: " + event.ctrlKey + "\n" ) ) ;
resultElement.appendChild( new Text( "shiftKey: " + event.shiftKey + "\n" ) ) ;
resultElement.appendChild( new Text( "altKey: " + event.altKey + "\n" ) ) ;
resultElement.appendChild( new Text( "metaKey: " + event.metaKey + "\n" ) ) ;
} ;
document.getElementById( "hoge" ).addEventListener( "keydown", callbackFn ) ;
/** try it! **/
var resultElement = document.getElementById( "result" ) ;
</script>
</body>
</html>
サポート状況
Chrome | Firefox | Safari | Edge | IE | Opera | iOS Safari | Android |
---|---|---|---|---|---|---|---|
● 43+ | ● | ● 7.1+ | ● | ● | ● | ● 7.0+ | ● |
関連記事
- MouseEvent.metaKey
- metaKeyは、MouseEventのプロパティです。イベント発生時にmetaキーが押されているか否かを返します。WindowsならWindowsキー、MacならCommandキーがこれにあたります。
- MouseEvent.altKey
- altKeyは、MouseEventのプロパティです。イベント発生時にaltキーが押されているか否かを返します。
- KeyboardEvent.metaKey
- metaKeyは、KeyboardEventのプロパティです。イベント発生時にmetaキーが押されているか否かを返します。WindowsならWindowsキー、MacならCommandキーがこれにあたります。
- KeyboardEvent.shiftKey
- shiftKeyは、KeyboardEventのプロパティです。イベント発生時にShiftキーが押されているか否かを返します。