我正在创建一个宏程序来录制和播放鼠标和键盘输入。录制工作正常,鼠标播放也是如此,但我在播放键盘输入时遇到了麻烦 - 特别是在释放前按住键几秒钟。这不等于重复按键。这就是我的尝试:
技术1:Me.KeyDown
Private Sub keyboard_pressed() Handles Me.KeyDown
Dim keypress = e.KeyData
MsgBox(keypress)
End Sub
仅在窗口处于焦点时才有效。
技术2:SendKeys
Private Sub timer_keyboardplayback_Tick() Handles timer_playback.Tick
SendKeys.Send("{LEFT}")
timer_playback.Interval = 30
End Sub
失焦,但重复按左箭头而不是按住箭头
技术3:keybd_event
Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Sub timer_keyboardplayback_Tick() Handles timer_playback.Tick
Const keydown = &H1
Const keyup = &H2
Dim VK_LEFT = 37
keybd_event(VK_LEFT, 0, keydown, 0)
End Sub
失焦,但仍无法按住箭头
有人可以告诉我如何才能实现新闻发布会按住左箭头键几秒钟,然后释放。
答案 0 :(得分:1)
几年前,keybd_event
和mouse_event
函数已被弃用。相反,您应该使用SendInput()
function。
使用.NET模拟输入有时会有点棘手,幸运的是我已经编写了一个名为 InputHelper (Download from GitHub)的库,它是{{Window Messages的包装器。 1}}。我已对其进行了定制,以便涵盖输入处理和输入模拟的许多不同方式,主要包括:
SendInput()
)。SendInput()
)。不幸的是,我还没有时间写一篇关于此的正确文档/ wiki(除了Visual Studio的IntelliSense显示的每个库成员的XML文档),但是到目前为止,您可以在SetWindowsHookEx()
上找到有关创建挂钩的一些信息。
该库包含的简短描述:
<强> SendInput()
强>
用于创建全局的低级鼠标/键盘挂钩(利用wiki和其他相关方法)。这部分涵盖在GetAsyncKeyState()
。
<强> InputHelper.Hooks
强>
用于处理/模拟物理键盘输入(使用InputHelper.Keyboard
和SendMessage()
)。
<强> SendInput()
强>
用于处理/模拟物理鼠标输入(使用InputHelper.Mouse
)。
<强> SendInput()
强>
用于处理/模拟虚拟鼠标/键盘输入,例如用于特定窗口(利用PostMessage()
和Scan Code)。
发送&#34;物理&#34;按键可以通过两个功能完成:
<强> InputHelper.WindowMessages
强>
发送指定键的两次击键(向下和向上)。
如果设置了
InputHelper.Keyboard.PressKey(Key As Keys, Optional HardwareKey As Boolean)
,则该功能会发送密钥的Virtual Key Code而不是{{3}} (默认为HardwareKey
)
<强> False
强>
发送指定键的单击键。
如果
InputHelper.Keyboard.SetKeyState(Key As Keys, KeyDown As Boolean, Optional HardwareKey As Boolean)
KeyDown
,密钥将作为KEYDOWN事件发送,否则为KEYUP。
True
与上述相同。
您使用后者,因为您想要控制按键的持续时间。
为了做到这一点,你需要使用某种计时器,就像你已经做的那样。然而,为了让事情变得更有活力,我已经编写了一个功能,可以指定要按住哪个键,以及持续多长时间。
HardwareKey
使用示例:
'Lookup table for the currently held keys.
Private HeldKeys As New Dictionary(Of Keys, Tuple(Of Timer, Timer))
''' <summary>
''' Holds down (and repeats, if specified) the specified key for a certain amount of time.
''' Returns False if the specified key is already being held down.
''' </summary>
''' <param name="Key">The key to hold down.</param>
''' <param name="Time">The amount of time (in milliseconds) to hold the key down for.</param>
''' <param name="RepeatInterval">How often to repeat the key press (in milliseconds, -1 = do not repeat).</param>
''' <remarks></remarks>
Public Function HoldKeyFor(ByVal Key As Keys, ByVal Time As Integer, Optional ByVal RepeatInterval As Integer = -1) As Boolean
If HeldKeys.ContainsKey(Key) = True Then Return False
Dim WaitTimer As New Timer With {.Interval = Time}
Dim RepeatTimer As Timer = Nothing
If RepeatInterval > 0 Then
RepeatTimer = New Timer With {.Interval = RepeatInterval}
'Handler for the repeat timer's tick event.
AddHandler RepeatTimer.Tick, _
Sub(tsender As Object, te As EventArgs)
InputHelper.Keyboard.SetKeyState(Key, True) 'True = Key down.
End Sub
End If
'Handler for the wait timer's tick event.
AddHandler WaitTimer.Tick, _
Sub(tsender As Object, te As EventArgs)
InputHelper.Keyboard.SetKeyState(Key, False) 'False = Key up.
WaitTimer.Stop()
WaitTimer.Dispose()
If RepeatTimer IsNot Nothing Then
RepeatTimer.Stop()
RepeatTimer.Dispose()
End If
HeldKeys.Remove(Key)
End Sub
'Add the current key to our lookup table.
HeldKeys.Add(Key, New Tuple(Of Timer, Timer)(WaitTimer, RepeatTimer))
WaitTimer.Start()
If RepeatTimer IsNot Nothing Then RepeatTimer.Start()
'Initial key press.
InputHelper.Keyboard.SetKeyState(Key, True)
Return True
End Function