vb.net – VB按住按键

vb.net – VB按住按键,第1张

概述我正在创建一个宏程序来记录和播放鼠标和键盘输入.录制工作正常,鼠标播放也是如此,但我在播放键盘输入时遇到了麻烦 – 特别是在释放前按住键几秒钟.这不等于重复按键.这是我尝试过的: 技巧1:Me.KeyDown Private Sub keyboard_pressed() Handles Me.KeyDown Dim keypress = e.KeyData MsgB 我正在创建一个宏程序来记录和播放鼠标和键盘输入.录制工作正常,鼠标播放也是如此,但我在播放键盘输入时遇到了麻烦 – 特别是在释放前按住键几秒钟.这不等于重复按键.这是我尝试过的:

技巧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:SendKeys

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,keydown,0) End Sub

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

有人可以告诉我如何才能实现新闻发布会按住左箭头键几秒钟,然后释放.

解决方法 几年前,不推荐使用keybd_event和mouse_event函数.相反,你应该使用 SendInput() function.

使用.NET模拟输入有时候有点棘手,幸运的是我写了一个名为inputHelper(Download from GitHub)的库,它是sendinput()的包装器.我已经对它进行了定制,以便它涵盖了许多不同的输入处理和输入模拟方法,主要包括:

>模拟击键(内部使用sendinput()).
>模拟鼠标移动和鼠标按钮单击(内部也使用sendinput()).
>将虚拟击键和鼠标点击发送到当前/特定窗口(内部使用Window Messages).
>创建全局,低级鼠标和键盘挂钩.

不幸的是我还没有时间写一篇关于这个的正确文档/ wiki(除了Visual Studio的IntelliSense显示的库的每个成员的XML文档),但是到目前为止你可以找到一些关于它的信息.在project’s wiki上创建钩子.

该库包含的简短描述:

> inputHelper.Hooks

用于创建全局,低级鼠标/键盘挂钩(使用SetWindowsHookEx()和其他相关方法).这部分涵盖在wiki中.
> inputHelper.Keyboard

用于处理/模拟物理键盘输入(利用sendinput()和GetAsyncKeyState()).
> inputHelper.Mouse

用于处理/模拟物理鼠标输入(利用sendinput()).
> inputHelper.WindowMessages

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

发送击键

发送“物理”击键可以通过两个功能完成:

> inputHelper.Keyboard.PressKey(键为键,可选HarDWareKey为布尔值)

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

If HarDWareKey is set,the function will send the key’s 07009 instead of its 070010 (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 TrueEnd Function

用法示例:

'Holds down 'A' for 5 seconds,repeating it every 50 milliseconds.HoldKeyFor(Keys.A,5000,50)
总结

以上是内存溢出为你收集整理的vb.net – VB按住按键全部内容,希望文章能够帮你解决vb.net – VB按住按键所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1250632.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存