need to adjust some vb6 code - Scancode to Ascii - Please HELP!!

P

pamelafluente

I have found on the web a post with this useful way to convert (if
possible) a ScanCode to Ascii. This is remarkably useful to filter keys
under KeyDown/up event.

My problem is that I am not familiar with Platform Invoke and this code
(vb6) needs just a little adjustment (types) to work in VB.NET.

Would anyone be so kind as to show the necessary changes?

Thank you very much in advance!!

-Pamela

'-----------------------------------------------------------------------------------------

Private Declare Function ToAscii Lib "user32" ( _
ByVal uVirtKey As Long, _
ByVal uScanCode As Long, _
lpbKeyState As Byte, _
ByVal lpwTransKey As String, _
ByVal fuState As Long) As Long

Private Declare Function GetKeyboardState Lib "user32" ( _
pbKeyState As Byte) As Long

Function KeyCodeToAscii(ByVal KeyCode As Integer) As String
Dim abKeystate(0 To 255) As Byte

KeyCodeToAscii = " "
GetKeyboardState abKeystate(0)
KeyCodeToAscii = Left$(KeyCodeToAscii, _
ToAscii(KeyCode, _
0, _
abKeystate(0), _
KeyCodeToAscii, _
0))

End Function

'---------------------------------------------------------------------------------------
 
S

ShaneO

.... This is remarkably useful to filter keys
under KeyDown/up event....

Please excuse my ignorance, but what's wrong with just using the
"e.KeyCode" value within VB.NET KeyDown/Up events?

Doesn't this provide you with sufficient ability to "filter keys"?

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
C

Cor Ligthert [MVP]

Pamela,

In Addition to Shane. You use in one sentence KeyDown and KeyUp as if they
are the same.

Although it seems ovious to use KeyDown, do I never use it. It gives almost
no information about the pressed key. Key Up gives a bunch of information.
Therefore I would follow Shanes advice and have a look at Key Up.

About the API's know that there has been a shift in meaning of the words
Short Integer and Long in VBNet. Therefore I use now Int16, Int32, Int64 for
fixed values as in API's.

VB6 its Long is VBNet Integer.

I hope this helps,

Cor
 
P

pamelafluente

Thank you ShaneO and Cor ,

actually I am talking about the TextBox mouse UP/DOWN. I am not clear
what you Cor mean by saying that mouseUP is more informative. As to key
pressed they seems quite the same to me (what am I missing? can I get
the ASCII there?).

I have some good reason to want the ASCII. For instance I can put
*dinamically* unwanted ascii chars (with appropriate Lower and Upper
case) in a string and filter (suppress) them. I cannot do that by using
the Code enumeration.

Even if I didn't use for that, it is anyway more flexible to compare
the ascii instead of using the enumeration.

As to the observation of SnaneO, note that when editing a textbox my
attention is on the final ascii, not on the scan code, which identifies
the key. I want to control the actual ascii which goes into the text
box.

Would I ask too much, dear Cor, if you could show how to adapt the
types in the above code. I am really unfamilar with that and all my
attempts have driven me to unbalance the stack :((

Thank you very much indeed,

-Pam
 
C

Cor Ligthert [MVP]

Pamela,
actually I am talking about the TextBox mouse UP/DOWN. I am not clear
what you Cor mean by saying that mouseUP is more informative. As to key
pressed they seems quite the same to me (what am I missing? can I get
the ASCII there?).

Where did I write about mouse Up information. You where talking about keys
and I have answered accoording to that.

A keyboard has a lot of keys a mouse has mostly not more than 3 buttons and
sometimes a wheel. In the way you write it is a monitor the same as a
diskdrive.

Giving an answer is mostly not that difficult. However if your question is
about the st. Pietro in Roma while you ask the route to the Westerkerk in
Amsterdam, than don't expect a right answer.

Cor
 
P

pamelafluente

Sorry Cor. Yes right, of course I meant keyUp (just a lapsus from a
non-native): we are talking about keys, nothing to do with the mouse of
course.... Still struggling with these types... :-(

Cor Ligthert [MVP] ha scritto:
 
S

ShaneO

I have some good reason to want the ASCII. For instance I can put
*dinamically* unwanted ascii chars (with appropriate Lower and Upper
case) in a string and filter (suppress) them. I cannot do that by using
the Code enumeration.

Even if I didn't use for that, it is anyway more flexible to compare
the ascii instead of using the enumeration.

As to the observation of SnaneO, note that when editing a textbox my
attention is on the final ascii, not on the scan code, which identifies
the key. I want to control the actual ascii which goes into the text
box.
Pam, as per your request, the following will provide the ASCII value for
each keypress (Watch out for line-wrapping) -

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

Dim KeyAscii As UInt16 = Asc(e.KeyChar)

End Sub

From here you can manipulate the value to whatever you like -

eg. KeyAscii = Asc(UCase(Chr(KeyAscii))) 'Returns UPPER Case value

Remember to use -

e.Handled = True

within the event handler if you want to suppress the actual keystroke.

I hope this helps. You requested that you wanted to know the ASCII
value, not the scan code, you now have it.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
P

pamelafluente

THANK YOU!!! ShaneO

It does help a lot. It seems you found the managed way to do it.
Fantastic!! How did you figure that out?

Btw, in case anyone cares, vb2005 automatically adds a conversion:

Dim KeyAscii As UInt16 = CUShort(Asc(e.KeyChar))


Thank you very much. That was very kind of you !
(the pinvoke stuff goes directly to the dubstin!) :)

-pam

ShaneO ha scritto:
 
S

ShaneO

THANK YOU!!! ShaneO

It does help a lot. It seems you found the managed way to do it.
Fantastic!! How did you figure that out?
There wasn't too much to figure out, a lot of people would already know
how to obtain the ASCII value of a keystroke. I guess my problem was
fully understanding your original question, which was my fault, sorry.

Regards,

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
C

Cor Ligthert [MVP]

Ok,

You got your answer from ShaneO. Have now a look at the Key Up in the same
situation. You see than a lot more posibilities with that e.

I hope this helps,

Cor
 

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