none
请问一下VB.NET怎么实现模拟长时间键盘按键? RRS feed

  • 问题

  • 有一个需求要按住W键几秒,之后按住O键几秒。

    我试了一下sendkeys不能保持按住不放的效果。

    短时间反复按是不行的,必须像人手工按住一样一直按才行。

    请教一下各位这个怎么实现?谢谢。

    2019年11月10日 12:20

全部回复

  • SendInput

    Sleep

    SendInput KEYEVENTF_KEYUP



    Visual C++ MVP

    2019年11月10日 19:56
    版主
  • 有一个需求要按住W键几秒,之后按住O键几秒。

    我试了一下sendkeys不能保持按住不放的效果。

    短时间反复按是不行的,必须像人手工按住一样一直按才行。

    请教一下各位这个怎么实现?谢谢。

    Hi  SaulChow,

    你可以参考开源项目InputHelper, 并使用下面的代码方便的实现模拟长时间键盘按键(下载引用库)。

       Private Sub TextBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles TextBox1.MouseDown
            'Holds down 'A' for 5 seconds, repeating it every 50 milliseconds.
            HoldKeyFor(Keys.A, 5000, 50)
        End Sub
    
    
        '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
    


    Best regards

    Yong Lu



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年11月11日 3:32
    版主