在LINUX下键盘编程 编写键盘应用程序 能够获取键盘按键

在LINUX下键盘编程 编写键盘应用程序 能够获取键盘按键,第1张

提供一个输入按键应用程序实例,你参考一下。

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/ioctl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <sys/select.h>

#include <sys/time.h>

#include <errno.h>

#include <linux/input.h>

int main(void)

{

int buttons_fd

int key_value,i=0,count

struct input_event ev_key

buttons_fd = open("/dev/input/event0", O_RDWR)

if (buttons_fd <0) {

perror("open device buttons")

exit(1)

}

for () {

count = read(buttons_fd,&ev_key,sizeof(struct input_event))

for(i=0i<(int)count/sizeof(struct input_event)i++)

if(EV_KEY==ev_key.type)

printf("type:%d,code:%d,value:%d\n", ev_key.type,ev_key.code-1,ev_key.value)

if(EV_SYN==ev_key.type)

printf("syn event\n\n")

}

close(buttons_fd)

return 0

}

scanf结束标志:

① 遇空格、“回车”、“跳格”键。

② 遇宽度结束。

③ 遇非法输入。

如果要识别空格的话 有三种方法:

1.人工加空格法:

用个变量读没有空格的单词,另一个变量存储变量,变量间用空格隔开。

2.gets()函数

这个函数用法比较危险,因为它无法判字符串的长度

如char a[10]

您的输入是abcdefggjhh dddda dddd

明显超过10个字符 而a数组只是存储了10个字符

3.继续我们的scanf函数 但是有规定只能是字母跟数字组成的字符串

scanf("%[ a-zA-Z0-9]s", str)

我们来看个例子:

#include <stdio.h>

int main()

{

char str[20]

scanf("%[ a-zA-Z0-9]s", str)

printf("%s\n",str)

return 0

}

输入:

12a bbb ccc 123 1adb2

输出

12a bbb ccc 123 1adb2

希望对楼主你有所帮助

linux中有三种标准输入输出,分别是STDIN,STDOUT,STDERR,对应的数字是0,1,2。

STDIN是标准输入,默认从键盘读取信息;

STDOUT是标准输出,默认将输出结果输出至终端;

STDERR是标准错误,默认将输出结果输出至终端。

由于STDOUT与STDERR都会默认显示在终端上,为了区分,就有了编号的0,1,2的定义,用1表示STDOUT,2表示STDERR。

2>&1,指将标准输出、标准错误指定为同一输出路径

举栗子:

eg1:cat >>filetest 2>&1 <<END-------建立filetest文件,当输入遇到END时,退出

eg2:

1、以普通用户执行find /etc -name passwd命令,默认会将命令的执行结果(STDOUT)与错误信息(STDERR)都输出至终端显示器。

2、执行find /etc -name passwd >find.out 2>find.err,会将STDOUT与STDERR分别存放至find.out和find.err中。该命令也可以写成下面三种形式

          find /etc -name passwd 1>find.out 2>find.err

          find /etc -name passwd 2>find.err >find.out

          find /etc -name passwd 2>find.err 1>find.out

3、若要将所有标准输出及标准错误都输出至文件,可用&表示全部1和2的信息,eg:

          find /etc -name passwd &>find.all 或 find /etc -name passwd >find.all 2>&1

4、2>&1 ---标准错误重新定向到标准输出

5、用法:find /etc -name passwd &2>&1 |less

可分解成

find /etc -name passwd &表示前面的命令放到后台执行。

2>&1 |less 表示将标准错误重定向至标准输出,并用less进行分页显示


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8650197.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-19
下一篇2023-04-19

发表评论

登录后才能评论

评论列表(0条)

    保存