The meaning of .altKey .ctrlKay .shiftKey in the keyboard event object, and the usage of .keyCode and a small demo (.value represents the input content of the input box)

		//键盘事件对象中的altkey ctrlKey shiftKey对应三个键alt ctrl shift
		document.onkeydown = function(e){
			var evt = e || event;
			console.log(evt.altKey, evt.ctrlKey, evt.shiftKey);
			//判断是否按了某一个键
			//我们这里以回车键举例.keyCode
			if(evt.keyCode === 13){
				console.log('你按的是回车键');
			}else{
				console.log('你按得不是回车键');
			}
		}
<!DOCTYPE HTML>
<html lang = 'en'>
	<head>
		<meta charset = 'UTF-8'/>
		<title>Document1</title>
		<style>
			div{
    
    
				width:500px;
				height:500px;
				border:1px solid black;
			}
		</style>
	</head>
	<body>
		<div></div>
		<input type = 'text'/>
		<input type = 'button' value = '发布'/>
		<script>
			var div = document.getElementsByTagName('div')[0];
			var text = document.getElementsByTagName('input')[0];
			var button = document.getElementsByTagName('input')[1];
			button.onclick = function(){
    
    
				div.innerHTML = text.value;
			}
			document.onkeydown = function(e){
    
    
				var evt = e || event;
				console.log(evt.ctrlKey);
				console.log(evt.keyCode);
				
				if(evt.keyCode === 13&&evt.ctrlKey){
    
    
					div.innerHTML = text.value;
				}
				
			}
		</script>
	</body>
</html>

Guess you like

Origin blog.csdn.net/weixin_48727085/article/details/108235251