How to get ASCII from KeyEventArgs

lchicaiz

New member
Joined
Jan 27, 2004
Messages
2
Hi everybody

I need to know when user types the underline character ( _, ASCII code 95), but when I receive the events KeyDown, KeyPress or KeyDown, all that I get is key codes (i.e. OemMinus, code 189).

I know that when the OemMinus is pressed and the "Shift" key is also pressed, then the user is typing the underline character, but I want a more general method ( API? ) to translate any KeyData / KeyCode or KeyValue into the ASCII code of the character typed by user.

Thanks in advance

Luis B. Chicaiza
 
To detect a key you can use
Code:
	Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
		If e.KeyChar = "_" Then    will get the _ key

		End If
	End Sub

To get the ascii value you can use
Code:
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
Dim b() As Byte = System.Text.Encoding.ASCII.GetBytes(e.KeyChar)
b(0) will now equal the ascii code 
End Sub
 
Back
Top