当前位置: 技术问答>linux和unix
关于 getch() 的问题
来源: 互联网 发布时间:2015-04-27
本文导语: 在 windows 下 ..... #include .... main() { while(1) { if(kbhit()) { char getch=getch(); if(getch='q') break; } } } 这段话 ,转到 RedHat Linux 7.3 下如何实现 ...
在 windows 下
.....
#include
....
main()
{
while(1)
{
if(kbhit())
{
char getch=getch();
if(getch='q') break;
}
}
}
这段话 ,转到 RedHat Linux 7.3 下如何实现
.....
#include
....
main()
{
while(1)
{
if(kbhit())
{
char getch=getch();
if(getch='q') break;
}
}
}
这段话 ,转到 RedHat Linux 7.3 下如何实现
|
if(kbhit())...
这个用ncurses编程可以做到。如果你是终端下的程序,应该是用curses吧(当然也有别的库),如果你是X下的程序,处理键盘消息就可以了。
这个用ncurses编程可以做到。如果你是终端下的程序,应该是用curses吧(当然也有别的库),如果你是X下的程序,处理键盘消息就可以了。
|
看Unix Programming FAQ中相关内容:http://www.erlenstar.demon.co.uk/unix/faq_toc.html
3.2 How can I read single characters from the terminal?
=======================================================
How can I read single characters from the terminal? My program is
always waiting for the user to press `'.
Terminals are usually in canonical mode, where input is read in lines after
it is edited. You may set this into non-canonical mode, where you set how
many characters should be read before input is given to your program. You
also may set the timer in non-canonical mode terminals to 0, this timer
flushs your buffer at set intervals. By doing this, you can use `getc()' to
grab the key pressed immediately by the user. We use `tcgetattr()' and
`tcsetattr()' both of which are defined by POSIX to manipulate the
`termios' structure.
#include
#include
#include
#include
static struct termios stored_settings;
void set_keypress(void)
{
struct termios new_settings;
tcgetattr(0,&stored_settings);
new_settings = stored_settings;
/* Disable canonical mode, and set buffer size to 1 byte */
new_settings.c_lflag &= (~ICANON);
new_settings.c_cc[VTIME] = 0;
new_settings.c_cc[VMIN] = 1;
tcsetattr(0,TCSANOW,&new_settings);
return;
}
void reset_keypress(void)
{
tcsetattr(0,TCSANOW,&stored_settings);
return;
}
3.3 How can I check and see if a key was pressed?
=================================================
How can I check and see if a key was pressed? On DOS I use the
`kbhit()' function, but there doesn't seem to be an equivalent?
If you set the terminal to single-character mode (see previous answer),
then (on most systems) you can use `select()' or `poll()' to test for
readability.
3.2 How can I read single characters from the terminal?
=======================================================
How can I read single characters from the terminal? My program is
always waiting for the user to press `'.
Terminals are usually in canonical mode, where input is read in lines after
it is edited. You may set this into non-canonical mode, where you set how
many characters should be read before input is given to your program. You
also may set the timer in non-canonical mode terminals to 0, this timer
flushs your buffer at set intervals. By doing this, you can use `getc()' to
grab the key pressed immediately by the user. We use `tcgetattr()' and
`tcsetattr()' both of which are defined by POSIX to manipulate the
`termios' structure.
#include
#include
#include
#include
static struct termios stored_settings;
void set_keypress(void)
{
struct termios new_settings;
tcgetattr(0,&stored_settings);
new_settings = stored_settings;
/* Disable canonical mode, and set buffer size to 1 byte */
new_settings.c_lflag &= (~ICANON);
new_settings.c_cc[VTIME] = 0;
new_settings.c_cc[VMIN] = 1;
tcsetattr(0,TCSANOW,&new_settings);
return;
}
void reset_keypress(void)
{
tcsetattr(0,TCSANOW,&stored_settings);
return;
}
3.3 How can I check and see if a key was pressed?
=================================================
How can I check and see if a key was pressed? On DOS I use the
`kbhit()' function, but there doesn't seem to be an equivalent?
If you set the terminal to single-character mode (see previous answer),
then (on most systems) you can use `select()' or `poll()' to test for
readability.
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。