?? buttons_test.c
字號:
#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>
int main(void)
{
int i;
int buttons_fd;
int key_value[4];
/*打開鍵盤設備文件*/
buttons_fd = open("/dev/buttons", 0);
if (buttons_fd < 0) {
perror("open device buttons");
exit(1);
}
for (;;) {
fd_set rds;
int ret;
FD_ZERO(&rds);
FD_SET(buttons_fd, &rds);
/*使用系統調用select檢查是否能夠從/dev/buttons設備讀取數據*/
ret = select(buttons_fd + 1, &rds, NULL, NULL, NULL);
/*讀取出錯則退出程序*/
if (ret < 0) {
perror("select");
exit(1);
}
if (ret == 0) {
printf("Timeout.\n");
}
/*能夠讀取到數據*/
else if (FD_ISSET(buttons_fd, &rds)) {
/*開始讀取鍵盤驅動發出的數據,注意key_value和鍵盤驅動中定義為一致的類型*/
int ret = read(buttons_fd, key_value, sizeof key_value);
if (ret != sizeof key_value) {
if (errno != EAGAIN)
perror("read buttons\n");
continue;
} else {
/*打印鍵值*/
for (i = 0; i < 4; i++)
printf("K%d %s, key value = 0x%02x\n", i, (key_value[i] & 0x80) ? "released" : \
key_value[i] ? "pressed down" : "", \
key_value[i]);
}
}
}
/*關閉設備文件句柄*/
close(buttons_fd);
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -