Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Keyboard Imput Detection and Action 2

Status
Not open for further replies.

inlandpac

Programmer
Jun 18, 2000
235
US
I am working on my first vb app and have run into a stickler.

I have a form frmCalculator. This form is a calculator that allows for numbers and operations to be entered either by clicking on the buttons in the form or by keystroke. I do not want to require focus in the number display text field in order for the user to use they keyboard to enter number.

I tried using the following as a test, but when I hit a key, the message popup display doesn't appear.

I tried different renditions of the code below as well as the code after.

Code:
Private Sub frmCalculator_KeyPress(ByVal sender As System.Object, _
    ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
    Select Case sender
        Case 37   'Left arrow
            MessageBox.Show("left")
        Case 38   'Up arrow
            MessageBox.Show("up")
        Case 39   'Right arrow
            MessageBox.Show("right")
        Case 40   'Down arrow
            MessageBox.Show("down")
    End Select
End Sub

and also

Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
     Select Case KeyCode
          Case 37   'Left arrow
               imgMain.Left = imgMain.Left - 40
          Case 38   'Up arrow
               imgMain.Top = imgMain.Top - 40
          Case 39   'Right arrow
               imgMain.Left = imgMain.Left + 40
          Case 40   'Down arrow
               imgMain.Top = imgMain.Top + 40
     End Select
End Sub

ICQ: 54380631
online.dll
 
first --- the keydown handles nothing looks like vb6 code or even access code so try again.

Second --- the sender will return the control that invoked the handlernot the key that was preseed. That should be found in the e eventargs something like e.keychar.

and if you want this to work you should probably set keypreview to true (this is a form property)

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Set KeyPreview to True (as Christiaan Baes suggested)

In order to capture the arrow keys you'll need to override the ProcessCmdKey function of the form.

The Following code will allows you catch any key, and process it. Returns true if processed, otherwise returns false.

Code:
    Protected Overrides Function ProcessCmdKey(ByRef _
 msg  As Message, ByVal keyData As Keys) As Boolean

        Dim bHandled As Boolean = False

        Select Case keyData
            Case Keys.Right
                MsgBox("right")
                bHandled = True
            Case Keys.Left
                MsgBox("left")
                bHandled = True
            Case Keys.Up
                MsgBox("up")
                bHandled = True
            Case Keys.Down
                MsgBox("down")
                bHandled = True
        End Select
        Return bHandled
    End Function


-+{John Vogel}+-


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top