日期:2014-05-16  浏览次数:20376 次

JS在网页非当前状态下获取键盘输入值(感觉好难啊,没什么头绪)
请教各位高手一个问题
现有如下一段代码(网上找的):
JScript code

<HTML>
<HEAD><TITLE>js 按键记录</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="羽殇仁">
<META NAME="Keywords" CONTENT="js 按键记录">
<META NAME="Description" CONTENT="js 按键 记录">
</HEAD>
<BODY>
<script type="text/javascript">
var keystring = "";//记录按键的字符串
function $(s){return document.getElementById(s)?document.getElementById(s):s;}
function keypress(e)
{
var currKey=0,CapsLock=0,e=e||event;
currKey=e.keyCode||e.which||e.charCode;
CapsLock=currKey>=65&&currKey<=90;
switch(currKey)
{
    //屏蔽了退格、制表、回车、空格、方向键、删除键
    case 8: case 9:case 13:case 32:case 37:case 38:case 39:case 40:case 46:keyName = "";break;
    default:keyName = String.fromCharCode(currKey); break;
}
keystring += keyName;
}
function keydown(e)
{
var e=e||event;
var currKey=e.keyCode||e.which||e.charCode;
if((currKey>7&&currKey<14)||(currKey>31&&currKey<47))
{
switch(currKey)
    {
      case 8: keyName = "[退格]"; break;
      case 9: keyName = "[制表]"; break;
      case 13:keyName = "[回车]"; break;
      case 32:keyName = "[空格]"; break;
      case 33:keyName = "[PageUp]"; break;
      case 34:keyName = "[PageDown]"; break;
      case 35:keyName = "[End]"; break;
      case 36:keyName = "[Home]"; break;
      case 37:keyName = "[方向键左]"; break;
      case 38:keyName = "[方向键上]"; break;
      case 39:keyName = "[方向键右]"; break;
      case 40:keyName = "[方向键下]"; break;
      case 46:keyName = "[删除]"; break;
      default:keyName = ""; break;
}
keystring += keyName;
}
$("content").innerHTML=keystring;
}
function keyup(e)
{
$("content").innerHTML=keystring;
}
document.onkeypress=keypress;
document.onkeydown =keydown;
document.onkeyup =keyup;
</script>
<input type="text" />
<input type="button" value="清空记录" onclick="$('content').innerHTML = '';keystring = '';"/>
<br/>请按下任意键查看键盘响应键值:<span id="content"></span>
</BODY>
</HTML>


利用这段代码在网页处于顶端时(也就是当前页面时)可以获取键盘值!
当如果我将网页最小化时(即非当前页面时)就不能获得值了。
请问还有什么其它办法可以实现:在这个网页最小化的情况也能获得键盘输入值!

------解决方案--------------------
document.onkeypress=keypress;
document.onkeydown =keydown;
document.onkeyup =keyup;
最小化了 无法做到吧 关注~~~~~~~