首先usb鍵盤驅(qū)動(dòng)的源代碼位于一下目錄:
drivers/usb/input/usbkbd.c
將usb鍵盤驅(qū)動(dòng)編譯進(jìn)內(nèi)核:
#make menuconfig
Device Drivers—>USB support---->USB HIDBP Keyboard (simple Boot) support
(注意:有可能默認(rèn)設(shè)置USB鍵盤驅(qū)動(dòng)是不可見的,需修改當(dāng)前目錄下的Kconfig文件)
保存設(shè)置后,重新編譯內(nèi)核:
#source setenv
#make uImage
#make zImage
uImage生成后位于目錄:arch/arm/boot/uImage;
(或者直接將usb鍵盤驅(qū)動(dòng)編譯為驅(qū)動(dòng)模塊,進(jìn)行加載也可);
啟動(dòng)系統(tǒng)后,確定usb鍵盤加載到了那個(gè)設(shè)備文件,一般為/dev/input/event0設(shè)備,可通過cat命令進(jìn)行確認(rèn):
#cat /dev/input/event0
操作usb鍵盤,會(huì)有亂碼出現(xiàn);
然后應(yīng)用層用這個(gè)程序來獲取usb鍵盤的輸入:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
struct input_event buff;
int fd;
int read_nu;
int main(int argc, char *argv[])
{undefined
fd = open("/dev/input/event0", O_RDONLY);
if (fd < 0)
{
perror("can not open device usbkeyboard!");
exit(1);
}
int i = 0;
printf("--fd:%d--\n",fd);
while(1)
{undefined
while(read(fd,&buff,sizeof(struct input_event))==0)
{undefined
;
}
//if(buff.code > 40)
printf("type:%d code:%d value:%d\n",buff.type,buff.code,buff.value);
//#if 0
//i++;
//if(i > 12)
//{undefined
//break;
//}
//#endif
}
close(fd);
return 1;
}
運(yùn)行程序后,按下A鍵,可見如下輸出:
–fd:3–
type:1 code:30 value:1
type:0 code:0 value:0文章來源:http://www.zghlxwxcb.cn/news/detail-674587.html
按下a鍵,可見如下輸出:
type:1 code:60 value:1
type:0 code:0 value:0文章來源地址http://www.zghlxwxcb.cn/news/detail-674587.html
到了這里,關(guān)于嵌入式linux下如何使用usb鍵盤的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!