For Programmers: Free Programming Magazines  


Home > Archive > VC Language > January 2006 > GetAsyncKeyState and GetKeyboardState









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author GetAsyncKeyState and GetKeyboardState
Jason Rangle

2006-01-24, 7:07 pm

I have been fighting with these two functions forever. I can't seem to get
it right. Here is what I have:
#include <windows.h>
#include <stdio.h>

int main()
{
int nAllKeys[256], i, RetMapVkey;
BYTE bKeyBuf[256];
WORD lpWord;
char ch;
for(i=0;i<256;i++)
{
nAllKeys[i] = i;
}

while(TRUE)
{
for(i=0;i<256;i++)
{
if(GetAsyncKeyState(nAllKeys[i]) == -32767)
{
GetKeyboardState( bKeyBuf );
RetMapVkey = MapVirtualKey(nAllKeys[i],0);
ToAscii(nAllKeys[i], RetMapVkey, bKeyBuf, &lpWord, 0);
ch = (char) lpWord;
printf("%c\r\n", ch);
}
}
}
return 0;
}

When I only have:

#include <windows.h>
#include <stdio.h>

int main()
{
int RetMapVkey;
BYTE bKeyBuf[256];
WORD lpWord;
char ch;

GetKeyboardState( bKeyBuf );
RetMapVkey = MapVirtualKey(0x41,0);
ToAscii(0x41, RetMapVkey, bKeyBuf, &lpWord, 0);
ch = (char) lpWord;
printf("%c\r\n", ch);

return 0;
}
I can hold down the shift key, execute my program and it will spit out 'A'
and will spit out 'a' when shift is not pressed... which is expected. For
some reason when I loop through all the virtual-keys, grab one that is
pressed then call GetKeyboardState() it doesn't work correctly. I have been
at this forever and I give up. Can someone please guide a lost soul?

--Jason


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com