Results 1 to 13 of 13

Thread: SendKeys - Hold Down a Key

  1. #1

    Thread Starter
    Addicted Member HunterTTP's Avatar
    Join Date
    Jul 2012
    Posts
    146

    Question SendKeys - Hold Down a Key

    Hey guys! It's been awhile. I've Googl(ed?) this for a few hours and can't seem to find a straight forward answer. I'm working on a program and I can't seem to figure out how to make SendKeys actually Hold Down a key.

    If I do this-

    Code:
    SendKeys.Send(W)
    Loop
    Then it literally spams the key as fast as possible. I just need it to hold down the key. I found this bit of code but I don't really understand how to utilize it-

    Code:
    Private Sub LetKeyGo(ByVal key As Byte)
    
    Const SPI_GETKEYBOARDDELAY = 22
        Const SPI_GETKEYBOARDSPEED = 10
        Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
        Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Integer, ByVal wMapType As Integer) As Integer
        Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Integer, ByVal uParam As Integer, ByRef lpvParam As Integer, ByVal fuWinIni As Integer) As Integer
    
            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
    Any help would be great! Thanks.
    Last edited by HunterTTP; Jan 12th, 2013 at 09:50 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: SendKeys - Hold Down a Key

    When you say that you need it to hold down the key, do you mean that you want to simulate the user depressing the key and then simulate the key being released after a pause? If so then, assuming that the code you posted there works, you would call HoldKeyDown (a better name would be DepressKey) and then Start a Timer with an appropriate Interval. When the TimerTicks, you Stop it and then call the LetKeyGo (a better name would be ReleaseKey). If you don't want a specific delay then don't use a Timer. Just call the methods on the Click of a Button or whatever.

    Note also that you will need to provide a VB.NET compatible declaration for the keybd_event and SystemParmetersInfo functions, which should be easy enough to find online, probably wherever you found that code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member HunterTTP's Avatar
    Join Date
    Jul 2012
    Posts
    146

    Re: SendKeys - Hold Down a Key

    Quote Originally Posted by jmcilhinney View Post
    When you say that you need it to hold down the key, do you mean that you want to simulate the user depressing the key and then simulate the key being released after a pause? If so then, assuming that the code you posted there works, you would call HoldKeyDown (a better name would be DepressKey) and then Start a Timer with an appropriate Interval. When the TimerTicks, you Stop it and then call the LetKeyGo (a better name would be ReleaseKey). If you don't want a specific delay then don't use a Timer. Just call the methods on the Click of a Button or whatever.

    Note also that you will need to provide a VB.NET compatible declaration for the keybd_event and SystemParmetersInfo functions, which should be easy enough to find online, probably wherever you found that code.
    Ahh, it seems like no matter what I do, I'm never specific enough. I'll learn one day

    Anyway, to make this an easier learning experience, I have setup a listbox and two buttons.

    The listbox has a bunch of keys listed so I can select the key that I want held down. Along with that I have 2 buttons, "On" and "Off".

    I simply need it to "Hold Down" the key I've selected in the list box when I press "On" and keep holding it until I press "Off".

  4. #4
    Member
    Join Date
    Jan 2013
    Posts
    40

    Re: SendKeys - Hold Down a Key

    dont know if this will help, i am only 4 days in newbie coder but i managed to get this working to turn my caps on and off but simulating keypress. intheory all you would have to do is split this as it is set to press down and then release if that makes sense:

    Code:
        Public Sub tmrCapsOff_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrCapsOff.Tick
    
            If Label3.Text <> "seal5" And My.Computer.Keyboard.CapsLock = True Then
                Call keybd_event(System.Windows.Forms.Keys.CapsLock, &H14, 1, 0)
                Call keybd_event(System.Windows.Forms.Keys.CapsLock, &H14, 3, 0)
            End If
    
        End Sub
    to make it work you do need this:
    Code:
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)

    so as u can see, to put the caps on it uses two lines so all u should need is to split them lines for when you want press down and release... i think..... hope it helps.

    Paul

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: SendKeys - Hold Down a Key

    Not sure this would actually work, but maybe try something like...

    Code:
    Public Class Form1
    
        Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
        Private Const KEYEVENTF_EXTENDEDKEY As Integer = &H1
        Private Const KEYEVENTF_KEYUP As Integer = &H2
    
        Private Sub iKeyDown(ByVal vKey As Keys)
            keybd_event(CByte(vKey), 0, KEYEVENTF_EXTENDEDKEY, 0)
        End Sub
    
        Private Sub iKeyUp(ByVal vKey As Keys)
            keybd_event(CByte(vKey), 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            'set focus on window that receive the key
            TextBox1.Focus()
            ' press the "C" key down
            iKeyDown(Keys.C)
            ' start timer to release the "C" key in 1 sec.
            Timer1.Interval = 1000
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            ' stop the timer!
            Timer1.Stop()
            ' release the "C" key!
            iKeyUp(Keys.C)
            ' show msg.
            MessageBox.Show("'C' key released.")
        End Sub
    
    End Class

  6. #6

    Thread Starter
    Addicted Member HunterTTP's Avatar
    Join Date
    Jul 2012
    Posts
    146

    Re: SendKeys - Hold Down a Key

    I've tried to do this as many ways as I can. I figured out how to use the code I posted originally. It doesn't show any errors but it simply doesn't work. Oh and I want this to simulate me holding down a key on my keyboard. So I want any application on my computer to be able to think that I'm constantly holding that key.

  7. #7
    Member
    Join Date
    Jan 2013
    Posts
    40

    Re: SendKeys - Hold Down a Key

    what will define the key release?

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: SendKeys - Hold Down a Key

    Quote Originally Posted by HunterTTP View Post
    I've tried to do this as many ways as I can. I figured out how to use the code I posted originally. It doesn't show any errors but it simply doesn't work. Oh and I want this to simulate me holding down a key on my keyboard. So I want any application on my computer to be able to think that I'm constantly holding that key.
    That doesn't actually make any sense. If you physically hold down a key, it repeats the relevant character continually until you stop, as in your Sendkey loop (the speed can obviously be regulated using a timer instead). If you hold down one of the modifiers (Ctrl, Alt etc.) it does nothing at all. There is nothing in between that simply says I'm holding this key but I don't want anything to happen as a result.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  9. #9

    Thread Starter
    Addicted Member HunterTTP's Avatar
    Join Date
    Jul 2012
    Posts
    146

    Arrow Re: SendKeys - Hold Down a Key

    Quote Originally Posted by dunfiddlin View Post
    That doesn't actually make any sense. If you physically hold down a key, it repeats the relevant character continually until you stop, as in your Sendkey loop (the speed can obviously be regulated using a timer instead). If you hold down one of the modifiers (Ctrl, Alt etc.) it does nothing at all. There is nothing in between that simply says I'm holding this key but I don't want anything to happen as a result.
    You actually make a fantastic point. I didn't think about it really. So if I do the send key loop, how long should I set the time between sends?

  10. #10
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: SendKeys - Hold Down a Key

    100ms will give you 10 per second which seems reasonable. Fastest available in Windows is around 30 per second, slowest around 3. Anything much faster than 10 will put you in less reliable timer country however.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  11. #11

    Thread Starter
    Addicted Member HunterTTP's Avatar
    Join Date
    Jul 2012
    Posts
    146

    Arrow Re: SendKeys - Hold Down a Key

    Quote Originally Posted by dunfiddlin View Post
    100ms will give you 10 per second which seems reasonable. Fastest available in Windows is around 30 per second, slowest around 3. Anything much faster than 10 will put you in less reliable timer country however.
    Alright well here is my code so far. The problem is that it freezes the program and sometimes even my computer. But it does everything else correctly. I was thinking about using a BackgroundWorker but how would I stop it once it started?

    Code:
    Imports System
    Imports System.IO
    Imports Microsoft.VisualBasic
    Public Class Form1
    
        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    
            Dim X As Integer
            X = 1
    
            If CheckBox1.Checked Then
                Do While X = 1
                    SendKeys.Send("W")
                    Threading.Thread.Sleep(25)
                Loop
            End If
    
        End Sub
    End Class

  12. #12
    Member
    Join Date
    Jan 2013
    Posts
    40

    Re: SendKeys - Hold Down a Key

    the sendkeys thing for me when i was doing caps used to freeze mine. i would suggest using my code above to toggle the key press down and up

    i think this should be it for you: (combination of yours and mine)
    Code:
    Imports System
    Imports System.IO
    Imports Microsoft.VisualBasic
    Public Class Form1
        Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    
            Dim X As Integer
            X = 1
    
            If CheckBox1.Checked Then
                Do While X = 1
                    Call keybd_event(System.Windows.Forms.Keys.W,  1, 0)
                    Threading.Thread.Sleep(25)
                Loop
            End If
    
        End Sub
    End Class
    im not 100% about this but some more experience guys may be able to clean it up a little

    good luck

  13. #13

    Thread Starter
    Addicted Member HunterTTP's Avatar
    Join Date
    Jul 2012
    Posts
    146

    Re: SendKeys - Hold Down a Key

    Quote Originally Posted by DaDDy ICEPOC View Post
    the sendkeys thing for me when i was doing caps used to freeze mine. i would suggest using my code above to toggle the key press down and up

    i think this should be it for you: (combination of yours and mine)
    Code:
    Imports System
    Imports System.IO
    Imports Microsoft.VisualBasic
    Public Class Form1
        Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    
            Dim X As Integer
            X = 1
    
            If CheckBox1.Checked Then
                Do While X = 1
                    Call keybd_event(System.Windows.Forms.Keys.W,  1, 0)
                    Threading.Thread.Sleep(25)
                Loop
            End If
    
        End Sub
    End Class
    im not 100% about this but some more experience guys may be able to clean it up a little

    good luck
    Thanks! But it gives me an error on Line 13. Looks like keybd_event not declared.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width