Converting virtual-key to ascii char
That describes it well enough, I think. I tried using MapVirtualKey() and WideCharToMultiByte(), but it doesn't work. Everything is zero. Here is the code:
UINT uMapping = 0;
unsigned short cUnicode;
unsigned char cKey;
uMapping = MapVirtualKey(pEventInfo->vkCode, 2);
cUnicode = LOWORD(uMapping);
WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, &cUnicode, 1, &cKey, 1, NULL, NULL);
if(nMsgType == WM_KEYDOWN)
WriteFile(hLogfile, (LPVOID)&cKey, 1, &dwWritten, NULL)
[519 byte] By [
philkr] at [2007-11-19 18:27:04]

# 1 Re: Converting virtual-key to ascii char
AFAIR you can use ::ToUnicode(..) (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/tounicode.asp) in winnt and higher, and ::ToAsciiEx(..) (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/toasciiex.asp) in win98.
Did you try that?
Cheers
# 2 Re: Converting virtual-key to ascii char
Well, I want the ascii value, because I'm going to save it in a simple txt-File. Why do you say ToAscii() is for win98 only?
Besides I'd like to try the function, but I don't know why I have to specify an array with the whole keyboard state. I only want to translate a single virtual-key code.
EDIT: I also have the scan code, if that helps.
philkr at 2007-11-9 13:20:52 >

# 3 Re: Converting virtual-key to ascii char
Well, I want the ascii value, because I'm going to save it in a simple txt-File. Why do you say ToAscii() is for win98 only?
Besides I'd like to try the function, but I don't know why I have to specify an array with the whole keyboard state. I only want to translate a single virtual-key code.
EDIT: I also have the scan code, if that helps.
I remember dealing with those functions once and since 98 isnt Unicode the ::ToUnicode(..) didnt work for me properly on some 98 machines, only ::ToAscii(..) did the job... but again it was couple of years from now I dont remember the whole details, but try it and post the results :)
Regarding the keyboard state you should call the ::GetKeyboardState(..) (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/getkeyboardstate.asp) and pass the result.
Cheers
# 4 Re: Converting virtual-key to ascii char
Thank you. This approach would surely work. It's just that I don't want to use GetKeyboardState(), because of performance reasons.
BTW: Did I understand right that ToUnicode() doesn't work properly on Win98, but ToAscii() runs on XP?
philkr at 2007-11-9 13:22:54 >

# 5 Re: Converting virtual-key to ascii char
I think I found the problem: My virtual keycode seems to be wrong. It is always the same. No matter which key was pressed it is 0x80FE1200. Now I will try to see why that is the case.
EDIT: Got it. I was using a pointer from a different address space. How can I calculate the right pointer using the HMODULE/HINSTANCE handles?
philkr at 2007-11-9 13:23:54 >

# 6 Re: Converting virtual-key to ascii char
Thank you. This approach would surely work. It's just that I don't want to use GetKeyboardState(), because of performance reasons.
Why? Do you think ::GetKeyboardState(..) is laggy? I dont recall having performance problems with it :confused:
BTW: Did I understand right that ToUnicode() doesn't work properly on Win98, but ToAscii() runs on XP?
No, AFAIR (which is something very easy to check if you have win98/winXP) is that ::ToUnicode(..) worked as expected on Nt/2000/XP but did some problems on Win98. I didnt check all the possibilities :D at least this is what I remember but again its very easy to check it. ;)
Cheers
# 7 Re: Converting virtual-key to ascii char
EDIT: Got it. I was using a pointer from a different address space. How can I calculate the right pointer using the HMODULE/HINSTANCE handles?
Can you please provide more info what you are trying to do? Are you catching those keys in a hook?
Cheers
# 8 Re: Converting virtual-key to ascii char
It is just so ironical. Why am I using hooks for logging keypresses, when I still need to call GetKeyboardState()?
philkr at 2007-11-9 13:27:06 >

# 9 Re: Converting virtual-key to ascii char
It is just so ironical. Why am I using hooks for logging keypresses, when I still need to call GetKeyboardState()?
::GetKeyboardState(..) should be passed to ::ToUnicode(..) so if you want to use that function I dont see other option.
And BTW it is not ironical since when you using hook you will get callback notification on each pressed case while if you will use ::GetKeyboardState(..) instead you will need to use pooling mechanism which is less preferred.
How can I calculate the right pointer using the HMODULE/HINSTANCE handles?
Why dont you do the entire mapping to Unicode in the hook dll? And send the result to your app? :confused:
Cheers
# 10 Re: Converting virtual-key to ascii char
Thanks, it is working now! I managed to get the whole keyevent data packed into WPARAM and LPARAM, so I don't have to use a pointer.
I first thought it was not possible, but then I found out that the window messages only require 16 bit, athough they are as 32. The same for flags: 8 bit instead of 32 bit. I guess the windows programmers wanted to be on the safe side as far as future changes are concerned.
There is still one issue: How can I make shifted characters out of the unshifted. Using the flags variable I know when shift was pressed. So the only thing I need to do is convert the chars. Uppercase to lower case should be no problem, but what about numbers and symbols?
philkr at 2007-11-9 13:29:04 >

# 11 Re: Converting virtual-key to ascii char
There is still one issue: How can I make shifted characters out of the unshifted. Using the flags variable I know when shift was pressed. So the only thing I need to do is convert the chars. Uppercase to lower case should be no problem, but what about numbers and symbols?
Take a look at ::VkKeyScanEx(..) (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/vkkeyscanex.asp) maybe it can help you in the process.
Cheers