VB Hold Key Down

Posts 13 of 3 · Page 1 of 1
VB Hold Key Down
well, I kind of want to make a program that holds down keys while it clicks in another key.
What I want it to do is hold ctrl and ALT down while it clicks the down arrow.

Sendkeys.send doesn't work because it doesn't hold the key down. it just cycles through the button press. over and over again.

Any help would be appreciated.
C+P
Code:
Private Sub letkeygo(ByVal key As Byte)
            Dim kb_delay As Integer
            Dim kb_speed As Integer
            SystemParametersInfo(SPI_GETKEYBOARDDELAY, 0, kb_delay, 0)
            SystemParametersInfo(SPI_GETKEYBOARDSPEED, 0, kb_speed, 0)
            keybd_event(key, MapVirtualKey(key, 0), 2, 0)
        End Sub

        Private Sub HoldKeyDown(ByVal key As Byte)
            Dim kb_delay As Integer
            Dim kb_speed As Integer
            SystemParametersInfo(SPI_GETKEYBOARDDELAY, 0, kb_delay, 0)
            SystemParametersInfo(SPI_GETKEYBOARDSPEED, 0, kb_speed, 0)
            keybd_event(key, MapVirtualKey(key, 0), 0, 0)
        End sub
The above is NOT the way to do it. Use the keybd_event API and signal all the keydowns before you signal the keyup. Here's some generic methods to get you started.

Code:
<System.Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function keybd_event(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As UInt32, ByVal dwExtraInfo As UIntPtr) As Boolean
End Function

Private Sub InvokeKeydown(ByVal key As Keys)
    Dim vk As Byte = CType(key, Byte)
    keybd_event(vk, 0, 0, UIntPtr.Zero)
End Sub

Private Sub InvokeKeyup(ByVal key As Keys)
    Dim vk As Byte = CType(key, Byte)
    keybd_event(vk, 0, 2, UIntPtr.Zero)
End Sub

Private Sub InvokeKeypress(ByVal key As Keys, Optional ByVal holdLength As Integer = 10)
    InvokeKeydown(key)
    Threading.Thread.Sleep(holdLength)
    InvokeKeyup(key)
End Sub
So, call InvokeKeydown on the first key, then on the second and finally the third. Then InvokeKeyup on them in reverse order.
Posts 13 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?