gpt4 book ai didi

vb.net - VB 按住按键

转载 作者:行者123 更新时间:2023-12-04 21:32:11 31 4
gpt4 key购买 nike

我正在创建一个宏程序来记录和回放鼠标和键盘输入。录制效果很好,鼠标播放也一样,但是我在播放键盘输入时遇到了麻烦——特别是在释放之前按住一个键几秒钟。这不等同于重复按键。这是我尝试过的:

技巧 1:Me.KeyDown

 Private Sub keyboard_pressed() Handles Me.KeyDown
Dim keypress = e.KeyData
MsgBox(keypress)
End Sub

Only works when window is in focus.



技巧 2:发送 key
 Private Sub timer_keyboardplayback_Tick() Handles timer_playback.Tick
SendKeys.Send("{LEFT}")
timer_playback.Interval = 30
End Sub

Works out of focus, but repetitively presses left arrow rather than press and hold arrow



技术 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

Works out of focus, but still fails to press hold arrow



有人可以告诉我如何实现按住左箭头键几秒钟,然后松开。

最佳答案

keybd_eventmouse_event功能在几年前已被弃用。相反,您应该使用 SendInput() function .

使用它从 .NET 模拟输入有时会有点棘手,幸运的是,尽管我编写了一个名为 InputHelper ( Download from GitHub ) 的库,它是 SendInput() 的包装器。 .我已经对其进行了定制,使其涵盖了输入处理和输入模拟的许多不同方式中的一些,主要是:

  • 模拟击键(内部使用 SendInput() )。
  • 模拟鼠标移动和鼠标按钮点击(也在内部使用 SendInput())。
  • 将虚拟击键和鼠标点击发送到当前/特定窗口(内部使用 Window Messages )。
  • 创建全局的低级鼠标和键盘 Hook 。

  • 不幸的是,我还没有时间为此编写适当的文档/wiki(除了库中每个成员的 XML 文档,由 Visual Studio 的 IntelliSense 显示),但到目前为止,您可以找到一些关于在 project's wiki 上创建 Hook .

    该库包含的内容的简短描述:
  • InputHelper.Hooks

    用于创建全局、低级鼠标/键盘 Hook (使用 SetWindowsHookEx() 和其他相关方法)。这在 wiki 中有部分内容。 .
  • InputHelper.Keyboard

    用于处理/模拟物理键盘输入(使用 SendInput() GetAsyncKeyState() )。
  • InputHelper.Mouse

    用于处理/模拟物理鼠标输入(使用 SendInput() )。
  • InputHelper.WindowMessages

    用于处理/模拟虚拟鼠标/键盘输入,例如特定窗口(使用 SendMessage() PostMessage() )。


  • 发送击键

    发送“物理”击键可以通过两个功能完成:
  • InputHelper.Keyboard.PressKey(Key As Keys, Optional HardwareKey As Boolean)

    Sends two keystrokes (down and up) of the specified key.

    If HardwareKey is set, the function will send the key's Scan Code instead of its Virtual Key Code (default is False).

  • InputHelper.Keyboard.SetKeyState(Key As Keys, KeyDown As Boolean, Optional HardwareKey As Boolean)

    Sends a single keystroke of the specified key.

    If KeyDown is True the key will be sent as a KEYDOWN event, otherwise KEYUP.

    HardwareKey is the same as above.


  • 你会使用后者,因为你想控制你想要按住键的时间。

    按住一个键指定的时间

    为了做到这一点,你需要使用某种计时器,就像你已经做的那样。然而,为了让事情更有活力,我编写了一个函数,可以让你指定按住哪个键,以及按住多长时间。
    '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

    用法示例:
    'Holds down 'A' for 5 seconds, repeating it every 50 milliseconds.
    HoldKeyFor(Keys.A, 5000, 50)

    关于vb.net - VB 按住按键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48382704/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com