サンプル コードで GetAsyncKeyState() の適切な使用法を説明する次のリンクを確認してください。
http://www.mpgh.net/forum/31-cc-programming/120656-proper-use-getasynckeystate.html
このリンクが問題の解決に役立つことを願っています
編集:
GetAsyncKeyState() 関数は、あなたがしようとしていることには理想的ではありません。
キーボード上のキーの実際の現在のナノ秒位置を確認するだけです。それを行うことは、ほとんどの場合正しくありません。
代わりに、適切な入力関数を使用してコンソール入力を読み取ります。以下のサンプルコードをご覧ください。
#include <stdio.h>
#include <windows.h>
int main()
{
DWORD mode; /* Preserved console mode */
INPUT_RECORD event; /* Input event */
BOOL done = FALSE; /* Program termination flag */
unsigned int counter = 0; /* The number of times 'Esc' is pressed */
/* Don't use binary for text files, OK? ;-) */
FILE* myfile = fopen( "example.txt", "w" );
/* Get the console input handle */
HANDLE hstdin = GetStdHandle( STD_INPUT_HANDLE );
/* Preserve the original console mode */
GetConsoleMode( hstdin, &mode );
/* Set to no line-buffering, no echo, no special-key-processing */
SetConsoleMode( hstdin, 0 );
/* Give the user instructions */
printf(
"Press Escape as many times as you like.\n"
"Press anything else to quit.\n\n"
);
while (!done)
{
if (WaitForSingleObject( hstdin, 0 ) == WAIT_OBJECT_0) /* if kbhit */
{
DWORD count; /* ignored */
/* Get the input event */
ReadConsoleInput( hstdin, &event, 1, &count );
/* Only respond to key release events */
if ((event.EventType == KEY_EVENT)
&& !event.Event.KeyEvent.bKeyDown)
switch (event.Event.KeyEvent.wVirtualKeyCode)
{
case VK_ESCAPE:
counter++;
fprintf( myfile, "Escape: %d\n", counter );
printf( "Button pressed!\n" );
break;
default:
done = TRUE;
}
}
}
/* All done! */
printf( "You pressed the Escape key %d times\n", counter );
fclose( myfile );
SetConsoleMode( hstdin, mode );
return 0;
}