简体   繁体   中英

Multiple questions for Visual Basic

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

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 

我认为SendKey函数可以满足您的需求。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM