שתף באמצעות


Delay between each character in sendkeys?

Question

Tuesday, October 31, 2017 10:56 PM

Hello,

How would I sendkeys with a slight delay between each character that's being sent from textbox1? So: it would "send" the first letter in the textbox (wait 200ms) second letter, and so on...

What I've tried:

I've tried experimenting with system.threading but can only get it working if I know what the character in the textbox are beforehand (the user - me - inputs these so this won't work). I've also tried implementing other timers but, a timer for each letter is very tedious. I tried.

Thanks.

All replies (12)

Tuesday, October 31, 2017 11:24 PM ✅Answered | 1 vote

 Use a System.Windows.Forms.Timer with the Interval property set to 200 milliseconds and send one character each time the Tick event is raised.  When the last character is sent,  stop the timer.

If you say it can`t be done then i`ll try it


Tuesday, October 31, 2017 11:54 PM ✅Answered | 2 votes

 Here is an example that should be close to what you have been doing in your last question or two.

Public Class Form1
    Private StringToSend As New System.Text.StringBuilder
    Private SpecialChars() As String = {"{", "}", "[", "]", "(", ")", "+", "^", "%"}

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.Text <> "" Then
            Button1.Enabled = False
            StringToSend.Append(TextBox1.Text)
            TextBox2.Select() 'i am sending the characters to TextBox2 so, i am setting focus to it before starting the timer
            Timer1.Interval = 200
            Timer1.Start()
        End If
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If StringToSend.Length > 0 Then
            Dim CharacterToSend As String = StringToSend(0) 'get the first character from the StringBuilder
            If SpecialChars.Contains(CharacterToSend) Then CharacterToSend = "{" & CharacterToSend & "}" 'add the parentheses around the character if it is a special character
            SendKeys.Send(CharacterToSend) 'send the character
            StringToSend.Remove(0, 1) 'remove the first character from the StringBuilder
        Else
            Timer1.Stop()
            Button1.Enabled = True
        End If
    End Sub
End Class

 

 

If you say it can`t be done then i`ll try it


Wednesday, November 1, 2017 2:09 AM ✅Answered | 1 vote

 You need the ELSE statement.  The code in the ELSE part gets ran when all the characters have been sent.  That is where you want to send the ENTER key and Append the TextBox.Text to the StringBuilder again.

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        If StringToSend.Length > 0 Then 'check if there are still any characters in the StringBuilder to be sent

            Dim CharacterToSend As String = StringToSend(0) 'get the first character from the StringBuilder
            If SpecialChars.Contains(CharacterToSend) Then CharacterToSend = "{" & CharacterToSend & "}" 'add the parentheses around the character if it is a special character
            SendKeys.Send(CharacterToSend) 'send the character
            StringToSend.Remove(0, 1) 'remove the first character from the StringBuilder (the one you just sent)

        Else 'all characters have been sent

            SendKeys.Send("{ENTER}") 'send the enter key
            StringToSend.Append(TextBox1.Text) 'append the text to the StringBuilder again

        End If

    End Sub

If you say it can`t be done then i`ll try it


Tuesday, October 31, 2017 11:43 PM

I was thinking of doing this when I was laying my dozens of timers. The thing is, however, I don't know how to push a single character at a time through. I'd know how if I knew what the characters were, but the user (me) inputs any characters in the textbox beforehand so I can't do it that way. I tried this:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim str As String
        str = TextBox1.Text
        SendKeys.Send(TextBox1.Text)
        SendKeys.Send("{enter}")
        For Each ch As Char In str
            SendKeys.Send(ch)
        Next
    End Sub

I don't know what's quite wrong with that.


Wednesday, November 1, 2017 12:09 AM

That's exactly what I want, except how do I make it so that once all the contents of the textbox has been sent, the enter key is pressed (SendKeys.Send("{enter}")) - and then it runs on a loop?

Thanks


Wednesday, November 1, 2017 12:20 AM | 1 vote

That's exactly what I want, except how do I make it so that once all the contents of the textbox has been sent, the enter key is pressed (SendKeys.Send("{enter}")) - and then it runs on a loop?

Thanks

 Well,  you know how to send an Enter key with the SendKeys.Send method,  right?  You see where i Stop the Timer and re-enable the Button after the last character has been sent,  you would just send the Enter key there.

** SendKeys.Send("{ENTER}")**

 If you want it to continue to do it over and over again,  then remove the line that Stops the timer.  Then send your Enter key and then Append the TextBox.Text to the StringBuilder.  Just let the Timer keep going.

If you say it can`t be done then i`ll try it


Wednesday, November 1, 2017 12:40 AM

That's what I tried to do before I sent my last message, it just presses enter after each sent character rather than waiting for the whole string to pass. :|

So it appears like this:

I

L

O

V

E

V

B


Wednesday, November 1, 2017 12:46 AM | 1 vote

That's what I tried to do before I sent my last message, it just presses enter after each sent character rather than waiting for the whole string to pass. :|

 Post the code that you tried so we can help you correct it.  It sounds like you are sending the Enter key on every tick of the timer in the If part of the IF THEN ELSE statement instead of in the ELSE part.

If you say it can`t be done then i`ll try it


Wednesday, November 1, 2017 12:57 AM

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If StringToSend.Length > 0 Then
            Dim CharacterToSend As String = StringToSend(0)
            If SpecialChars.Contains(CharacterToSend) Then CharacterToSend = "{" & CharacterToSend & "}"
            SendKeys.Send(CharacterToSend)
            StringToSend.Remove(0, 1)
            SendKeys.Send("{enter}")
        End If
    End Sub

I've ran it with and without the timer1.stop and the else statement. Thank you!


Wednesday, November 1, 2017 12:42 PM

Perfect! Problem solved!

Thank you!!!


Wednesday, November 1, 2017 4:48 PM | 1 vote

Perfect! Problem solved!

Thank you!!!

 You're Welcome.  8)

If you say it can`t be done then i`ll try it


Wednesday, November 1, 2017 5:02 PM | 2 votes

 Here is an example that should be close to what you have been doing in your last question or two.

 

If you say it can`t be done then i`ll try it

Nice one IronMan..

La vida loca