int readch() { char ch; if(peek_character!= -1) { ch = peek_character; peek_character = -1; 返回ch; } 读取(0,& ch,1); 返回ch; } / * kbhit.c EOF * / / *以下代码为kbhit.h * / #ifndef __KBHIT_H__ #define __KBHIT_H__ void init_keyboard(void); void close_keyboard(void); int kbhit(void); int readch(void); #endif / * kbhit.h EOF * / / * 像这个快速而肮脏的例子一样使用它 * / #include< stdlib.h> #include< stdio .h> #include" kbhit.h" int main(无效) { int ch; init_keyboard(); //每个程序运行一次 //使用它的一种方法 if(kbhit())ch = readch(); //这里是另一个样本用法 do { if(kbhit())// kbhit立即返回,按下按键或不是 { ch = readch(); //根据ch /> 的值做任何事情if(ch =''q'')休息; } sleep(1); } while(1); close_keyboard(); 返回0; }Search the net for kbhit.c there''s a version of it out there for linux, iuse it a lot and it solves these kinds of problems. Just add the kbhit.cfile to your Makefile and put #include "kbhit.h" in your program.Call init_keyboard() at top of program and close_keyboard() on or beforeexit. These two steps are really important, dont forget them.Warning: failure to call close_keyboard() before your program exits willleave your keybaord in a really weird state.Ah hell, its short enough - Here...Eric/* The following code is kbhit.c */#include "kbhit.h"#include <termios.h>#include <unistd.h> // for read()static struct termios initial_settings, new_settings;static int peek_character = -1;void init_keyboard(){tcgetattr(0,&initial_settings);new_settings = initial_settings;new_settings.c_lflag &= ~ICANON;new_settings.c_lflag &= ~ECHO;new_settings.c_lflag &= ~ISIG;new_settings.c_cc[VMIN] = 1;new_settings.c_cc[VTIME] = 0;tcsetattr(0, TCSANOW, &new_settings);}void close_keyboard(){tcsetattr(0, TCSANOW, &initial_settings);}int kbhit(){unsigned char ch;int nread;if (peek_character != -1) return 1;new_settings.c_cc[VMIN]=0;tcsetattr(0, TCSANOW, &new_settings);nread = read(0,&ch,1);new_settings.c_cc[VMIN]=1;tcsetattr(0, TCSANOW, &new_settings);if(nread == 1){peek_character = ch;return 1;}return 0;}int readch(){char ch;if(peek_character != -1){ch = peek_character;peek_character = -1;return ch;}read(0,&ch,1);return ch;}/*kbhit.c EOF*//* The following code is kbhit.h */#ifndef __KBHIT_H__#define __KBHIT_H__void init_keyboard(void);void close_keyboard(void);int kbhit(void);int readch(void);#endif/*kbhit.h EOF*//*Use it like this quick and dirty example*/#include <stdlib.h>#include <stdio.h>#include "kbhit.h"int main(void){int ch;init_keyboard(); // run this once per program// one way to use itif(kbhit()) ch = readch();// here''s another sample usagedo {if(kbhit()) // kbhit returns immediately, key pressed or not{ch = readch();// do whatever here based on value of chif(ch=''q'') break;}sleep(1);} while(1);close_keyboard();return 0;} 97 61 97 61 98 62 98 62 99 63 99 63 100 64 100 64 113 71 我尝试过使用scanf()但得到了相同的结果。 关于如何输入ONE的任何想法只有人物? TIA :) Alan 113 71 I have tried using scanf() but got the same results. Any ideas as to how I can input ONE character only? TIA :) Alan 在网上搜索kbhit.c那里有一个适用于Linux的版本,我使用它很多,它解决了这些问题。只需添加kbhit.c Search the net for kbhit.c there''s a version of it out there for linux, i use it a lot and it solves these kinds of problems. Just add the kbhit.c < snip> 实现它都不可移植。如果你想讨论如何做到b 请把它带到一个适当的系统特定组。 getchar的工作原理就是它stdin的输入是 通常行缓冲(即输入被加载到一个缓冲区,直到 是一个完整的行,然后只有程序可用)。 /> 此外,如果用户将输入重定向到来自文件的,您希望发生什么?或管道输出另一个程序?使用由Eric建议的kbhit 解决方案可能会成为一个问题。 - Flash Gordon,生活在有趣的时代。 网站 - http://home.flash-gordon。 me.uk/ comp.lang.c发布指南和介绍: http://clc-wiki.net/wiki/Intro_to_clc 只需考虑一下事情。通常,您希望能够即时输入和更正输入。这意味着使用 之类的东西,比如退格键。你想要在键盘和你的程序之间完成这个编辑工作 。这意味着 是一种信号发送方式编辑完成,使用此功能。这就是 ENTER键,我们老朋友更了解返回。 现在想想这是怎么做到的,你呢将看到缓冲输入一词来自哪里。 有些系统会绕过所有这些。他们使用 非标准功能,因此不在此处。讨论 他们去一个讨论你的特定系统的新闻组。 - 一些信息链接: news:news.announce.newusers http:// www.geocities.com/nnqweb/ http://www.catb.org/~esr/faqs/smart-questions.html http://www.caliburn.nl/topposting.html http://www.netmeister.org/news/learn2quote.htmlJust think about things for a moment. In general, you want to beable to type, and correct, input on the fly. This means usingthings like the backspace key. You want this editing to be done upfront, between the keyboard and your program. This means there hasto be a means of signalling "Editing done, use this". That is theENTER key, better known to us old fogies as ''return''.Now think about how this is actually done, and you will see wherethe term ''buffered input'' comes from.Some systems make provisions for bypassing all this. They usenon-standard features, and are thus off topic here. For discussionof them go to a newsgroup that discusses your particular system.--Some informative links:news:news.announce.newusers http://www.geocities.com/nnqweb/ http://www.catb.org/~esr/faqs/smart-questions.html http://www.caliburn.nl/topposting.html http://www.netmeister.org/news/learn2quote.html 这篇关于getchar()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-11 10:53