问题
I have an app in visual basic that displays a message box with a text range from A - F upon user integer input in a textbox.
I have a button control on the form that triggers the event, but the user has to press the button every time to get a result, that's cool, but what if I wanted the event triggered by say the B
key on my keyboard?
I know of a way of assigning the AcceptButton
property to my button in visual studio 2012 which accepts just the Enter
key alone. I want the same effect as pressing the button on the press of a key on a keyboard instead. Thanks.
回答1:
Use the form Keydown event, and check to see if the B key was pressed. If so, display the messagebox.
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.B Then MsgBox("B key was pressed")
End Sub
来源:https://stackoverflow.com/questions/23176009/how-do-i-trigger-an-event-on-the-press-of-a-key-in-vb