8-2 ������L�P�ƹ��ƥ�

¥»¸`±N¤¶²ÐÁä½L»P·Æ¹«¨Æ¥ó¡A¥H¤Î¬ÛÃö½d¨Ò¡C

§Ú­Ì¥i¥H°»´ú¬Y¤@­Ó¯S©w«öÁä¬O§_³Q«ö¤U¡A¨ä¨BÆJ¦p¤U¡G

  1. §Q¥Î document.onkeydown ¨Ó§ì¨ì¡u«öÁä¨Æ¥ó¡v¡A¨Ã«ü©w¬ÛÃöªº¨Æ¥ó³B²zµ{¦¡¡C
  2. ¦b¨Æ¥ó³B²zµ{¦¡¤¤¡A¥i¥H§Q¥Î window.event.keycode ¨Óª¾¹D«öÁä¥N½X¡A¨Ã¶i¦æ¬ÛÃöªº³B²z¡C
¥H¤U¬O¤@­Ó¨ãÅé½d¨Ò¡A¥u­n§A«ö¤U¥ô¦ó¤@­ÓÁä½L¡A´N·|Åã¥Ü¹ïÀ³ªº¥N½X¡A¦p¤U¡G

Example¡]keyboardEvent01.htm¡^¡G

¤W­z½d¨Òªº§¹¾ã­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]keyboardEvent01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>
<body>
<h2 align=center>Áä½L¨Æ¥ó¡G°ò¥»´ú¸Õ</h2>
<hr>

<script>
function keyFunction() {
	alert("Key code = " + event.keyCode);
	if (event.keyCode==27) {
		alert("Esc ªº¤º«Ø¥\¯à¤w³Q¨ú®ø¡I");
		return false;
	} else if (event.keyCode==8) {
		alert("Backspace ªº¤º«Ø¥\¯à¤w³Q¨ú®ø¡I");
		return false;
	} else if (event.keyCode==9) {
		alert("Tab ªº¤º«Ø¥\¯à¤w³Q¨ú®ø¡I");
		return false;
	} else if (event.keyCode==71) {
		document.location="http://www.google.com";
	}
}
document.onkeydown=keyFunction;
</script>

½Ð«ö¤UÁä½L¥ô·NÁä¡A¥HÅã¥Ü«öÁä¥N½X¡I½Ðª`·N¡A¤U¦C«öÁ䪺¤º«Ø¥\¯à¤w³Q¨ú®ø¡G
<ul>
<li>Esc
<li>Tab
<li>Backspace
</ul>
¦¹¥~¡A"g" Á䦳¯S®í¥\¯à¡I

<hr>
</body>
</html>

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¨Ï¥Î onkeydown ¨Æ¥ó¨Ó°õ¦æ keyFunction() ¨ç¼Æ¡C¯S§O­nª`·Nªº¬O¡A¦pªG¨Æ¥ó³B²zµ{¦¡¦^¶Çµ¹ document.onkeydown ªº­È¬° false¡A«h­ì¥ý«öÁ䪺¹w³]¥\¯à±N·|³Q¨ú®ø¡C¨Ò¦p¡A¤W­z½d¨Òªº Backspace¡]¦^¨ì¤W¤@­¶¡^ªº¹w³]¥\¯à´N¤w¸g³Q¨ú®ø¤F¡C

Hint
window.event.keycode ¥i¥H²¼g¦¨ event.keycode¡C

¦³®É­Ô§Ú­Ì­n©w¸q¤@¨Ç¡u¼öÁä¡v¨Ó°õ¦æ¯S®í¥\¯à¡A¦ý¬O³o¨Ç¼öÁä³Ì¦n¬O½Æ¦XÁä¡]¥ý«ö Shift ©Î Ctrl ©Î Alt ¤£©ñ¡A¦A«ö¥ô·NÁä¡^¡A¥H§K»\±¼­ì¦³ªº¹w³]¥\¯à¡A¦]¦¹§Ú­Ì´N­n¯à°÷°»´ú½Æ¦XÁä±`¥Îªº Shift¡BCtrl¡BAlt¡A¥i¨£¤U¦C½d¨Ò¡G

Example¡]keyboardEvent02.htm¡^¡G

¤W­z½d¨Òªº§¹¾ã­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]keyboardEvent02.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>Áä½L¨Æ¥ó¡G°»´ú½Æ¦XÁä</h2>
<hr>

