在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

}

在Unix/Linux下,并没有提供int kbhit(void)这个函数。在linux下开发控制台程序时,需要自己编写kbhit()实现的程序了。下面是kbhit在Unix/Linux下的一个实现。用到了一种终端 *** 作库termios。下面是头文件kbhit.h:QUOTE:#ifndef KBHITh#define KBHIThvoid init_keyboard(void)void close_keyboard(void)int kbhit(void)int readch(void)#endif下面式源程序kbhit.c:QUOTE:#include "kbhit.h"#include <stdio.h>#include <termios.h>static struct termios initial_settings, new_settingsstatic int peek_character = -1void init_keyboard(){tcgetattr(0,&initial_settings)new_settings = initial_settingsnew_settings.c_lflag &= ~ICANONnew_settings.c_lflag &= ~ECHOnew_settings.c_lflag &= ~ISIGnew_settings.c_cc[VMIN] = 1new_settings.c_cc[VTIME] = 0tcsetattr(0, TCSANOW, &new_settings)}void close_keyboard(){tcsetattr(0, TCSANOW, &initial_settings)}int kbhit(){unsigned char chint nreadif (peek_character != -1) return 1new_settings.c_cc[VMIN]=0tcsetattr(0, TCSANOW, &new_settings)nread = read(0,&ch,1)new_settings.c_cc[VMIN]=1tcsetattr(0, TCSANOW, &new_settings)if(nread == 1) {peek_character = chreturn 1}return 0}int readch(){char chif(peek_character != -1) {ch = peek_characterpeek_character = -1return ch}read(0,&ch,1)return ch}

#!/bin/bash

#任意输入5个数,判断最大值,最小值,总和

s=0

read-p"pleaseinput:"num

s=$(($s+$num))

max=$num

min=$num

avg=$(($s/5))

foriin`seq4`

do

read-p"pleaseinput:"num

s=$(($s+$num))

if[$num-le$min]then

min=$num

fi

if[$num-ge$max]then

max=$num

fi

done

echomax:$maxmin:$minavg:$avg

扩展资料:

for循环:

一般格式为:

forvarinitem1item2...itemN

do

command1

command2

...

commandN

done

写成一行:

forvarinitem1item2...itemNdocommand1command2?done


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

原文地址: http://outofmemory.cn/yw/8541210.html

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

发表评论

登录后才能评论

评论列表(0条)

保存