我正在尝试在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 %dn", 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%cn", 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 correctn", input);
}
else
{
//printf("The letter %c was typed and was incorrectn", 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 secondsnPress 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;
}