如何判断键盘击键?

liwz123 2003-06-13 01:42:18
如题。
我知道在windows里有个_kbhit的函数来判断,但在linux下如何判断?
望高手请教。
...全文
62 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
mingjava 2003-06-14
  • 打赏
  • 举报
回复
好像只能捕获字母 对方向键有点无能为力,我运行了一下。

另外录入有点错误,
int readcn();//should change to int readch();

int main(void)
{
int ch = 0;

init_keyboard();

/* Looping, press q to quit */
while (ch != 'q') {
printf("Looping\n");
sleep(1);
if (kbhit()) {
ch = readcn();//should change ro ch = readch();

谁有判断方向键按下的函数 谢谢
Joran 2003-06-13
  • 打赏
  • 举报
回复
我给你敲进来,干脆。
#include <stdio.h>
#include <termios.h>
#include <term.h>
#include <curses.h>
#include <unistd.h>

static struct termios initial_settings;
static struct termios new_settings;
static int peek_character = -1;
void init_keyboard();
void close_keyboard();
int kbhit();
int readcn();

int main(void)
{
int ch = 0;

init_keyboard();

/* Looping, press q to quit */
while (ch != 'q') {
printf("Looping\n");
sleep(1);
if (kbhit()) {
ch = readcn();
printf("You hit %c\n", ch);
}
}

close_keyboard();
exit(0);

}

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()
{
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;
}
wwwunix 2003-06-13
  • 打赏
  • 举报
回复
1.先安装console-tools的源码包.用rpm
2.在/usr/src/redhat可以找到源码,用tar 解开里面的.bz的包
3.查看showkey.c
mingjava 2003-06-13
  • 打赏
  • 举报
回复
怎么看showkey德源代码?能不能都说的清楚点
liwz123 2003-06-13
  • 打赏
  • 举报
回复
to Joran(老朱)
在哪里?
具体点好吗?
多谢了!
Joran 2003-06-13
  • 打赏
  • 举报
回复
BLP 上有个例子
wwwunix 2003-06-13
  • 打赏
  • 举报
回复
建议参考showkey的源代码.可以实现你的需求.

23,115

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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