Click here to Skip to main content
15,877,915 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, everyone.
I have trouble while implementing SetWindowsHookex() function.

I gonna log key pressing, using SetWIndowsHookex() function.
And I'm sure key event is hooked correctly.

So I have to know which key is pressed, using ToAscii() function.

Here are my snippet of my code.

Dim Key As String
Dim ks(256) As Byte
Dim b As Long
b = GetKeyboardState(ks(0))
Dim w As Long, scan As Long
scan = 0
ToAscii(lParam.vkCode, lParam.scanCode, ks(0), w, 0)

Key = ChrW(CType(w, Integer))

But it didn't work well.

Please help me.

Updated 7-Jul-11 8:59am

What does "didn't work well" mean? When you ran it under the debugger, what key did you press, and what was the value of lParam? You did run it under the debugger, right?
Share this answer
[no name] 7-Jul-11 15:04pm    
again, I have used SetWindowsHookEx().
and this code is in Callback function.
like below.

Public Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer

This URL can be helpful to understand my trouble.[^]
Sergey Alexandrovich Kryukov 7-Jul-11 18:33pm    
Not clear, but OP is lost conceptually, as it all has nothing to do with ASCII.
What OP needs is comparison of virtual key codes, which has nothing to do with characters. Please see my solution.
To know which key is pressed, you don't need to convert anything to ASCII. By the way, keep in mind that .NET internally supports Unicode UTF-16, not ASCII.

What you have in lParam.vkCode in not directly related to character codes; this is a "virtual code" of the key press.

Here is the table of those codes:[^].

You can take this table in C# code, make a enumeration type out of it and use. Actually, you should get the same enumeration as System.Windows.Input.Key or System.Windows.Forms.Keys. You can use one of those types and type cast.


Answering a follow-up question: "Would you like to explain me about the way how to convert Virtual code to Unicode?"

There is no such conversion. In you case you don't need it, you need to compare just keys.

Let me explain. Virtual code carry no information about Unicode characters because this information is simply absent. The character is not yet generated. Virtual key codes represent the keys on the physical keyboard device, not character. For example, if you press the key marked as '{', later on a character will be generated, but this character can be '{', '[' or some non-ASCII character, depending on several factors: status of modifier keys (like Shift), key toggles (like Capital, Caps Lock), installed and activated keyboard layout, Unicode or non-Unicode mode of the application, current input language, input method, current UI culture of the application and, finally, local processing of the events in a currently active application All that factors are taken into account by OS to generate a character.

At the moment you receive a virtual key code, this information — in general case — is not available. You only got an information of the physical key pressed. Now, even though alphabetical key, say English 'A' has the same integer code point value as the virtual key marked as 'A', it does not mean a character 'A' will be generated. A generated character (if any) depends on the factors I listed in the above paragraph. Many key generate no characters at all and are always used in their virtual key mode form: F1 to F12, Alt, Shift, VolumeMute, VolumeDown — many of them. Please see the table I referenced above.

Generally, there are several layers of keyboard processing, one based on another: scan code, OEM code, virtual key code. Virtual key code in not the "most hardware" level. For example, if contains mouse event which are unrelated from the hardware stand point.

Share this answer
Joan M 7-Jul-11 18:40pm    
perfect! 5ed...
Sergey Alexandrovich Kryukov 7-Jul-11 19:04pm    
Thank you Joan.
[no name] 7-Jul-11 19:54pm    
thanks for your corporation.
Would you like to explain me about the way how to convert Virtual code to Unicode?
Sergey Alexandrovich Kryukov 7-Jul-11 20:19pm    
Seriously? OK, please see the updated solution, after [EDIT].
Now you have a pretty complete picture.

Well you formally accept the answer now (green button)?
If not, (sigh...), well, ask me what's not clear... :-)

Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900