How to use the Sendkeys Command!

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
3 posts Page 1 of 1
Contributors
User avatar
2cool4cereal2
VIP - Donator
VIP - Donator
Posts: 151
Joined: Thu Oct 14, 2010 3:26 am

****ALL CODING DONE BY ME IS IN VB 2010!****



Ok, I figured since I'm a new member, it'd be good to share a little of my knowledge that took a bit of google-ing to find. This tutorial covers how to create a program that uses the Sendkeys command.

Start off by Adding one button and three check boxes to your program.
Image

Next, click your button and add this code:
Code: Select all
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            If CheckBox1.Checked Then

                Dim retVal As Object
                retVal = Shell("Notepad", vbNormalFocus)
                SendKeys.Send("10101010101010101010110101010101010101010101010101001")

            ElseIf CheckBox2.Checked Then
                If MsgBox("You are about to send 10 on a repeating loop, the only way to stop it is by forcing the program to shut down. Continue?", _
                                   MsgBoxStyle.YesNo, "Notepad Loop") = Windows.Forms.DialogResult.Yes Then
H:
                    SendKeys.Send("10101010101010101010110101010101010101010101010101001")
                    GoTo H
                Else
                    SendKeys.Send("10101010101010101010110101010101010101010101010101001")
                End If
                End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        If CheckBox3.Checked Then
            Try
                If MsgBox("You are about to send 10 to notepad on a repeating loop, the only way to stop it is by forcing the program to shut down. Continue?", _
                                   MsgBoxStyle.YesNo, "Notepad Loop") = Windows.Forms.DialogResult.Yes Then

                    Dim retVal As Object
                    retVal = Shell("Notepad", vbNormalFocus)
J:
                    SendKeys.Send("10101010101010101010110101010101010101010101010101001")
                    GoTo J
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If

    End Sub
Here's a bit of an explanation of the code above:
Once you click on Button1, the program will look to see which check box is checked and will do the code of the checked one.

Let's continue with our coding before further explanation:

Ok, now, double click on your first checkbox to edit its code. (This check box's name should be "Open Notepad (Recommended)")
Add this code to checkbox1:
Code: Select all
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked Then
            CheckBox3.Checked = False
            CheckBox3.Enabled = False
            CheckBox2.Checked = False
            CheckBox2.Enabled = False
        Else
            CheckBox3.Enabled = True
            CheckBox2.Enabled = True
        End If
    End Sub
Next, double click on Checbox2 and add the following code: (Name this box "Loop")
Code: Select all
 Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
        If CheckBox2.Checked Then
            CheckBox1.Checked = False
            CheckBox1.Enabled = False
            CheckBox3.Checked = False
            CheckBox3.Enabled = False
        Else
            CheckBox1.Enabled = True
            CheckBox3.Enabled = True
        End If
    End Sub
Finally, add a third button along with this code: (Name this button "Start Open Notepad and Loop")
Code: Select all
    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
        If CheckBox3.Checked Then
            CheckBox1.Checked = False
            CheckBox1.Enabled = False
            CheckBox2.Checked = False
            CheckBox2.Enabled = False
        Else
            CheckBox1.Enabled = True
            CheckBox2.Enabled = True
        End If
    End Sub
You should now have fully functional checkboxes and button.
I'll now explain the entire coding of the button.

When you click button1, it the program will check to see which checkbox is selected, if none are selected nothing will happen. If checkbox1 is selected, however, the program will open notepad and type "10101010101010101010110101010101010101010101010101001" using sendkeys.

(Notepad is opened using
Code: Select all
Dim retVal As Object
                    retVal = Shell("Notepad", vbNormalFocus)
This will open notepad and focus on it.)

If checkbox2 is selected, then the program will 'loop' "10101010101010101010110101010101010101010101010101001"
over and over again until the program is shut down.

Finally, if checkbox3 is selected, the program will open notepad and loop "10101010101010101010110101010101010101010101010101001"
over and over again.

By editing this code on each button, you can change what each one says:
Code: Select all
If CheckBox1.Checked Then
SendKeys.Send("YOUR MESSAGE")
I know the title says using the sendkeys command, but this tutorial also covered opening and focusing on a program using
Code: Select all
Dim retVal As Object
                    retVal = Shell("Notepad", vbNormalFocus)
I hope you enjoyed this tutorial, and if you have questions or want more pictures, etc, just ask! :)

P.s. The download contains a royalty-free icon, completely safe for free use on any project including this one! :D
You do not have the required permissions to view the files attached to this post.
Last edited by 2cool4cereal2 on Sat Oct 16, 2010 9:39 pm, edited 1 time in total.
"I try to be modest at all times, and that's what makes me better than everyone else."
Image
Image
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: How to use the Sendkeys Command!
mandai
I think it would be good to add a part that checks whether notepad has focus during the sendkeys loop.
User avatar
2cool4cereal2
VIP - Donator
VIP - Donator
Posts: 151
Joined: Thu Oct 14, 2010 3:26 am

mandai wrote:
I think it would be good to add a part that checks whether notepad has focus during the sendkeys loop.
Hmmm, that does sound interesting. Maybe I'll look into that. But, really, this isn't a very serious project, this was just a joke to leave running on my brother's computer to freak him out.
I bet there are a lot of features to be added. Thanks for your idea. :)
Last bumped by 2cool4cereal2 on Thu Nov 25, 2010 1:55 am.
"I try to be modest at all times, and that's what makes me better than everyone else."
Image
Image
Image
3 posts Page 1 of 1
Return to “Tutorials”