|
|
|
|
 关于直接捕获按键的问题! - Solidus [ 2005-09-30 05:48 | 2,208 byte(s)]
 Re: 关于直接捕获按键的问题! - Solidus [ 2005-09-30 11:56 | 40 byte(s)]
 Re: 关于直接捕获按键的问题! - Atu [ 2005-09-30 10:03 | 253 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
//谁帮着看看为什么我这个输入完字符后还需要回车啊?
#include<iostream>
#include<fstream>
#include<cctype>
#include<termios.h>
using namespace std;
int get_response(char& question)
{
for(;;)
{
if( question == 'y')
{
return 1;
}
else if ( question == 'n')
{
return 0;
}
else
{
cout << "Bad input, please enter again!" << endl;
return -1;
}
}
}
void set_crmode()
{
termios ttystate;
tcgetattr(0, &ttystate);
ttystate.c_lflag &=(~ICANON);
ttystate.c_cc[VTIME] = 0;
ttystate.c_cc[VMIN] = 1;
tcsetattr(0, TCSANOW, &ttystate);
}
void tty_mode(int how)
{
static termios orginal_mode;
if(how == 0)
{
tcgetattr(0, &orginal_mode);
set_crmode();
}
else
{
tcsetattr(0, TCSANOW, &orginal_mode);
}
}
int main()
{
cout << "Do you want to another transaction(y/n)?" << endl;
char c;
int response;
while(1)
{
tty_mode(0);
getchar(c);
tty_mode(1);
c = char(tolower(c));
response = get_response(c);
while(cin.get() != '
') continue;
if(response == -1)
continue;
else
break;
}
cout << response << endl;
return response;
}
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
> //谁帮着看看为什么我这个输入完字符后还需要回车啊?
你应该知道你在做什么。
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
while(cin.get() != '
') continue;
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
[Original]
[Print]
[Top]
|
|
|