4

我正在使用一个简单的循环来捕获用户按键;

while (1)
{
    for(i = 8; i <= 190; i++)
    {
       if (GetAsyncKeyState(i) == -32767){
         //Do stuff
       }
    }
}

当用户按下某个键时,它会“做一些事情”,但很明显,因为它无限循环,而且我是 C++ 新手,它占用了 100% 的 CPU,这不利于简单的输入。

我究竟做错了什么?我试过 Sleep() 函数(如果我把它放在'for循环'它会错过按键,如果我把它放在'while循环'它根本不会降低CPU,afaik)

我怎样才能让它捕捉到相同的按键,但使用的 CPU 几乎没有那么多;我错过了一个技巧吗?我敢肯定大多数程序都会捕获按键,而您不会看到所有程序都使用 100%!

非常感谢。

4

3 回答 3

3

将你的无限循环放在一个工作线程中,并让它在每次迭代时休眠一段合理的时间间隔。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;
}
于 2012-09-30T03:40:36.097 回答
1

请查看以下链接,该链接通过示例代码解释了正确使用 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;
}
于 2012-09-30T03:23:16.160 回答
0

你可以使用

while (!kbhit());

这可能会有所帮助。

于 2012-09-30T03:10:04.073 回答