<script>
// Shift ªºÁä½L¥N½X¬O 16¡ACtrl ªºÁä½L¥N½X¬O 17¡AAlt ªºÁä½L¥N½X¬O 18
function keyFunction() {
	// ­Y¤£¥[²Ä¤G­Ó±ø¥ó¡A·|¦L¥X¨â¦¸Äµ§iµøµ¡
	// ¤@¦¸¬O«ö Shift¡A²Ä¤G¦¸¬O«ö¨ä¥LÁä
	if ((event.shiftKey) && (event.keyCode!=16))	
		alert("Shift + "+event.keyCode);
	if ((event.ctrlKey) && (event.keyCode!=17))
		alert("Ctrl + "+event.keyCode);
	if ((event.altKey) && (event.keyCode!=18))
		alert("Alt + "+event.keyCode);
}
document.onkeydown=keyFunction;
</script>
½Ð«ö¤UÁä½L¥ô·N½Æ¦XÁä¡]¥ý«ö Shift ©Î Ctrl ©Î Alt ¤£©ñ¡A¦A«ö¥ô·NÁä¡^¡A¥HÅã¥Ü«öÁä¥N½X¡I

<hr>
</body>
</html>

¥Ñ¤W­z½d¨Ò¥iª¾¡A§Ú­Ì¥i¥Î event.shiftKey¡Bevent.ctrlKey¡Bevent.altKey ªº­È¬O§_¬O true ¨Ó½T»{ Shift¡]Áä½L¥N½X¬O 16¡^¡BCtrl¡]Áä½L¥N½X¬O 17¡^¡BAlt¡]Áä½L¥N½X¬O 18¡^ ªº«öÁä¬O§_¬O¦b³Q«ö¤Uªºª¬ºA¡CÃþ¦üªº©Ê½èÁÙ¦³¡G

Hint
¦p¦ó°»´ú Windows Áä©M¨ä¥LÁä©Ò§Î¦¨ªº²Õ¦XÁä¡H­Y¦³ÅªªÌª¾¹D¡A½Ð±N½d¨Ò±Hµ¹§Ú¡AÁÂÁ¡C

¥H¤U¬O¤@­Ó§@§Ë¤Hªººô­¶¡G

Example¡]keyboardEvent03.htm¡^¡G

¤W­z½d¨Òªº§¹¾ã­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]keyboardEvent03.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>
<body>
<h2 align=center>Áä½L¨Æ¥ó¡G§@§Ë¤Hªººô­¶</h2>
<hr>

<script>
function encrypt(){
	event.keyCode-=1;
}
function check(){
	if (testField.value=="I am a pig")
		alert("µª¹ï¤F¡I");
}
</script>
½Ð¥ÑÁä½L¿é¤J¡uJ!bn!b!qjh¡v¡G
<p>
<input id=testField onKeyPress="encrypt()" onKeyUp="check()">

<hr>
</body>
</html>

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¨Ï¥Î input ¼ÐÅÒªº onKeyPress ¨Æ¥ó¨Ó§Y®É§ïÅܨϥΪ̪º¿é¤J¡A¨Ã¥Î onKeyUp ¨Æ¥ó¨ÓÀˬd¨Ï¥ÎªÌ¬O§_§¹¦¨¥¿½T¿é¤J¡C

¦b¥H¤Uªººô­¶¤¤¡A¬õ¦â¤è¶ô±NÀHµÛ§Aªº·Æ¹«²¾°Ê¡G

Example¡]newCursor01.htm¡^¡G

¤W­z½d¨Òªº§¹¾ã­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]newCursor01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body onmousemove="newCursor()">
<h2 align=center>Áä½L¨Æ¥ó¡GÂH¦í´å¼Ðªº¤è¶ô</h2>

<script>
function newCursor() {
	redSquare.style.posLeft=event.clientX-10;
	redSquare.style.posTop=event.clientY-10;
}
</script>
<div id=redSquare style="position:absolute; top:10; left:10; height:20; width:20; background-color:red"></div>

</body>
</html>

¦b¤W­z­ì©l½X¤¤¡A§Ú­Ì¨Ï¥Î id=redSquare ¨Ó¥Nªí¬õ¦â¤è¶ô¡Aevent.clientX ¤Î event.clientY ¤À§O¥Nªí·Æ¹«¬Û¹ïÀ³©óºô­¶µøµ¡ªº X ©M Y ®y¼Ð¡C

