简体   繁体   English

IE10中的JavaScript键盘事件

[英]JavaScript Keyboard Events in IE10

I have been trying to get a VBA script to dispatch a keydown event but no success... There is no much information on getting the 'iniKeyboardEvent'working on InternetExplorer as well.. 我一直在尝试获取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

Can anyone assist me? 谁能帮助我?

Thanks 谢谢

This is always dodgy at best in VBA, but you could try using Win API to send a keyboard event to the active window. 在VBA中,这始终充其量是狡猾的,但是您可以尝试使用Win API将键盘事件发送到活动窗口。 2 main points: 2个要点:

  • Make sure the IE window is the active window at the time of sending the key event (You can use AppActivate() or other API methods to do this) 在发送键事件时,请确保IE窗口是活动窗口(您可以使用AppActivate()或其他API方法来执行此操作)

  • You will need to know the virtual key code for the key(s) you want to 'press' here is a list of most of them 您将需要知道要“按”的键的虚拟键代码, 此处是其中大多数的列表。

Then something like this in a standard code module: 然后在标准代码模块中执行以下操作:

#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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM