Problem mapping a scancode to ascii

K

Kelly Elias

I would like to take a scancode and get the ascii value. I
have tried to use ToAsciiEx, but I first need the virtual
code. To get the virtual code I call MapVirtualKeyEx but
it seems to fail returning a 0. Any idea whats wrong with
this code?

BEGIN CODE-----------

Private Declare Function MapVirtualKeyEx Lib "user32"
Alias "MapVirtualKeyExA" (ByVal uCode As Integer, ByVal
uMapType As Integer, ByVal dwhkl As Integer) As Integer
Private Declare Function ToAsciiEx Lib "user32"
Alias "ToAsciiEx" (ByVal uVirtKey As Integer, ByVal
uScanCode As Integer, ByVal lpKeyState() As Byte, ByVal
lpChar As Integer, ByVal uFlags As Integer, ByVal dwhkl As
Integer) As Integer
Private Declare Function VkKeyScanEx Lib "user32"
Alias "VkKeyScanExA" (ByVal ch As Byte, ByVal dwhkl As
Integer) As Integer
Private Declare Function GetKeyboardState Lib "user32"
Alias "GetKeyboardState" (ByVal pbKeyState() As Byte) As
Integer
Private Declare Function GetKeyboardLayout
Lib "user32" Alias "GetKeyboardLayout" (ByVal dwLayout As
Integer) As Integer

Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim scancode As Integer = 9547
Dim keyboardstate(256) As Byte

Dim layout As Integer = GetKeyboardLayout(0)

GetKeyboardState(keyboardstate)

'PROBLEM. 0 is Returned.
Dim virtualkey As Integer = MapVirtualKeyEx
(scancode, 1, layout)

Dim result As Integer
ToAsciiEx(virtualkey, scancode, keyboardstate,
result, 0, layout)

MsgBox(result)
End Sub

--------END CODE
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi,

Wouldn't it be easier to handle WM_CHAR message sent for alphanumeric keys
when they are pressed on the keyboard? In addition, I beleive both the
virtual code and the scan code are supplied in lParam and wParam of the
WM_KEYDOWN/WM_KEYUP notifications.

Yes, I realize this is also low-level stuff, but it seems the framework
itself does not provide access to this API-level information. So you might
have to override the WndProc method and handle the keyboard messages
manually.

P.S. I was wondering whether it would be better to tell what you would like
to achieve in the first place so we could probably suggest an easier way?
 
M

Michael \(michka\) Kaplan [MS]

Where are you getting your scan code values from? They look more than a
little bit bogus.

I agree with Dmitriy; if you explained what you wanted to accomplish it
would be easier to assist you....


--
MichKa [MS]
Collation/Locale/Keyboard Development
Globalization Infrastructure and Font Technologies


This posting is provided "AS IS" with
no warranties, and confers no rights.
 

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