Reputation: 7243
I am using hooks and I have a vkCode and a scanCode so I am using the command ToAscii() as such:
...
LPWORD wCharacter;
ToAscii(kbdStruct.vkCode, kbdStruct.scanCode, keyboard_state, wCharacter, 0);
So now wCharacter should hold the Ascii character right?
How can I print that out?
I tried: printf(wCharacter);
but it says: "cannot convert 'WORD*' to 'const char*'"
What am I doing wrong? How do I print out a WORD*? Or was I doing the ToAscii command wrong?
Upvotes: 1
Views: 5038
Reputation: 181
LPWORD wCharacter;
is a Long Pointer to a word. Word here as an integer of two bytes not a "word" in a sentence. It's basically an int16. printf would handle that as.
printf("%hd", *wCharacter );
if you wanted your printed value to reflect it as unsigned
printf("%hu", *wCharacter );
unsigned hex
printf("%hx", *wCharacter );
unsigned hex with capital letters
printf("%hX", *wCharacter );
Now having said all that it's possible your WORD int is a Unicode character or two byte character as apposed to a normal 8 bit standard char.
In unicode if you are still representing standard ascii characters as apposed to some Arabic or Chinese characters you can translate the unicode character into a standard char by ignoring the first byte.
LPWORD wCharacter;
char *pChar = (char*)wCharacter;
printf("%c", pChar[1]);
This works if you aren't using international character set.
Upvotes: 0
Reputation: 942000
You are not going to get far with this, you are passing an uninitialized pointer to ToAscii(). Proper code should look like this:
WORD wCharacter[2];
int len = ToAscii(kbdStruct.vkCode, kbdStruct.scanCode, keyboard_state, wCharacter, 0);
if (len == 1) printf("%c", wCharacter[0]);
if (len == 2) printf("%c", wCharacter[1]);
This ought to compile and work, somewhat. In practice you cannot get this reliable. The *keyboard_state* variable you pass should be the keyboard state of the process that owns the foreground window. And you should pay attention to the keyboard layout that's active for that process (see ToAsciiEx). That cannot be made to work with a low-level keyboard hook. A keyboard logger must use a WH_CALLWNDPROC hook instead to intercept WM_CHAR messages (I think, never wrote one). Much harder to get right, that requires a DLL that can be injecting into other processes. You are of course inventing a wheel, buy and not build is the best advice. Also would make your users a bit more comfortable about your intentions.
Upvotes: 1
Reputation: 1198
WORD nor ToAscii() is standard C++, so answering this is kind of tricky. However, there are two issues anyhow:
Upvotes: 1