IE10 中的 JavaScript 键盘事件

JavaScript Keyboard Events in IE10

本文关键字:事件 键盘 JavaScript 中的 IE10      更新时间:2023-09-26

我一直在尝试获取 VBA 脚本来调度键控事件,但没有成功......没有太多关于在InternetExplorer上获得"iniKeyboardEvent"的信息。

Sub ie_evt()
Dim ie As InternetExplorer
Dim doc As HTMLDocument
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onkeydown.htm"
Do While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
ie.Visible = True
Set doc = ie.Document
 doc.parentWindow.execScript ("var tb = document.getElementById('oExample')")
 doc.parentWindow.execScript ("tb.value = 'a'")
 doc.parentWindow.execScript ("var e = document.createEvent('KeyboardEvent')")
 doc.parentWindow.execScript ("e.initKeyboardEvent('keydown', true, true, window, false, false, false, false, 65, 0);")
 doc.parentWindow.execScript ("tb.dispatchEvent(e)")
End Sub

谁能帮我?

谢谢

这在 VBA 中充其量总是狡猾的,但您可以尝试使用 Win API 将键盘事件发送到活动窗口。 2个要点:

  • 确保 IE 窗口在发送密钥事件时是活动窗口(您可以使用 AppActivate() 或其他 API 方法来执行此操作)

  • 您需要知道要"按下"的键的虚拟键代码,这是其中大多数的列表

然后在标准代码模块中像这样:

#If Win64 Then
    Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
#Else
    Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
#End If
'// Example, send the number 5
Const NUM_5 As Byte = &H65
Sub foo()
'// your code here - ensure IE window is active window and then:
'// Send key press event
keybd_event NUM_5, 0&, 0&, 0&
'// rest of code
End Sub