¦b¥H¤Uªººô­¶¤¤¡A§A¥i¥H¨Ï¥Î¤è¦VÁä¡]½bÀYÁä¡^¨Ó²¾°Êºô­¶¤¤ªººñ¦â¤è¶ô¡G

Example¡]squareMover01.htm¡^¡G

¤W­z½d¨Òªº§¹¾ã­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]squareMover01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body onkeydown="move()">
<h2 align=center>Áä½L¨Æ¥ó¡G¨Ï¥Î¤è¦VÁä¨Ó²¾°Êª«¥ó</h2>
<hr>

<script>
function move(){
	ek=event.keyCode;
	if (ek==37) myArea.style.posLeft-=5;
	if (ek==39) myArea.style.posLeft+=5;
	if (ek==38) myArea.style.posTop-=5;
	if (ek==40) myArea.style.posTop+=5;
}
</script>
<div id=myArea style="position:absolute; top:20; left:20; height:50; width:50; background-color:#00FF00"></div>
½Ð«öÁä½Lªº¤è¦VÁä¨Ó²¾°Êºñ¦âªº¤è¶ô¡C

<hr>
</body>
</html>

¥t¥~¡A§Q¥Î event.button¡A§Ú­Ì¥i¥H°»´ú¨Ï¥ÎªÌ¥Î¨Ó²£¥Í·Æ¹«¨Æ¥óªº·Æ¹«Áä¡]¥ªÁä©Î¬O¥kÁä¡^¡A¨Ò¦p¡G

Example¡]mouseEvent01.htm¡^¡G

¦b¤W­z½d¨Ò¤¤¡A¨Ï¥Î¤£¦P·Æ¹««öÁä¥hÂI¿ï¤å¦r¡A´N·|²£¥Í¤£¦PªºÄµ§iµøµ¡¨ÓÅã¥Ü©Ò¥Îªº·Æ¹««öÁä¡C¤W­z½d¨Òªº§¹¾ã­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]mouseEvent01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>·Æ¹«¨Æ¥ó¡G°»´ú·Æ¹«Áä</h2>
<hr>

<script>
function showMouseButton() {
	switch (event.button){
		case 1:
			alert("§A¥Î·Æ¹«¥ªÁä¡I");
			break;
		case 2:
			alert("§A¥Î·Æ¹«¥kÁä¡I");
			break;
		case 4:
			alert("§A¥Î·Æ¹«¤¤Áä¡I");
			break;
		default:
			alert("¥¼ª¾ªº·Æ¹«Áä¡I");
			break;
	}
}
</script>
<div onMouseDown="showMouseButton()">½Ð¥Î·Æ¹««ö§Ú¡I</div>

<hr>
</body>
</html>

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¨Ï¥Î event.button ¨Ó°»´ú·Æ¹««öÁä¡G

¬°¤F¨¾¤î¡]©Î¬Oªýê¡^¨Ï¥ÎªÌ¤U¸ü¹Ï¤ù©Î¬OÀ˵ø­ì©lÀÉ¡A§Ú­Ì¥i¥H¨ú®ø·Æ¹«¥kÁ䪺¹w³]¥\¯à¡A½d¨Ò¦p¤U¡G

Example¡]disableRightButton01.htm¡^¡G

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¨Ï¥Î event.button ¨Ó°»´ú·Æ¹«Áä¡A¦pªG¬O¥kÁä¡A«h¥Hĵ§iµøµ¡¼J¯º¨Ï¥ÎªÌ¡A¨Ã¦^¶Ç false¡A¥H¨ú®ø¹w³]¤§¥\¯à¡C·íµM¡A¡u¹D°ª¤@¤Ø¡AÅ]°ª¤@¤V¡v¡A­Y§AÁÙ¬O°õ·N­n§ì¹Ï¡A¦b¤W­zºô­¶«ö¤U Ctrl-a¡A§Y¥i¬Ý¨ìºJ¨B¡C¡]¥Ñ©ó½g´T¦³­­¡A¤£¦A¦C¥X¥»½d¨Ò¤§µ{¦¡½X¡A§A¥i¥H¥Ñ½d¨Òºô­¶¦Û¦æÀ˵ø­ì©l½X¡C¡^

¬ÛÃö½d¨Ò¡G


JavaScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©óºô­¶¥Î¤áºÝ