如何同时处理来自 Stdin Stream 的两个输入,其中一个来自 C 中的数字键盘?

How to handle two inputs at the same time from Stdin Stream, one of them from the number pad in C?

我希望我的程序同时从标准输入流中读取数字键盘和键盘。 我不知道这是否可能,这就是我问的原因。

我的程序运行正常,但如果第一个玩家仍然按下箭头,第二个玩家将无法通过同时按下 z/x 来移动球拍,除非第一个玩家停止按下箭头。

#include <stdio.h>
#include <pthread.h>
#include <windows.h>

void first_player_arrow_press();
void second_player_Z_or_X_press();
void *second_player();

int arrow = 0, button_pressed = 0;
int z = 50;

int main()
{
    pthread_t my_thread;
    int x = 50;
    char buff[] = "################"; // the paddle
    char buff1[] = "                ";
    HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    while (1)
    {
        arrow = 0;
        button_pressed = 0;
        first_player_arrow_press();
        switch (arrow)
        {
        case 1:
            x = x;
            COORD clear1 = {x, 0};
            SetConsoleCursorPosition(hOutput, clear1);
            WriteConsoleA(hOutput, buff1, 15, NULL, NULL);
            x = x - 5;
            COORD coord = {x, 0};
            SetConsoleCursorPosition(hOutput, coord);
            WriteConsoleA(hOutput, buff, 15, NULL, NULL);
            break;
        case 2:
            x = x;
            COORD clear2 = {x, 0};
            SetConsoleCursorPosition(hOutput, clear2);
            WriteConsoleA(hOutput, buff1, 15, NULL, NULL);
            x = x + 5;
            COORD coord1 = {x, 0};
            SetConsoleCursorPosition(hOutput, coord1);
            WriteConsoleA(hOutput, buff, 15, NULL, NULL);
            break;
        }

        pthread_create(&my_thread, NULL, second_player, NULL);
        pthread_join(my_thread, NULL);
    }
    return 0;
}

void first_player_arrow_press()
{
    int ch = getch();
    if (ch == 0 || ch == 224)
    {
        switch (getch())
        {
        case 75: // left
            arrow = 1;
            break;

        case 77: // right
            arrow = 2;
            break;
        }
    }
}

void second_player_Z_or_X_press()
{
    int ch = getch();
    switch (getch())
    {
    case 122:
    case 90:     // left
        button_pressed = 1; // pressed Z , go left
        break;

    case 120:
    case 88:     // pressed X , go right
        button_pressed = 2;
        break;
    }
}

void *second_player()
{
    HANDLE hOutput1 = GetStdHandle(STD_OUTPUT_HANDLE);
    char buff[] = "################";
    char buff1[] = "                ";

    second_player_Z_or_X_press();
    switch (button_pressed)
    {
    case 1:
        z = z;
        COORD clears = {z, y};
        SetConsoleCursorPosition(hOutput1, clears);
        WriteConsoleA(hOutput1, buff1, 15, NULL, NULL);
        z = z - 5;
        COORD coords = {z, y};
        SetConsoleCursorPosition(hOutput1, coords);
        WriteConsoleA(hOutput1, buff, 15, NULL, NULL);
        break;

    case 2:
        z = z;
        COORD clears2 = {z, y};
        SetConsoleCursorPosition(hOutput1, clears2);
        WriteConsoleA(hOutput1, buff1, 15, NULL, NULL);
        z = z + 5;
        COORD coords1 = {z, y};
        SetConsoleCursorPosition(hOutput1, coords1);
        WriteConsoleA(hOutput1, buff, 15, NULL, NULL);
        break;
    }
}

这也可能是键盘重影问题。如果你看看亚马逊上的游戏键盘,有些会有多套防重影组合键。这些键组合使用时不会相互冲突。 HAVIT HV-KB380L LED 例如在 x/z 和箭头上有一个反幽灵。