tags:

views:

44

answers:

2

How would I do the following in Visual Basic Express?

a) Press "delete", "home", and "shift" on the keyboard with the program. // Still need to figure out how to do this.
b) Detect when "z" and "x" are pressed. // I'm using buttons instead of this part now.

Thanks so much! :)

  • Windows Form Application
A: 

a) To send keyboard commands, you can use this SendKeys method.
b) To capture keystrokes check out this support article.

Dustin Laine
+2  A: 

Here is "ONE" way.. it detects the Enter press in .NET Win Forms. The 13 represents "Enter".

   Public Function KeyAscii(ByVal UserKeyArgument As KeyPressEventArgs) As Short
        KeyAscii = Asc(UserKeyArgument.KeyChar)
    End Function

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If KeyAscii(e) = 13 Then
            MsgBox("you press ENTER key")
        End If
    End Sub

More key types can be found by using something like this to detect your key presses.

Private Sub Form_KeyPress(KeyAscii As Integer)
    Debug.Print "KeyAscii: " & KeyAscii
End Sub

Private Sub Form_Load()
    Form1.KeyPreview = True
End Sub 
rockinthesixstring