GetAsyncKeyState(VK_RETURN) 错误地评估为真 [英] GetAsyncKeyState(VK_RETURN) incorrectly evaluating to true

查看:38
本文介绍了GetAsyncKeyState(VK_RETURN) 错误地评估为真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的程序,用于记录点击位置和点击间隔.在设置过程中,用户按ENTER"将新位置添加到列表中,完成输入后按ESC".

I'm writing a simple program that records click positions and intervals between clicks. During the setup, the user presses 'ENTER' to add a new position to the list, and 'ESC' once they are done entering positions.

我得到的一些奇怪的行为是其他按键导致 else if (GetAsyncKeyState(VK_RETURN)) 错误地评估为 true.我的猜测是结束 std::cin 的ENTER"在缓冲区中徘徊并导致该结果为真,但我认为 std::cin.get()std::cin.ignore 会解决这个问题.

Some weird behavior I am getting is that other key presses cause else if (GetAsyncKeyState(VK_RETURN)) to evaluate as true incorrectly. My guess was that the 'ENTER' to end a std::cin was lingering in the buffer and causing that true, however I thought std::cin.get() and std::cin.ignore would have fixed that.

为什么 'ENTER' 以外的键会导致 (GetAsyncKeyState(VK_RETURN)) 评估为真?

Why is it that keys other than 'ENTER' cause (GetAsyncKeyState(VK_RETURN)) to evaluate to 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;
}

VK_SPACE 替换 VK_RETURN 使程序完美运行.问题似乎只是 ENTER 键.

Replacing VK_RETURN with VK_SPACE makes the program work perfectly. The issue just seems to be with the ENTER key.

推荐答案

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

You are not checking the return value correctly, it does not return a BOOL! GetAsyncKeyState(VK_RETURN) < 0 or GetAsyncKeyState(VK_RETURN) > 0 depending on what you are checking for. Either way, GetAsyncKeyState is not the correct approach for a console application.

使用 ReadConsoleInput在控制台中处理输入.

如果您想在用户在另一个应用程序中工作时捕获输入,您应该使用 钩子 捕获鼠标和键盘事件.

If you want to capture input even when the user is working in another application you should use hooks to capture mouse and keyboard events.

这篇关于GetAsyncKeyState(VK_RETURN) 错误地评估为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