我正在使用一个简单的循环来捕获用户按键;
while (1)
{
for(i = 8; i <= 190; i++)
{
if (GetAsyncKeyState(i) == -32767){
//Do stuff
}
}
}
当用户按下某个键时会“做东西”,但很明显,因为它无限循环,而且我是C ++的新手,它占用了100%的CPU,这对于简单的输入来说并不好。
我做错了什么?我已经尝试过Sleep()函数(如果我把它放在'for循环'中它会错过按键,如果我把它放在'while循环'中它根本不会降低CPU,afaik)
我怎样才能使它按下同样的按键,但使用的CPU几乎没有那么多;我错过了一招吗?我敢肯定,大多数程序都会抓住按键,你看不到所有程序都使用100%!
非常感谢。
答案 0 :(得分:2)
请查看以下链接,该链接说明正确使用GetAsyncKeyState()和示例代码。 http://www.mpgh.net/forum/31-c-c-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;
}
答案 1 :(得分:2)
将无限循环放在工作线程中,并在每次迭代时让它睡眠一段合理的间隔。 C ++ 11使这很容易:
#include <thread>
#include <chrono>
std::chrono::milliseconds THREAD_WAIT = 50;
int keypress = -1;
void GetKeyPress()
{
while (1)
{
for(i = 8; i <= 190; i++)
{
int k = GetAsyncKeyState(i);
if (/*whatever condition needs to be satisfied*/)
keypress = k;
}
if (keypress != -1) break; //Use this only if you have td.join() below
std::this_thread::sleep_for(THREAD_WAIT);
}
}
int main(void)
{
...
std::thread td( GetKeyPress );
td.join(); //If you want to block until the user presses a key, otherwise remove.
//If no join(), do the rest of your program, checking in on keypress when need be
return 0;
}
答案 2 :(得分:0)
您可以使用
while (!kbhit());
这可能会有所帮助。