ASCII Keyboard Code for VB.Net

  • Thread starter Lumpierbritches
  • Start date
L

Lumpierbritches

Thank you in advance for any and all assistance. Is there a way in VB.Net to
call the ASCII equivilalent to each corresponding keypress?

Similar to VB6, using KeyAscii?

If KeyAscii <= 26 Then Exit Sub
Dim I As Integer

I was able to pull ASCII codes for each letter or keypress, which this code
above. Is there a way to do this in DotNet?

Michael
 
S

Scott

The KeyPress event in .NET has a KeyPressEventArgs object which contains a
member named KeyChar. This member contains the ASCII character code. You can
use this in several ways....

Sub keypressed(ByVal o As [Object], ByVal e As KeyPressEventArgs)
' Using the character code
If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
e.Handled = True
End If
' Using the Keys enumeration
If e.KeyChar = Keys.Enter Then
e.Handled = True
End If
End Sub 'keypressed
 
L

Lumpierbritches

Thank you, however I'm sure I was unclear. I want to pull and place into a
lblName.text the ASCII equivalent to the Key Pressed on the keyboard. I'm using
this for a psuedo registration/password algo. for a small program. Each time a
key is pressed on the keyboard, the lblName.Text will reflect the keypress
equivalent to the corresponding ASCII numeric value.

Michael
 
S

Scott

I'm still not quite sure I understand what you're looking for. If you want
to capture a system-wide key press (not only from within a certain form or
application) then I suggest checking out the following article on low-level
keyboard hooks in VB.NET
(http://www.developer.com/net/net/article.php/11087_2193301_1). If you are
trying to catch key press events from a specific textbox in your application
then use the KeyPressEventArgs object as I suggested previously...

Sub keypressed(ByVal o As [Object], ByVal e As KeyPressEventArgs)
lblName.Text += e.KeyChar.ToString()
End Sub

KeyChar is of type "Char" which means you can do anything with it that you
can do with a "Char" object including getting the ASCII code or character...
 
M

Michael Hesse

If I enter the code
If e.KeyChar = Keys.Enter Then ...
I get this error message:

Operator '=' is not defined for types 'Char' and
'System.Windows.Forms.Keys'.

Am I doing something wrong?

Michael

Scott said:
The KeyPress event in .NET has a KeyPressEventArgs object which contains a
member named KeyChar. This member contains the ASCII character code. You can
use this in several ways....

Sub keypressed(ByVal o As [Object], ByVal e As KeyPressEventArgs)
' Using the character code
If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
e.Handled = True
End If
' Using the Keys enumeration
If e.KeyChar = Keys.Enter Then
e.Handled = True
End If
End Sub 'keypressed


Lumpierbritches said:
Thank you in advance for any and all assistance. Is there a way in
VB.Net
to
call the ASCII equivilalent to each corresponding keypress?

Similar to VB6, using KeyAscii?

If KeyAscii <= 26 Then Exit Sub
Dim I As Integer

I was able to pull ASCII codes for each letter or keypress, which this code
above. Is there a way to do this in DotNet?

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top