Linux虚拟终端控制小键盘灯

Linux内核提供函数 ioctl 用于控制底层设备与描述符。参数KDSETLED指示小键盘灯的状态,0x01为scroll lock灯亮,0x02为num lock灯亮, 0x04为caps lock灯亮。

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <linux/kd.h>
#include <sys/types.h>
#include <sys/ioctl.h>

int ERROR=-1;

int main(int argc, char** argv)
{
   int fd;
   if ((fd = open("/dev/console", O_NOCTTY)) == ERROR) {
      perror("open");
      exit(ERROR);
   }
   ioctl(fd, KDSETLED, 0x01);
   usleep(50000);
   ioctl(fd, KDSETLED, 0x02);
   usleep(50000);
   ioctl(fd, KDSETLED, 0x04);
   usleep(50000);
   ioctl(fd, KDSETLED, 0x01);
   close(fd);
   return 0;
}

编译:gcc test.c -o test

root用户运行, 指示灯依次闪亮,最后scroll lock灯亮。

posted @ 2011-05-23 16:47  liyropt  阅读(1471)  评论(1编辑  收藏  举报