SYNCERのロゴ
アイキャッチ画像

KeyboardEvent.shiftKey - shiftキーを押しているか?

shiftKeyは、KeyboardEventのプロパティです。イベント発生時にShiftキーが押されているか否かを返します。

概要

名前
shiftKey
所属
KeyboardEvent
IDL
readonly attribute boolean shiftKey;
仕様書
https://w3c.github.io/uievents/#dom-keyboardevent-shiftkey

説明

shiftキーが押されている場合はtrue、違うならfalseを返します。

デモ

KeyboardEvent.shiftKeyのデモです。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( "shiftKey", event.shiftKey ) ;

	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>

サポート状況

ChromeFirefoxSafariEdgeIEOperaiOS SafariAndroid
19+ 5+ 6.0+ 6.0+ 4.3+
  • Twitterでシェア
  • Facebookでシェア
  • Google+でシェア
  • はてなブックマークでシェア
  • pocketに保存
  • LINEでシェア
更新履歴
2017年10月9日 (月)
コンテンツを公開しました。