getKeystate()之后的清除控制台输入

发布于 2025-02-10 23:50:49 字数 513 浏览 1 评论 0原文

在按“ 1”之后,我有一个用于创建用户名的控制台登录系统。但是,每当我按该键时,按下的键都会显示在控制台输入中。我想摆脱这一点。

#include <Windows.h>

    bool pressed1;
    std::string inputUsername;
    
    do
    {
        pressed1 = GetKeyState('1') & 0x8000;
        if (pressed1) {
            std::cout << "\nPressed '1' enter username: ";
            std::cin >> inputUsername;
        }
    } while (!pressed1);

因此,执行和按下“ 1”控制台时说:

Pressed '1' enter username: 1

如何摆脱输入中的按下按钮?

I have a console login-system for creating a username after pressing '1'. But whenever i press that key, the pressed key shows up in the console input. I want to get rid of that.

#include <Windows.h>

    bool pressed1;
    std::string inputUsername;
    
    do
    {
        pressed1 = GetKeyState('1') & 0x8000;
        if (pressed1) {
            std::cout << "\nPressed '1' enter username: ";
            std::cin >> inputUsername;
        }
    } while (!pressed1);

So when executing and pressing '1' the console says:

Pressed '1' enter username: 1

How do i get rid of the pressed button in the input?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↙温凉少女 2025-02-17 23:50:49

您可以使用flushConsoleInputBuffer(getStdhandle(std_input_handle))

#include <Windows.h>
#include <string>
#include <iostream>

void main()
{
    bool pressed1;
    std::string inputUsername;

    do
    {
        pressed1 = GetKeyState(VK_F1) & 0x8000;
        if (pressed1) {
            std::cout << "\nPressed 'F1' enter username: ";
            FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
            std::cin >> inputUsername;
        }
    } while (!pressed1);
}

另请参见以前的答案”

You can use FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE))

#include <Windows.h>
#include <string>
#include <iostream>

void main()
{
    bool pressed1;
    std::string inputUsername;

    do
    {
        pressed1 = GetKeyState(VK_F1) & 0x8000;
        if (pressed1) {
            std::cout << "\nPressed 'F1' enter username: ";
            FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
            std::cin >> inputUsername;
        }
    } while (!pressed1);
}

See also this previous answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文