Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TuT] Trigger a Button With a Key
#1
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
Reply
#2
Imma go try this. I am pretty new and still learning.
Reply
#3
Wow very nice toturials this helped me alot thanks.. Big Grin
Reply
#4

(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.
Reply
#5
Nice broBig Grin
I never knew about this.
What bout space button?
Reply
#6
Nice tutorial easy and good for people who just started on Visual basic
My profession, you can call me a pothead, 'nuff said
Reply
#7
also what about shift or numbers
Reply
#8
(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
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
[Image: t5BWm.png]
Reply
#9
(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.
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Encrypt String using x509Certificate private Key wih RSA jeffstan 0 2,434 01-26-2014, 04:18 PM
Last Post: jeffstan
  [TUT] How to make a simple WebBrowser in VB 2010! [TUT] - [ Pictures] Statics 95 61,907 10-07-2012, 06:56 AM
Last Post: a99
  Good Button :) HF~Legend 5 2,067 09-28-2012, 01:21 AM
Last Post: rqmok
  Button firefox Menu HF~Legend 2 1,524 09-08-2012, 04:20 PM
Last Post: spesificrelax
  [TUT]Auto-Update System[TUT] HB Virus 3 2,428 01-07-2012, 02:21 PM
Last Post: Mastermrz

Forum Jump:


Users browsing this thread: 2 Guest(s)