<SCRIPT>
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 />
<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.