Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,085 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,791 people online right now. Registration is fast and FREE... Join Now!




how do I make button.click event perform using keyboard?

 
Reply to this topicStart new topic

how do I make button.click event perform using keyboard?

MrC
22 Jun, 2008 - 04:43 AM
Post #1

New D.I.C Head
*

Joined: 21 Jun, 2008
Posts: 14


My Contributions
hi all

I've been trying to make a form button click by using keys rather than mouseclicks. This is one way I have been trying to make it work but nothing is happening. Any ideas greatly appreciated.

C

Private Sub frmCalculator_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.NumPad0 Then
btn0.PerformClick()
End If
If e.KeyCode = Keys.NumPad1 Then
btn1.PerformClick()
End If
End Sub
User is offlineProfile CardPM
+Quote Post

WayneSpangler
RE: How Do I Make Button.click Event Perform Using Keyboard?
22 Jun, 2008 - 06:33 AM
Post #2

D.I.C Head
**

Joined: 22 Mar, 2008
Posts: 103



Thanked: 9 times
My Contributions
Make sure that the KeyPreview is set in the properties for the form.
Also I would suggest something like this soyour code does not have so many "If" statements.
CODE
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.NumPad0
                btn0.PerformClick()
            Case Keys.NumPad1
                btn1.PerformClick()
            Case Keys.NumPad2
                btn2.PerformClick()
            Case Keys.NumPad3
                btn3.PerformClick()
            Case Keys.NumPad4
                btn4.PerformClick()
            Case Keys.NumPad5
                btn5.PerformClick()
            Case Keys.NumPad6
                btn6.PerformClick()
            Case Keys.NumPad7
                btn7.PerformClick()
            Case Keys.NumPad8
                btn8.PerformClick()
            Case Keys.NumPad9
                btn9.PerformClick()
        End Select
    End Sub

User is offlineProfile CardPM
+Quote Post

Locke37
RE: How Do I Make Button.click Event Perform Using Keyboard?
22 Jun, 2008 - 06:40 AM
Post #3

Contributor of the Year
Group Icon

Joined: 20 Mar, 2008
Posts: 1,274



Thanked: 57 times
Dream Kudos: 325
My Contributions
Well, I think you can make shortcut keys for the number pad. Maybe I'm wrong.
User is offlineProfile CardPM
+Quote Post

MrC
RE: How Do I Make Button.click Event Perform Using Keyboard?
22 Jun, 2008 - 03:17 PM
Post #4

New D.I.C Head
*

Joined: 21 Jun, 2008
Posts: 14


My Contributions
QUOTE(WayneSpangler @ 22 Jun, 2008 - 07:33 AM) *

Make sure that the KeyPreview is set in the properties for the form.
Also I would suggest something like this soyour code does not have so many "If" statements.
CODE
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.NumPad0
                btn0.PerformClick()
            Case Keys.NumPad1
                btn1.PerformClick()
            Case Keys.NumPad2
                btn2.PerformClick()
            Case Keys.NumPad3
                btn3.PerformClick()
            Case Keys.NumPad4
                btn4.PerformClick()
            Case Keys.NumPad5
                btn5.PerformClick()
            Case Keys.NumPad6
                btn6.PerformClick()
            Case Keys.NumPad7
                btn7.PerformClick()
            Case Keys.NumPad8
                btn8.PerformClick()
            Case Keys.NumPad9
                btn9.PerformClick()
        End Select
    End Sub



thats brilliant, sorted it right out. my only problem now is that I want to set the enter button to something but the enter inputs the button in focus. I have tried setting enter as above and tried setting it as the acceptbutton but to no avail. mad.gif
User is offlineProfile CardPM
+Quote Post

WayneSpangler
RE: How Do I Make Button.click Event Perform Using Keyboard?
23 Jun, 2008 - 05:36 AM
Post #5

D.I.C Head
**

Joined: 22 Mar, 2008
Posts: 103



