javascript如何捕捉鍵盤事件? (ZT)
?
<SCRIPT>
function fnTrapKD(){
?? if(oTrap.checked){
????? oOutput.innerText+="[trap = " + event.keyCode + "]";
????? event.returnValue=false;
?? }
?? else{
????? oOutput.innerText+=String.fromCharCode(event.keyCode);
?? }
}
function fnTrapKD(){
?? if(oTrap.checked){
????? oOutput.innerText+="[trap = " + event.keyCode + "]";
????? event.returnValue=false;
?? }
?? else{
????? oOutput.innerText+=String.fromCharCode(event.keyCode);
?? }
}
function test()
{
?if (event.keyCode==13)
?{
??alert("press enter");
?}
?
}
</SCRIPT>
<INPUT TYPE="checkbox" ID="oTrap" checked>顯示按鍵的keyCode<br />
在該輸入框按鍵:<INPUT ID="oExample" TYPE="text" onkeydown="fnTrapKD()"><br />
<TEXTAREA ID="oOutput" ROWS="10" COLS="50">
</TEXTAREA>
<br />{
?if (event.keyCode==13)
?{
??alert("press enter");
?}
?
}
</SCRIPT>
<INPUT TYPE="checkbox" ID="oTrap" checked>顯示按鍵的keyCode<br />
在該輸入框按鍵:<INPUT ID="oExample" TYPE="text" onkeydown="fnTrapKD()"><br />
<TEXTAREA ID="oOutput" ROWS="10" COLS="50">
</TEXTAREA>
<INPUT ID="oExample1" TYPE="text" onkeydown="test()">
<SCRIPT>
document.write( String.fromCharCode( 81,49 ) + "<br>" );
</SCRIPT>
使用event,keyCode來捕獲編碼,再用String的fromCharCode方法轉換成字母(大寫)
The fromCharCode(num1,num2,…, numN) method is a static String method that creates a string containing one or more characters whose Unicode values are passed as parameters.
posted on 2006-07-11 19:06 風人園 閱讀(1057) 評論(0) 編輯 收藏 所屬分類: JavaScript