Page 1 of 1

How do you use the keyboard keys in the form? Tutorial

Posted: Tue Aug 10, 2010 2:51 am
by harmeister
Hello everyone..

Have you ever wondered how to get your form to do something by pressing a button on the keyboard? Well problem solved!

See here is a code I use for my web browser for my address bar so when I press enter it navigates although this is not what you are looking for it is the same idea.
Code: Select all
    Private Sub ComboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown

        If e.KeyCode = Keys.Enter Then
            'Navigate to the current address.
            If ComboBox1.Text = My.Settings.Blocked(Form3.ListBox1.SelectedItem) Then
                MsgBox("This site has been blocked, to unblock it go under tools and blocked sites.", MsgBoxStyle.Critical)
            Else
                CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ComboBox1.Text)
            End If
            My.Settings.History.Add(ComboBox1.Text)
            ListBox2.Items.Clear()
            For Each Item As String In My.Settings.History
                ListBox2.Items.Add(Item)
                ComboBox1.Items.Add(Item)
            Next
            Me.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).DocumentTitle & " - Web Browser Elite"

            'Prevent the sound that indicates an invalid key press.
            e.SuppressKeyPress = True
        End If
    End Sub
Now as you can see it works and you can change the keys.enter to keys.space or keys.b or whatever you need.

Thanks for looking at this tutorial!

Re: How do you use the keyboard keys in the form? Tutorial

Posted: Tue Aug 10, 2010 3:28 am
by zachman61
good job
but these are called hotkeys

Re: How do you use the keyboard keys in the form? Tutorial

Posted: Tue Aug 10, 2010 4:00 am
by harmeister
oh they are? my bad.

Re: How do you use the keyboard keys in the form? Tutorial

Posted: Tue Aug 10, 2010 6:03 am
by zachman61
just saying so you can get more views on topic im not mad or nothing just telling cooll;

Re: How do you use the keyboard keys in the form? Tutorial

Posted: Wed Aug 11, 2010 10:15 pm
by harmeister
oh i know but I was just saying my bad because i didn't know

Re: How do you use the keyboard keys in the form? Tutorial

Posted: Sat Aug 14, 2010 12:14 am
by zachman61
oh ok
lol :D