linux下实现getch()函数的功能,linux下有没有和windows下的getch函数功能基本一样的函数?...

linux下可以模拟实现一个,网上找到的getch()函数,无法识别方向键,我改了一下,使用多线程实现获取方向键的键值,具体代码如下:

#include

#include

#include

int input;

pthread_t thread[1];

pthread_mutex_t mut;

void *regetch()

{

int n = 0;

n = fgetc(stdin);

input = n;

pthread_exit(NULL);

}

int getch(void)

{

struct termios tm, tm_old;

int fd = STDIN_FILENO,c;

if(tcgetattr(fd, &tm) < 0)

return -1;

tm_old = tm;

cfmakeraw(&tm);

if(tcsetattr(fd, TCSANOW, &tm) < 0)

return -1;

int k,temp;

input = fgetc(stdin);

k = input;

if(tcsetattr(fd,TCSANOW,&tm_old)<0) return -1;

if(k > 32){ /*为了能正常输入中文,就加了这段代码*/

c = k;

}

else{

while(1)

{

memset(&thread,0,sizeof(thread));

if((temp = pthread_create(&thread[0],NULL,regetch,NULL))!=0)

printf("用于获取按键输入的线程创建失败!\n");

usleep(1000);

pthread_cancel(thread[0]);

pthread_join(thread[0],NULL);

if(k == input) { c=input; break; }

else{ k = input; c=c+k;}

}

}

return c;

}

有什么不懂的可以追问。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值