KeyDown/ Shortcut Key- Help!

TheLaw

Member
Joined
Oct 30, 2010
Messages
13
Programming Experience
Beginner
Hi,

I'm working on a simple calculator program for work in VB.NET and I'd like it that an operation could be performed without a mouse click and instead by using the Enter key.

So essentially I want the segment of the program that is usually run by button to have the ability to be run by the Enter key

I'm using VS2010.

This is what I was trying to do but I suppose I'm not doing it correctly because the Enter key does not do anything when I press it.

VB.NET:
Private Sub btnCalc_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles btnCalc.KeyDown
        Select Case e.KeyCode
            Case Keys.Enter
        End Select
    End Sub

Sorry I am still new to this stuff. Thanks.
 
You should code against the Form. In the Form's KeyDown or KeyPress event add the select case statement for all keystrokes you want to perform something after being pressed.
e.g.
Select Case e.KeyCode
Case Keys.Enter
btnCalc.PerformClick()
etc. etc.


However the btnCalc should perform some math operation on click.
 
I've used the 'Esc' key before to close the form and the code should be the same for your enter:

VB.NET:
    Private Sub frm_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode.ToString = "Escape" Then Me.Hide()
    End Sub
so for you would be: If e.keyCode.ToString = "Enter" Then DoMyCalcullations()

just remember that for this to work you have to set the "KeyPreview" property of your form to True.
 
Back
Top