C++捕捉按键
Catching keypresses C++
本文关键字:C++ 更新时间:2023-10-16
我正在使用一个简单的循环来捕获用户按键;
while (1)
{
for(i = 8; i <= 190; i++)
{
if (GetAsyncKeyState(i) == -32767){
//Do stuff
}
}
}
当用户按下某个键时,这将"做一些事情",但是很明显,因为它是无限循环的,而且我是新手C++它占用了 100% 的 CPU,这对于简单的输入不利。
我做错了什么?我尝试过 Sleep() 函数(如果我把它放在"for 循环"中,它会错过按键,如果我把它放在"while 循环"中,它根本不会降低 CPU,afaik)
我怎样才能让它捕捉相同的按键,但使用不那么多的CPU;我错过了一个技巧吗?我敢肯定,大多数程序都会捕获按键,而您不会看到所有程序都使用 100%!
多谢。
将无限循环放在工作线程中,并在每次迭代中让它休眠一段时间。 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;
}
请查看以下链接,该链接解释了正确使用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.nn"
);
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: %dn", counter );
printf( "Button pressed!n" );
break;
default:
done = TRUE;
}
}
}
/* All done! */
printf( "You pressed the Escape key %d timesn", counter );
fclose( myfile );
SetConsoleMode( hstdin, mode );
return 0;
}
您可以使用
while (!kbhit());
这可能会有所帮助。
相关文章:
- 没有找到相关文章