Keys.Oem1 etc to real character

P

Pieter

Hi,

How can I convert KeyCode's to the real character? For instance: the row of
keys on a keyboard under the F-keys return as Keycode the Keys.Oem1,
Keys.Oem2 etc... Depending on the keyboard layout it is another character
(and also depening on the fact if you have used Shift or Alt Gr).

But my problem is: I would like to knwo the exact character the user sees on
the screen.

I'm using VB.NET 2003.

Thanks a lot in advance,

Pieter
 
S

Stephany Young

No. Asc(Keys.Oem1) will return a byte. You need AscW(Keys.Oem1) to return
a char.
 
P

Pieter

Hi,

I tried these alreaddy, but they don't work...
For instance:
The 2 upper left keys on my keyboard (under the Esc-key) are (I'm using an
Belgian Azerty): "²" and "&".

What I get for them is:
"²":
- KeyCode = 222 = Keys.OemQuotes
- Convert.ToChar(e.KeyCode) = "Þ"c (I don't even knwo the name of this sign,
I hope it shows up in the newsreader...)
- Asc(e.KeyCode) = AscW(e.KeyCode) = 50

"&":
- KeyCode = 49 = Keys.D1
- Convert.ToChar(e.KeyCode) = "1"c (If I do Shift + ThisKey then I get
indeed "1", but I did it without the Shift...)
- Asc(e.KeyCode) = AscW(e.KeyCode) = 52


Anybody has any idea??? I'm really stuck on this :-(
 
P

Pieter

Aaah, I found it :)



Declare Function ToAscii Lib "user32" (ByVal uVirtKey As Integer, ByVal
uScanCode As Integer, ByRef lpbKeyState As Byte, ByRef lpwTransKey As
Integer, ByVal fuState As Integer) As Integer
Private Declare Function GetKeyboardState Lib "user32.dll" (ByRef
pbKeyState As Byte) As Long

Private Function GetCharFromKey(ByVal KeyCode As Integer) As String
Dim KeyBoardState(255) As Byte
Dim Out As Long
If GetKeyboardState(KeyBoardState(0)) <> 0 Then
If ToAscii(KeyCode, 0, KeyBoardState(0), Out, 0) <> 0 Then
If Out <= 255 Then
GetCharFromKey = Chr(Out)
Else
'GetCharFromKey =
Microsoft.VisualBasic.Left(StrConv(ChrW(Out), vbUnicode), 1)
GetCharFromKey =
Microsoft.VisualBasic.Left(StrConv(ChrW(Out), VbStrConv.None), 1)
End If
Else
GetCharFromKey = ""
End If
Else
GetCharFromKey = ""
End If
End Function
 

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