Home All Groups Group Topic Archive Search About

Keys.Oem1 etc to real character

Author
14 Dec 2006 8:14 AM
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

Author
14 Dec 2006 8:58 AM
Stephany Young
Ummmmmmmm ... How about Convert.ToChar(Keys.Oem1)


Show quote
"Pieter" <pieterNOSPAMcoucke@hotmail.com> wrote in message
news:OlWhmf1HHHA.816@TK2MSFTNGP06.phx.gbl...
> 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
>
Author
14 Dec 2006 10:41 AM
Stuart Nathan
or even Asc(Keys.Oem1)
Author
14 Dec 2006 10:47 AM
Stephany Young
No.  Asc(Keys.Oem1) will return a byte.  You need AscW(Keys.Oem1) to return
a char.


Show quote
"Stuart Nathan" <stuart.nat***@homecall.co.uk> wrote in message
news:u9NP6x2HHHA.3872@TK2MSFTNGP06.phx.gbl...
> or even Asc(Keys.Oem1)
>
Author
14 Dec 2006 11:35 AM
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 :-(




Show quote
"Stephany Young" <noone@localhost> wrote in message
news:uKybB12HHHA.1044@TK2MSFTNGP02.phx.gbl...
> No.  Asc(Keys.Oem1) will return a byte.  You need AscW(Keys.Oem1) to
> return a char.
>
>
> "Stuart Nathan" <stuart.nat***@homecall.co.uk> wrote in message
> news:u9NP6x2HHHA.3872@TK2MSFTNGP06.phx.gbl...
>> or even Asc(Keys.Oem1)
>>
>
>
Author
14 Dec 2006 11:59 AM
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

AddThis Social Bookmark Button