Support Forums
[TuT] Trigger a Button With a Key - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Visual Basic and the .NET Framework (https://www.supportforums.net/forumdisplay.php?fid=19)
+---- Thread: [TuT] Trigger a Button With a Key (/showthread.php?tid=20043)

Pages: 1 2


[TuT] Trigger a Button With a Key - Black Ghost - 06-29-2011

1. Make a new application.
2. Double click anywhere on your program so you get to the code.
3. Now there should be a drop down menu in the top right corner, select "Key_Down" from that menu.
4. Now place this code there:
Code:
If e.Keys = KeyCode.Enter Then
Button1.Perform_Click
End if

You can change "Button1" to whatever your button is named as.

Enjoy Smile


RE: [TuT] Trigger a Button With a Key - ily - 06-29-2011

Imma go try this. I am pretty new and still learning.


RE: [TuT] Trigger a Button With a Key - H-Q - 06-29-2011

Wow very nice toturials this helped me alot thanks.. Big Grin


RE: [TuT] Trigger a Button With a Key - ily - 06-29-2011


(06-29-2011, 09:43 AM)lqp Wrote: Thanks man just keep up the lqp work xD

Quit trolling. Is it worth getting the Support Feather? Mr. HF Staff.


RE: [TuT] Trigger a Button With a Key - Injection - 07-01-2011

Nice broBig Grin
I never knew about this.
What bout space button?


RE: [TuT] Trigger a Button With a Key - Firetech - 07-01-2011

Nice tutorial easy and good for people who just started on Visual basic


RE: [TuT] Trigger a Button With a Key - Injection - 07-01-2011

also what about shift or numbers


RE: [TuT] Trigger a Button With a Key - Resistance - 07-01-2011

(07-01-2011, 03:36 PM)Injection Wrote: also what about shift or numbers
SPACE
If e.KeyCode = Keys.Space Then
'Statement of Action
End If
ANY SHIFT KEY
If e.KeyCode = Keys.ShiftKey Then 'This ShiftKey stands for any of the shifts keys.
'Statement of Action
End If
NUMBER 1
If e.KeyCode = Keys.D1 Then 'Substitute the number one with any singlet digit number availible on your key board.
'Statement of Action
End If
NUMBER 6
If e.KeyCode = Keys.D6 Then 'For this example, we are using number 6.
'Statement of Action
End If

ENJOY DUDE
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


RE: [TuT] Trigger a Button With a Key - H-Q - 07-02-2011

(06-29-2011, 09:45 AM)ily Wrote: Quit trolling. Is it worth getting the Support Feather? Mr. HF Staff.
Yes, that's why you have your account Closed.


RE: [TuT] Trigger a Button With a Key - AceInfinity - 07-03-2011

It's not:
Code:
Button1.Perform_Click

It is:
Code:
Button1.PerformClick()

Also, you forgot to mention that the value needs to be:
Code:
System.Windows.Forms.KeyEventArgs

And not the usual:
Code:
System.EventArgs

Otherwise it won't work.

And also, it should be KeyData instead of KeyCode Otherwise keys in combination with Enter will also activate the button click event. KeyData ensures that it only happens when enter is pressed alone, so that something like {Shift} + {Enter} will not work to activate the button click, whereas if you had KeyCode it will...

You also got the first part of your code mixed up a bit, instead it should be:
Code:
If e.KeyCode = Keys.Enter Then

Not
Code:
If e.Keys = KeyCode.Enter Then

But the preferred code for one keydown event should be this in my opinion:
Code:
If e.KeyData = Keys.Enter Then
    Button1.PerformClick()
End If

Full thing:
Code:
Private Sub Something_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles SomeControl.KeyDown
        If e.KeyData = Keys.A Then
            Button1.PerformClick()
        End If
    End Sub

Take note that ByVal e is System.Windows.Forms.KeyEventArgs. If e was System.EventArgs, which is the default, then it will not work.

An Example of a Multiple Keydown event would be:
Code:
If e.Control = True AndAlso e.Alt AndAlso e.KeyCode = Keys.T Then
    Button1.PerformClick()
End If

That will work when the Ctrl, Alt and "T" key on the keyboard are pressed in combination.