C++ GetAsyncKeyState(VK_返回)计算结果不正确,为true

C++ GetAsyncKeyState(VK_返回)计算结果不正确,为true,c++,.net,windows,keypress,C++,.net,Windows,Keypress,我正在写一个简单的程序,记录点击位置和点击间隔。在设置过程中,用户按“回车”将新位置添加到列表中,输入位置后按“ESC” 我得到的一些奇怪的行为是,其他按键导致else if(GetAsyncKeyState(VK_RETURN))错误地计算为true。我的猜测是,结束std::cin的“ENTER”在缓冲区中挥之不去,并导致出现这种情况,但是我认为std::cin.get()和std::cin.ignore会解决这个问题 为什么“ENTER”以外的键会导致(GetAsyncKeyState(V

我正在写一个简单的程序,记录点击位置和点击间隔。在设置过程中,用户按“回车”将新位置添加到列表中,输入位置后按“ESC”

我得到的一些奇怪的行为是,其他按键导致
else if(GetAsyncKeyState(VK_RETURN))
错误地计算为true。我的猜测是,结束
std::cin
的“ENTER”在缓冲区中挥之不去,并导致出现这种情况,但是我认为
std::cin.get()
std::cin.ignore
会解决这个问题

为什么“ENTER”以外的键会导致
(GetAsyncKeyState(VK_RETURN))
计算为true

void initialSetup() {

    int temp = 0;
    char input;

    std::cout << "Unique sleep times? (y/n):  ";
    std::cin >> input;
    std::cin.get();
    std::cin.ignore(100, '\n'); // discards the input buffer

    // Ask the user for a sleep each time, or use the same
    if (input == 'y') {
        uniqueSleepBetweenClicks = true;
    }
    else {
        // Sleep times are constant after each click
        std::cout << "Constant sleep time between clicks in ms:  ";
        std::cin >> constSleepBetweenClicks;
        std::cin.get();
        std::cin.ignore(100, '\n'); // discards the input buffer
    }

    std::cout << endl;
    std::cout << "***********************************" << endl;
    std::cout << "* 'ENTER' to set new position     *" << endl;
    std::cout << "* 'ESC' when done                 *" << endl;
    std::cout << "***********************************" << endl << endl;


    // Add new clicks to the sequence
    while (_getch()){

        Click click;

        if (GetAsyncKeyState(VK_ESCAPE)) {
            // Escape keypress ends adding new clicks
            break;
        }
        else if (GetAsyncKeyState(VK_RETURN)) {

            // Set the click position
            GetCursorPos(&click.point);
            std::cout << "Position set to (" << click.point.x << "," << click.point.y << ") " << endl;

            if (uniqueSleepBetweenClicks) {
                std::cout << "Sleep time in ms: ";
                std::cin >> click.sleep;
                std::cin.get();
                std::cin.ignore(100, '\n'); // discards the input buffer
            } 
            else { 
                click.sleep = constSleepBetweenClicks;
            }

            // Add to the list
            clickList.push_back(click);

        } 
    }
    return;
}
void initialSetup(){
内部温度=0;
字符输入;
std::cout>输入;
std::cin.get();
std::cin.ignore(100,'\n');//丢弃输入缓冲区
//每次都要求用户睡眠,或者使用相同的方法
如果(输入='y'){
uniqueSleepBetweenClicks=true;
}
否则{
//每次点击后睡眠时间不变
std::cout>constSleepBetweenClicks;
std::cin.get();
std::cin.ignore(100,'\n');//丢弃输入缓冲区
}

std::cout您没有正确检查返回值,它不会返回BOOL!
GetAsyncKeyState(VK_return)<0
GetAsyncKeyState(VK_return)>0
,这取决于您检查的内容。无论哪种方式,
GetAsyncKeyState
都不是控制台应用程序的正确方法

使用
ReadConsoleInput


如果您想捕获输入,即使用户在另一个应用程序中工作,您也应该使用它来捕获鼠标和键盘事件。

我选择了
GetAsyncKeyState
,因为即使控制台不是活动窗口,我也需要读取按键。似乎除escape之外的任何按键都算作enter。