vb.net – VB按住按键
我正在创建一个宏程序来记录和播放鼠标和键盘输入.录制工作正常,鼠标播放也是如此,但我在播放键盘输入时遇到了麻烦 – 特别是在释放前按住键几秒钟.这不等于重复按键.这是我尝试过的:
技巧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,keydown,0) End Sub
有人可以告诉我如何才能实现新闻发布会按住左箭头键几秒钟,然后释放. 解决方法
几年前,不推荐使用keybd_event和mouse_event函数.相反,你应该使用
SendInput() function.
使用.NET模拟输入有时候有点棘手,幸运的是我写了一个名为InputHelper(Download from GitHub)的库,它是SendInput()的包装器.我已经对它进行了定制,以便它涵盖了许多不同的输入处理和输入模拟方法,主要包括: >模拟击键(内部使用SendInput()). 不幸的是我还没有时间写一篇关于这个的正确文档/ wiki(除了Visual Studio的IntelliSense显示的库的每个成员的XML文档),但是到目前为止你可以找到一些关于它的信息.在project’s wiki上创建钩子. 该库包含的简短描述: > InputHelper.Hooks 用于创建全局,低级鼠标/键盘挂钩(使用 用于处理/模拟物理键盘输入(利用SendInput()和 用于处理/模拟物理鼠标输入(利用SendInput()). 用于处理/模拟虚拟鼠标/键盘输入,例如到特定窗口(利用 发送击键 发送“物理”击键可以通过两个功能完成: > InputHelper.Keyboard.PressKey(键为键,可选HardwareKey为布尔值)
> InputHelper.Keyboard.SetKeyState(Key As Keys,KeyDown As Boolean,Optional HardwareKey As Boolean)
你会使用后者,因为你想控制你想要按下键多长时间. 按住键指定的时间 为了做到这一点,你需要使用某种计时器,就像你已经做的那样.然而,为了让事情变得更有活力,我写了一个函数,它可以让你指定要按住哪个键,以及多长时间. '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) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |