- 1). Open the project that must respond to keyboard input in Visual Studio.
- 2). Add a TextBox to the design surface. Click on the "Events" icon in the Properties menu while the TextBox is selected. Double-click on the KeyPress event. Visual Studio will automatically create an empty subroutine that combines the name of the TextBox and the KeyPress event:
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
End Sub - 3). Add code as required within the subroutine. Check, for example, to see if the key pressed was the backspace:
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyCode <> Keys.Tab Then
'create appropriate response for your application to backspace event
End Sub - 4). Access any key pressed by using the members of the Keys Enumeration class. Access a particular key by typing the String "Key" followed by a period and the member name as specified in the Keys Enumeration class:
'Return
Keys.Return
'Control key
Keys.Control
'The letter "K"
Keys.K - 5). Save your work and hit F5 to debug it. Test the application to ensure it works as expected.
SHARE