Thanked: 9 times
My Contributions
That is something I have never been able to do. Maybe someone else has an answer to that.
Sorry.
User is offlineProfile CardPM
+Quote Post

versus
RE: How Do I Make Button.click Event Perform Using Keyboard?
23 Jun, 2008 - 01:55 PM
Post #6

New D.I.C Head
*

Joined: 21 Jun, 2008
Posts: 20

here you go. first of all make a module and add this declare code:
CODE

    Public Declare Function GetASyncKeyState Lib "user32" Alias "GetASyncKeyState" (ByVal vKey As Long) As Integer


then you just use
CODE

if getasynckeystate(keynumber) then
do something
End if


here a list of the keys:
LButton (1)
RButton (2)
Cancel (3)
MButton (4)
Back (8)
Tab (9)
Clear (12)
Return (13)
Shift (16)
Control (17)
Menu (18)
Pause (19)
Capital (20)
Escape (27)
Space (32)
PageUp (33)
PageDown (34)
End (35)
Home (36)
Left (37)
Up (38)
Right (39)
Down (40)
Select (41)
Print (42)
Execute (43)
Snapshot (44)
Insert (45)
Delete (46)
Help (47)
Numlock (144)
ScrollLock (145)


Alphabetic Key Code Constants

A (65)
B (66)
C (67)
D (68)
E (69)
F (70)
G (71)
H (72)
I (73)
J (74)
K (75)
L (76)
M (77)
N (78)
O (79)
P (80)
Q (81)
R (82)
S (83)
T (84)
U (85)
V (86)
W (87)
X (88)
Y (89)
Z (90)



Numeric Key Constants


0 (48)
1 (49)
2 (50)
3 (51)
4 (52)
5 (53)
6 (54)
7 (55)
8 (56)
9 (57)


Numeric Keypad Key Constants

Numpad0 (96)
Numpad1 (97)
Numpad2 (98)
Numpad3 (99)
Numpad4 (100)
Numpad5 (101)
Numpad6 (102)
Numpad7 (103)
Numpad8 (104)
Numpad9 (105)
Multiply (106)
Add (107)
Separator (108)
Subtract (109)
Decimal (110)
Divide (111)



Function Key Constants

F1 (112)
F2 (113)
F3 (114)
F4 (115)
F5 (116)
F6 (117)
F7 (118)
F8 (119)
F9 (120)
F10 (121)
F11 (122)
F12 (123)
F13 (124)
F14 (125)
F15 (126)
F16 (127)

hope it was useful
heres a list of

This post has been edited by versus: 23 Jun, 2008 - 01:56 PM
User is offlineProfile CardPM
+Quote Post

ashura517
RE: How Do I Make Button.click Event Perform Using Keyboard?
24 Jun, 2008 - 07:00 PM
Post #7

New D.I.C Head
*

Joined: 10 Mar, 2008
Posts: 33


My Contributions
Hey yo..im having the same problem too. I cant seem to get the Enter key work.. i use Select Case statement too. How man??
User is offlineProfile CardPM
+Quote Post

WayneSpangler
RE: How Do I Make Button.click Event Perform Using Keyboard?
25 Jun, 2008 - 12:40 AM
Post #8

D.I.C Head
**

Joined: 22 Mar, 2008
Posts: 103



Thanked: 9 times
My Contributions
OK guys,
Thanks to Versus try this:

CODE
Public Class Form1
    Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Short

    Private Sub btnClicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click
        If GetAsyncKeyState(13) Then
            MessageBox.Show("Enter key pressed")
        Else
            MessageBox.Show(sender.name.ToString & " clicked")
        End If
    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.NumPad0
                btn0.PerformClick()
            Case Keys.NumPad1
                btn1.PerformClick()
            Case Keys.NumPad2
                btn2.PerformClick()
            Case Keys.NumPad3
                btn3.PerformClick()
        End Select
    End Sub
End Class

You will notice all my btn clicks all go to one sub routine called btnClicked and sender gets the name of the button.

This post has been edited by WayneSpangler: 25 Jun, 2008 - 12:42 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:42PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month