谁能帮我编写一个简单的程序?

simonjay2007 2009-08-04 11:27:11
谁能用C语言帮我写一个程序实现如下功能:
当我在键盘上输入字符串的时候,只要出现字母h(不按回车键)的时候就会自动输出help?(也就是按键的绑定)
...全文
115 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
fox000002 2009-08-05
  • 打赏
  • 举报
回复
其实是把输入缓冲去掉吧

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <termios.h>

int getchx(void) {
int c=0;

struct termios org_opts, new_opts;
int res=0;
//----- store old settings -----------
res=tcgetattr(STDIN_FILENO, &org_opts);
assert(res==0);
//---- set new terminal parms --------
memcpy(&new_opts, &org_opts, sizeof(new_opts));
new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
c=getchar();
//------ restore old settings ---------
res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
assert(res==0);
return(c);
}

int main()
{
while ('h' == getchx())
{
printf("help\n");
}
return 0;
}

2,163

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 UNIX文化
社区管理员
  • UNIX文化社区
  • 文天大人
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