HTML/HTA 文档中鼠标位置的实时更新

Live updates of the Mouse Position in HTML/HTA Document

我希望能够在 HTA 中不断实时更新鼠标位置 window。

我知道 Window.Event 仅在调用 DOM 事件时可用。所以有一个函数可以做到这一点

VBScript:

Function vbsUpdateMousePos()

  Dim X,Y, MouseX, MouseY

  Set MouseX=document.getElementById ("MouseX")
  Set MouseY=document.getElementById ("MouseY")

  X=Window.Event.ClientX
  Y=Window.Event.ClientY

  MouseX.value=X
  MouseY.value=y


End Function

JScript:

function jsUpdateMousePos ()
{

var MouseX=document.getElementById ("MouseX");
var MouseY=document.getElementById ("MouseY");

var x=window.event.clientX;
var y=window.event.clientY;

MouseX.value=x;
MouseY.value=y;

}
如果我用 'OnClick' 或 'onDblClick'

调用任何这些函数,

会起作用

<Body OnClick=jsUpdateMousePos OnDblClick=vbsUpdateMousePos>

但是当我尝试创建间隔时不是。

VBScript:

setInterval "vbsUpdateMousePos",100

JScript:

setInterval ("jsUpdateMousePos",100);

因为需要对象 "window.event" 因为没有 DOM 事件处理程序被调用。

但我希望能够实时更新位置(有点像您在 MSPaint.exe 程序中获得的,光标位置显示在状态栏中。)

这可能吗?


这里是完整的演示:(请注意,为了获得最佳效果,请将此片段另存为 .HTA 文件。)


<html>
<head>
<title>Mucho Cursor Position Del Hogar Por Favor</title>
<HTA:APPLICATION
  APPLICATIONNAME="Cursor Position"
  ID="IDontKnow"
  VERSION="6.9"/>
</head>

<script language="VBScript">

Function vbsUpdateMousePos()

  Dim X,Y, MouseX,MouseY

  Set MouseX=document.getElementById ("MouseX")
  Set MouseY=document.getElementById ("MouseY")

  X=Window.Event.ClientX
  Y=Window.Event.ClientY

  MouseX.value=X
  MouseY.value=y



End Function


Sub vbsStartInterval

  'Error Occurs when Trying this 
  setInterval "vbsUpdateMousePos",100

End Sub

</script>



<script language=JavaScript>


function jsUpdateMousePos ()
{

  var MouseX=document.getElementById ("MouseX");
  var MouseY=document.getElementById ("MouseY");

  var x=window.event.clientX;
  var y=window.event.clientY;

  MouseX.value=x;
  MouseY.value=y;

}



function jsStartInterval (){
  //Error Occurs when trying this
  setInterval ("jsUpdateMousePos",100);
}

</script>




<body onclick=jsUpdateMousePos ondblclick=vbsUpdateMousePos>
  <center>
 Click anywhere in this window to test JavaScript Update Cursor position<br>
 Double Click anywhere in this window to test VBScript Update Cursor Position

  <table>
    <tr>
      <td>
        Mouse X: 
      </td>
      <td>
        <input id=MouseX type=text size=4>
      </td>
    </tr>
    <tr>
      <td>
        Mouse Y: 
      </td>
      <td>
        <input id=MouseY type=text size=4>
      </td>
    </tr>
  </table>


  <button onclick=jsStartInterval title="start live updates of the cursor position">Start Interval (JavaScript)</button>

  <button onclick=vbsStartInterval title="start live updates of the cursor position">Start Interval (VBScript)</button>
  </center>
</body>

</html>

这很可能通过简单的 mousemove 函数调用实现。这在 .hta 文件中工作得很好。

<html>
<script>
function getCoords(e) {
    document.getElementById("xCoord").innerHTML = e.clientX;
    document.getElementById("yCoord").innerHTML = e.clientY;
}
</script>
<body onmousemove="getCoords(event)">
X:<span id="xCoord"></span> Y:<span id="yCoord"></span>
</body>
</html>