繁体   English   中英

在不按 Enter 的情况下读取命令提示符输入

[英]Read in command prompt input without pressing enter

我正在尝试在 Windows 命令提示符下用 C 编写一个程序,我可以用它来练习触摸打字。 我想通过让程序提示输入一个字母来做到这一点,一旦我输入了一个字母,我希望它记录该字母是否正确并在退出之前重复该过程预定义的次数并告诉我我的时间和准确性。 通过在每个字母之间按 Enter 使其工作很容易,但我觉得它不会像我不必按 Enter 那样有用。 我在大学做了一个项目,它有一个类似的组件,但那是在 linux 中使用 C++。 我不想只为这个程序设置一个虚拟框等。

//The linux program included something like this:
//collecting original structure formate
tcgetattr(STDIN_FILENO, &normTerm); 
//assigning the original structure format to the temporary structure format
tempTerm = normTerm; 
//making the temporary structure format into raw form
cfmakeraw(&tempTerm); 
//setting the structure format to the raw form
tcsetattr(STDIN_FILENO, TCSANOW, &tempTerm);

//cfmakeraw() turns the structure at the address into the raw terminal attributes desired

//insert function that asks the user to input their password with all the flags changed so that you can't read what you have typed

    tcsetattr(STDIN_FILENO, TCSANOW, &normTerm);

如果我可以使用 C 在 Windows 中的命令提示符下制作具有相同功能的东西,那就太好了,但是人们一直说“在 C 中不可能有可移植的解决方案”。 我不介意它是否只能在这台 PC 上运行,我只是想让它运行。

我想到的另一个想法是找到一个重复刷新键盘缓冲区的函数,但仍然可以看到它刷新了什么。 那么,只要它足够快,无论如何它最多只能有一个字符。这个功能存在吗? 显然它可以用 conio.h 库来完成,但据说它来自 DOS 并且不适用于现代系统(拒绝在我的系统上工作)。

The code I have written so far is below
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <termios.h>


void main()
{
    //seeding random number generator
    srand(time(NULL));

    //char variables for actual and desired user input
    char input = 0;
    char testCharacter = 1;

    //float variables for calculating accuracy
    float countCorrect = 0;
    float countTotal = 5;
    float accuracy = 0;

    //temp variables for program operations
    int iterations = int(countTotal);
    int characterIndex = 0; 

    long startTime = time(NULL);    

    while(iterations>0)
    {
        //I am aware of the asymmetry of this, I might get around to fixing it latter
        characterIndex = (rand() %52) + 1;
        //printf("Value returned by random num gen is %d\n", characterIndex);

        //The following is messy because I don't use all the ascii characters
        //I could also probably just write a number to the char variable and then treat it as a letter to do away with the switch case statements, but I will look into that latter
        characterIndex += 64;
        if(characterIndex >= 91)
        {
            characterIndex = characterIndex + 7;
        }

        //switch case statements go here

        printf("Please type the letter below:\n%c\n", testCharacter);

        //%$&$&%*&)()*)&^%&$^&(*)_(*^&$%#^&$^%^*(&)*)(_)_*&^$%^#$^$&*(&)*(*&(^
        //This is the bit I want to modify to not require the enter key
        scanf("%c", &input);
        getchar();
        //something like 
        while(keyboard buffer is empty)
        {
        }
        flush keyboard into &input
        //maybe I could use a shell command to manually press enter whenever the keyboard buffer isn't empty???
        //(*()%&$^#$%$&^(*)*&(^$%#%$&^(*)*)&^($%&&^*(&)&*&(^$*(&)*&^($&(***&^$%*^&

        printf("\n");
        //keeps track of correct answers
        if(input == testCharacter)
        {
            countCorrect++;
            //printf("The letter %c was typed and was correct\n", input);           
        }
        else
        {
            //printf("The letter %c was typed and was incorrect\n", input);
        }

        iterations = iterations - 1;
    }

    //calculates time difference in seconds
    long timeDifference = time(NULL) - startTime;
    //calculates accuracy
    accuracy = 100.0 * (countCorrect / countTotal);
    printf("Accuracy achieve was %f%\nThe time taken was %d seconds\nPress any key to continue: ", accuracy, timeDifference);

    scanf("%c", &input);    

    return 0;
}

这个怎么样? 基本上,您将控制台重置为无缓冲,读取时没有换行,也没有回声,这会阻止字符出现。 要回显,请将 SetConsoleMode 更改为 ENABLE_ECHO_INPUT 而不是 0。一旦您想要正常输入,就重置控制台模式,这是最后一行。

#include <windows.h>
#include <stdio.h>
int main()
{
    DWORD        mode;
    HANDLE       hstdin;

    hstdin = GetStdHandle( STD_INPUT_HANDLE );

    GetConsoleMode( hstdin, &mode );
    SetConsoleMode( hstdin, mode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT));

    char result;

    printf("Press x to exit");

    while(1){
        result = getchar();
        if(result == 'x')
            break;
    }
    SetConsoleMode(hstdin, mode);
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM