【问题标题】:Read in command prompt input without pressing enter在不按回车的情况下读入命令提示符输入
【发布时间】:2014-12-26 05:06:54
【问题描述】:

我正在尝试在 Windows 命令提示符下用 C 语言编写一个程序,我可以用它来练习触摸打字。我想通过让程序提示输入一个字母来做到这一点,一旦我输入了一个字母,我希望它记录那个字母是否正确,并在退出之前重复这个过程预定的次数并告诉我我的时间和准确性。 通过在每个字母之间按回车键使其工作很容易,但我觉得它不会像我不必按回车键那样有用。我在大学做了一个项目,它有一个类似的组件,但那是在 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 语言中不可能有可移植的解决方案”。我不介意它是否只在这台电脑上工作,我只是希望它工作。

我想到的另一个想法是找到一个反复刷新键盘缓冲区的函数,但仍然可以看到它刷新了什么。那么,只要速度够快,反正最多只能有一个字符。有这个功能吗?显然它可以使用 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;
}

【问题讨论】:

  • 不看你的大部分帖子,建议scanf("% c", &amp;input);(加空格)。
  • 原始代码是这样做的,我什至在它之后有 getchar() 从缓冲区中清除输入字符。否则,它将“B[enter key]”解释为 2 个输入,并将 enter 键作为我对下一个提示的回答提交。
  • 没有验证以下 getchar() 消耗了 '\n' 或其他东西 - 这是一个猜测。
  • scanf("%c", &input) 函数可以完成这项工作,但问题是它需要按 Enter 键,我正在尝试解决此问题

标签: c windows command-line keyboard


【解决方案1】:

this 怎么样?基本上,您将控制台重置为无缓冲,它在没有换行的情况下读取,并且没有回显,这会阻止字符出现。要回显,请将 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;
}

【讨论】:

  • 我仍在尝试找到我需要的库,以便使用 _getch() 函数编译代码。我认为 getch() (不带下划线)具有我想要的功能,但这只适用于 DOS 或其他东西???
  • 有趣。你在运行什么编译器?我正在运行 Cygwin gcc 并且它丢失了,但是如果我切换到 MinGW gcc conio.h 就在那里......
  • 我已经设法让它与 windows.h 一起运行,但它与 Cygwin 中断了。
  • 当我编译程序时,我在命令的开头输入 g++
  • 我更新了答案,它使用 windows.h 代替,所有 Windows 编译器都应该存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多